求连通分量的7个板子

题目

在这里插入图片描述
前置芝士:连通分量指最大联通子图的节点数

其一(bfs+邻接表)

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct ap
{
	int y,next;
}e[10005];
int n,ans,head[10005],a[10005],k,s;
bool v[10005];
void bfs(int i)
{
	int h=0,t=1,q[10005]={};
	q[1]=i;
	do
	{
		h++;
		for(int j=head[q[h]];j;j=e[j].next)
		{
			if(!v[e[j].y])
			{
				v[e[j].y]=1;
				s++;
				q[++t]=e[j].y;
			}
		}
	}while(h<t);
}
int main()
{
	cin>>n;
	int x,y;
	while(1)
	{
		cin>>x>>y;
		if(!x&&!y) break;
		e[++k].y=y;e[k].next=head[x];head[x]=k;
		e[++k].y=x;e[k].next=head[y];head[y]=k;
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			v[i]=1;
			s=1;
			bfs(i);
			ans=max(ans,s);
		}
	cout<<ans;
	return 0;
}

其二(bfs+邻接矩阵(vector))

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
vector<int>e[10005]; 
int n,ans,head[10005],a[10005],s;
bool v[10005];
void bfs(int i)
{
	int h=0,t=1,q[10005]={};
	v[i]=1;
	q[1]=i;
	do
	{
		h++;
		int p=q[h];
		for(int j=0;j<e[p].size();j++)
			if(!v[e[p][j]])
			{
				s++;
				v[e[p][j]]=1;
				q[++t]=e[p][j];
			}
	}while(h<t); 
}
int main()
{
	cin>>n;
	int x,y;
	while(1) 
	{
		cin>>x>>y;
		if(!x&&!y) break;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			s=1;
			bfs(i);
			ans=max(ans,s);
		}
	cout<<ans;
	return 0;
}

其三(bfs+邻接矩阵)

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,ans,head[10005],e[105][105],a[10005],s;
bool v[10005];
void bfs(int i)
{
	int h=0,t=1,q[10005]={};
	v[i]=1;
	q[1]=i;
	do
	{
		h++;
		int p=q[h];
		for(int j=1;j<=n;j++)
			if(!v[j]&&e[p][j])
			{
				s++;
				v[j]=1;
				q[++t]=j;
			}
	}while(h<t); 
}
int main()
{
	cin>>n;
	int x,y;
	while(1) 
	{
		cin>>x>>y;
		if(!x&&!y) break;
		e[x][y]=1;
		e[y][x]=1;
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			s=1;
			bfs(i);
			ans=max(ans,s);
		}
	cout<<ans;
	return 0;
}

其四(bfs+邻接表+STL)

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct ap
{
	int y,next;
}e[10005];
int n,ans,head[10005],a[10005],k,s;
bool v[10005];
void bfs(int i)
{
	int h=0,t=1;
	queue<int>q;
	q.push(i);
	while(!q.empty())
	{
		int p=q.front();
		q.pop();
		for(int j=head[p];j;j=e[j].next)
		{
			if(!v[e[j].y])
			{
				v[e[j].y]=1;
				s++;
				q.push(e[j].y);
			}
		}
	}
}
int main()
{
	cin>>n;
	int x,y;
	while(1)
	{
		cin>>x>>y;
		if(!x&&!y) break;
		e[++k].y=y;e[k].next=head[x];head[x]=k;
		e[++k].y=x;e[k].next=head[y];head[y]=k;
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			v[i]=1;
			s=1;
			bfs(i);
			ans=max(ans,s);
		}
	cout<<ans;
	return 0;
}

其五(dfs+邻接表)

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct ap
{
	int y,next;
}e[10005];
int n,ans,head[10005],a[10005],k,s;
bool v[10005];
void dfs(int i)
{
	v[i]=1;
	for(int j=head[i];j;j=e[j].next)
	{
		if(!v[e[j].y])
		{
			s++;
			dfs(e[j].y);
		}
	}
}
int main()
{
	cin>>n;
	int x,y;
	while(1)
	{
		cin>>x>>y;
		if(!x&&!y) break;
		e[++k].y=y;e[k].next=head[x];head[x]=k;
		e[++k].y=x;e[k].next=head[y];head[y]=k;
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			v[i]=1;
			s=1;
			dfs(i);
			ans=max(ans,s);
		}
	cout<<ans;
	return 0;
}

其六(dfs+邻接矩阵)

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,ans,head[105],e[105][105],a[105],m[105],k;
bool v[105];
void dfs(int i)
{
	v[i]=1;m[k]++;
	for(int j=1;j<=n;j++)
	{
		if(!v[j]&&e[j][i])
		{
			v[j]=1;
			dfs(j);
		}
	}
}
int main()
{
	cin>>n;
	int x,y;
	k=1;
	while(1) 
	{
		cin>>x>>y;
		if(!x&&!y) break;
		e[x][y]=1;
		e[y][x]=1;
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			dfs(i);
			ans=max(ans,m[k]);
			k++;
		}
	cout<<ans;
	return 0;
}

其七(dfs+邻接矩阵(vector))

#include<queue>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
vector<int>e[1005];
int n,ans,head[105],a[105],m[105],s;
bool v[10005];
void dfs(int i)
{
	v[i]=1;
	for(int j=0;j<e[i].size();j++)
	{
		if(!v[e[i][j]])
		{
			s++;
			dfs(e[i][j]);
		}
	}
}
int main()
{
	cin>>n;
	int x,y;
	while(1) 
	{
		cin>>x>>y;
		if(!x&&!y) break;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	for(int i=1;i<=n;i++)
		if(!v[i])
		{
			s=1;
			dfs(i);
			ans=max(ans,s);
		}
	cout<<ans;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值