Description
求一个图的连通分量
Input
n 顶点数(<=100)
边
Output
连通分量
Sample Input
5
1 2
3 4
2 3
0 0
Sample Output
4
解题思路:用广度优先搜索建立图的邻接表。
程序:var
e:array[0..100,0..100] of longint;
v:array[0..100] of boolean;
num,state:array[0..100] of longint;
ans,n,max,x,y,i:longint;
procedure dfs(x:longint);
var
head,tail,i:longint;
begin
head:=0;
tail:=1;
state[1]:=x;
v[x]:=false;
ans:=1;
repeat
inc(head);
for i:=1 to num[state[head]] do
if v[e[state[head],i]] then
begin
inc(ans);
inc(tail);
state[tail]:=e[state[head],i];
v[e[state[head],i]]:=false;
end;
until head>=tail;
end;
begin
readln(n);
readln(x,y);
while (x>0) and (y>0) do
begin
inc(num[x]);
e[x,num[x]]:=y;
inc(num[y]);
e[y,num[y]]:=x;
readln(x,y);
end;
fillchar(v,sizeof(v),true);
max:=0;
for i:=1 to n do
if v[i] then
begin
ans:=0;
dfs(i);
if ans>max then max:=ans;
end;
writeln(max);
end.
版权属于:Chris
原文地址:http://blog.sina.com.cn/s/blog_83ac6af80102v0us.html
转载时必须以链接形式注明原始出处及本声明。