求连通分量
Description
求一个图的连通分量
Input
n 顶点数(<=100)
边
Output
连通分量
Sample Input
8
6 3
1 2
2 5
5 4
4 1
8 7
0 0
Sample Output
4
代码
#include<stdio.h>
#include<iostream>
using namespace std;
int n,x,y=1,a[105][105],b[105],ooxx,ans;
void dfs(int x){
b[x]=1;
ooxx++;
for(int j=1;j<=n;j++)
if(a[x][j] and !b[j])dfs(j);
}
int main(){
scanf("%d",&n);
while(x!=0 or y!=0){
scanf("%d%d",&x,&y);
a[x][y]=1;a[y][x]=1;
}
for(int i=1;i<=n;i++){
ooxx=0;
dfs(i);
ans=max(ans,ooxx);
}
printf("%d
",ans);
return 0;
}
//深搜(邻接矩阵)