ssl1759 求連通分量

2021-07-29 12:26:18 字數 1247 閱讀 3492

求連通分量

time limit:1000ms  memory limit:65536k

total submit:240 accepted:135

description

求乙個圖的連通分量

input

n 頂點數(<=100) 邊

output

連通分量

sample input

5

1 23 4

2 30 0

sample output

4

source

elba

var

a:array[0..100,0..100]of boolean;

v:array[0..100]of boolean;

n,i,j,k,s,max:longint;

procedure dfs(i:longint);

var j:longint;//一定要!不然就會錯!(全域性變數會使深搜和迴圈的混在一起,我的正確率(π - π ))

begin

for j:=1 to n do

if (a[i,j]) and not(v[j]) then//如果其中有一條線,而且j沒被走過

begin

v[j]:=true;//走過了

inc(s);//累加當前連通分量(線的數量)

dfs(j);//深搜

end;

end;

begin

read(n);

while not(eof) do//如果有輸入就(可惜這樣寫不能直接測試,不知是自己蠢還是什麼)

begin

readln(i,j);

a[i,j]:=true;//i到j有條線

a[j,i]:=true;//j到i有條線

end;

for i:=1 to n do

begin

if s>max then max:=s;//求最大連通變數

s:=0;//重置

dfs(i);

fillchar(v,sizeof(v),false);//重置

end;

write(max);

end.

SSL 1759 求連通分量

求乙個圖的連通分量 應為最大連通分量 n頂點數 100 邊連通分量 同上 863 1225 5441 87004對於每個點,判斷是否被標記過,如果沒有,就從該點向外搜尋並標記,並對大小取最大值即可 本題寫了五種特別像的方法,請欣賞 include include using namespace st...

1759 求連通分量

求連通分量 time limit 1000ms memory limit 65536k total submit 243 accepted 136 description 求乙個圖的連通分量 input n 頂點數 100 邊 output 連通分量 sample input 5 1 23 4 2 ...

SSL1759 連通分量 五種方法

求乙個圖的連通分量 input n 頂點數 100 連線的邊 output 連通分量 sample input 86 3 1 22 5 5 44 1 8 70 0 sample output1.dfs 鄰接矩陣 2.dfs 鄰接表 3.bfs 鄰接矩陣 4.bfs 鄰接表 5.bfs 鄰接表 stl...