求连通分量
Time Limit:1000MS Memory Limit:65536K
Total Submit:240 Accepted:135
Description
求一个图的连通分量
Input
n 顶点数(<=100)
边
Output
连通分量
Sample Input
5
1 2
3 4
2 3
0 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.