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:array[0..100] of longint;
ans,n,max,x,y,i:longint;
procedure dfs(x:longint);
var
i:longint;
begin
v[x]:=false;
inc(ans);
for i:=1 to num[x] do
if v[e[x,i]] then dfs(e[x,i]);
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_83ac6af80102v0uq.html
转载时必须以链接形式注明原始出处及本声明。