[图论]求连通分量

求连通分量


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\n",ans);
	return 0;
} 
//深搜(邻接矩阵)
posted @ 2019-12-19 17:19  学校开除后  阅读(249)  评论(0编辑  收藏  举报