求连通分量

求连通分量
Time Limit:1000MS Memory Limit:65536K
Total Submit:233 Accepted:132

Description
求一个图的连通分量

Input
n 顶点数(<=100)边

Output
连通分量

Sample Input
5
1 2
3 4
2 3
0 0

Sample Output
4

首先解释一下样例输入:
5 //顶点的数量
1 2 //顶点1和顶点2相连
3 4 //顶点3和顶点4相连
2 3 //顶点2和顶点3相连
0 0 //两个零表示结束

输出的就是最多有几个点是连在一起的

这题在dfs的基础上减少了回溯的那一部分,输入稍微麻烦一点


var
  i,n,x,y,ans,max:longint;
  b:array[0..101,0..101]of boolean;
  a:array[0..101]of boolean;
procedure dfs(k:longint);
var i:longint;
begin
  for i:=1 to n do
    if (b[i,k])and(a[i]=false) then
    begin
      a[i]:=true;
      inc(ans);
      dfs(i);
    end;
end;
begin
  readln(n);
  readln(x,y);
  while (x<>0)or(y<>0) do
  begin
    b[x,y]:=true;
    b[y,x]:=true;
    readln(x,y);
  end;
  for i:=1 to n do
  begin
    ans:=0;
    dfs(i);
    if ans>max then max:=ans;
  end;
  writeln(max);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值