求连通分量-方法2(图论算法)

Description

求一个图的连通分量

Input

n 顶点数(<=100) 

Output

连通分量

Sample Input

 


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
转载时必须以链接形式注明原始出处及本声明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值