SSL 1757 求連通分量

2021-07-29 08:16:07 字數 974 閱讀 9201

description

求乙個圖的連通分量

input

n 頂點數(<=100)

邊output

連通分量

sample input

5 1 2

3 4

2 3

0 0

sample output

這題不是很難,主要要在意如果dfs回溯的話,會超時!

這題在dfs的基礎上減少了回溯的部分,方可ace

**如下:

var  n,x,y,i,ans,max,maxn:longint;

a:array[0..101,0..101]of longint;

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

procedure

dfs(x:longint);

var i:longint;

begin

for i:=1

to n do

if (a[i,x]=1)and(v[i]=false) then

begin

v[i]:=true;

inc(ans);

dfs(i);

end;

end;

begin

readln(n);

readln(x,y);

while (x<>0)or(y<>0) do

begin

a[x,y]:=1;

a[y,x]:=1;

readln(x,y);

end;

for i:=1

to n do

begin

ans:=0;

dfs(i);

if ans>max then max:=ans;

end;

write(max);

end.

BFS 連通分量 求連通分量

題目描述 求乙個圖的連通分量 input n 頂點數 100 邊 以0 0作為結束標誌 output 連通分量 強連通圖的連通分量為其本身。如果為非連通圖,則連通分量為該圖的最大連通子圖。分析 建乙個100 100的布林矩陣,b x,y true表示x與y連通。同時還要記錄該點是否被遍歷過 然後遍歷...

ssl1759 求連通分量

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

SSL 1759 求連通分量

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