【SSL_1759】求连通分量 #邻接矩阵 #邻接表 #dfs #bfs #队列

11 篇文章 1 订阅
4 篇文章 1 订阅

求连通分量

Time Limit:1000MS Memory Limit:65536K
Total Submit:481 Accepted:290

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


思路

图论基本例题,连通分量指的是最大连通数,这里提供五种方法:


邻接矩阵:

a[x][y]=1表示x到y存在路径,无向图所以a[y][x]也要标记为1。

1.邻接矩阵dfs

邻接矩阵存储路径,枚举每个没访问过的点开始 dfs,求出连通分量。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,x,y,a[101][101],b[101],t,head,tail,c[201],w,s,ans=0;
void in(){
	cin>>n;
	do{
		cin>>x>>y;
		if(!x) break;
		a[x][y]=1;
		a[y][x]=1;
		//邻接矩阵标记路径
	}while(1);
}
int dfs(int f){
	for(int i=1;i<=n;i++){
	//枚举每个点,若当前的点与该点存在路径且没访问过,则往该点搜索
		if(a[f][i]&&!b[i]){
			s++;
			b[i]=1;
			dfs(i);
		}
	}return s;
}
int main(){
	in();
	for(int i=1;i<=n;i++){
		s=1;
		if(!b[i]){
			b[i]=1;
			ans=max(ans,dfs(i));
		}
	}cout<<ans;
}

2.邻接矩阵bfs

邻接矩阵存储路径,枚举每个没访问过的点开始 bfs,求出连通分量。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,x,y,a[101][101],b[101],t,head,tail,c[201],w,s,ans;
void in(){
	cin>>n;
	do{
		cin>>x>>y;
		if(!x) break;
		a[x][y]=1;
		a[y][x]=1;
		//邻接矩阵标记路径
	}while(1);
}
int bfs(int f){
	head=0;tail=1;
	//head:队首, tail:队尾
	c[1]=f;
	do{
		head++;w=c[head];
		for(int i=1;i<=n;i++){
		//枚举每个点,若当前的点与该点存在路径且没访问过,则将该点加入队列
			if(a[w][i]&&!b[i]){
				b[i]=1;
				c[++tail]=i;
				s++;
			}
		}
	}while(head!=tail);
	return s;
}
int main(){
	in();
	for(int i=1;i<=n;i++){
		s=1;
		if(!b[i]){
			b[i]=1;
			ans=max(ans,bfs(i));
		}
	}cout<<ans;
}

邻接表:

邻接表
e[i].y表示路径i连通的一个点y
e[i].nt表示连接另一个点x的下一条路径存储在e中的下标
h[x]表示连接点x的路径的下标

cin>>x>>y;

e[++l].y=y;e[l].nt=h[x];h[x]=l;
e[++l].y=x;e[l].nt=h[y];h[y]=l;
// 无向图,所以路径反过来也要存储

3.邻接表dfs

邻接表存储路径,枚举每个没访问过的点开始 dfs,求出连通分量。
dfs:枚举每个当前点能通且没访问过的点搜索下去,

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,x,y,b[101],t,head,tail,c[201],w,s,ans=0,l,h[10001];
struct haha{
	int y,nt;
}e[11000];
void in(){
	cin>>n;
	do{
		cin>>x>>y;
		if(!x) break;
		e[++l].y=y;e[l].nt=h[x];h[x]=l;
		e[++l].y=x;e[l].nt=h[y];h[y]=l;
		//邻接表存储路径
	}while(1);
}
int dfs(int f){
	for(int i=h[f];i;i=e[i].nt){
	//枚举每个当前点能通且没访问过的点搜索下去
		if(!b[e[i].y]){
			b[e[i].y]=1;
			s++;
			dfs(e[i].y);
		}
	}return s;
}
int main(){
	in();
	for(int i=1;i<=n;i++){
		s=1;
		if(!b[i]){
			b[i]=1;
			ans=max(ans,dfs(i));
		}
	}cout<<ans;
}

4.邻接表bfs

跟dfs一样,枚举每个没访问过的点开始 bfs,枚举每个当前点能通且没访问过的点加入队列,求出连通分量。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,x,y,b[101],t,head,tail,c[201],w,s,ans=0,l,h[10001];
struct haha{
	int y,nt;
}e[11000];
void in(){
	cin>>n;
	do{
		cin>>x>>y;
		if(!x) break;
		e[++l].y=y;e[l].nt=h[x];h[x]=l;
		e[++l].y=x;e[l].nt=h[y];h[y]=l;
	}while(1);
}
int bfs(int f){
	head=0;tail=1;
	c[1]=f;
	do{
		head++;w=c[head];
		for(int i=h[w];i;i=e[i].nt){
			if(!b[e[i].y]){
			//枚举每个当前点能通且没访问过的点加入队列
				b[e[i].y]=1;
				c[++tail]=e[i].y;
				s++;
			}
		}
	}while(head!=tail);
	return s;
}
int main(){
	in();
	for(int i=1;i<=n;i++){
		s=1;
		if(!b[i]){
			b[i]=1;
			ans=max(ans,bfs(i));
		}
	}cout<<ans;
}

5.邻接表bfs+STL

使用queue自带队列

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,x,y,b[101],t,head,tail,c[201],w,s,ans=0,l,h[10001];
struct haha{
	int y,nt;
}e[11000];
void in(){
	cin>>n;
	do{
		cin>>x>>y;
		if(!x) break;
		e[++l].y=y;e[l].nt=h[x];h[x]=l;
		e[++l].y=x;e[l].nt=h[y];h[y]=l;
	}while(1);
}
int bfs(int f){
	queue<int> c;//定义队列
	c.push(f);//推入f
	do{
		w=c.front();//取队首
		for(int i=h[w];i;i=e[i].nt){
			if(!b[e[i].y]){
				b[e[i].y]=1;
				c.push(e[i].y);
				s++;
			}
		}c.pop();//弹出队首
	}while(!c.empty());//若队列非空,则继续循环
	return s;
}
int main(){
	in();
	for(int i=1;i<=n;i++){
		s=1;
		if(!b[i]){
			b[i]=1;
			ans=max(ans,bfs(i));
		}
	}cout<<ans;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值