【图论】连通图

Description

判断一个图是否为一个边通图

Input

n n 顶点 ( n n <= 100 100 )

Output

1 1 表示连通
0 0 表示不边通

Sample Input

5
1 2
2 3 
5 4
0 0

Sample Output

0

思路

这一道其实跟求连通分量差不多
我这里用的是 b f s bfs +邻接表+ S T L STL
直接改成判断 a n s ans 是否等于 n n 就行了

#include<iostream> 
#include<cstdio>
#include<queue>
using namespace std;
struct whw
{
	int x,y;
}a[10001];
int n,m,x,y,ans,maxx;
int b[10001],h[10001];
bool wh[10001];
void bfs(int k)
{
	int head=0,tail=1;
	queue<int>wh;
	wh.push(k);
	b[k]=1;
	while(!wh.empty())
	{
		int l=wh.front();
		wh.pop();
		for(int j=h[l];j;j=a[j].y)
			if(!b[a[j].x])
			{
				b[a[j].x]=1;
				++ans;
				wh.push(a[j].x);
			}
	}
}
int main()
{
	scanf("%d",&n);
	scanf("%d%d",&x,&y); 
	while (x && y)
	{
	    a[++m]=(whw){y,h[x]};h[x]=m;
	    a[++m]=(whw){x,h[y]};h[y]=m;
		scanf("%d%d",&x,&y);
	}
	for (int i=1;i<=n;i++)
		if (!wh[i])
		{
			wh[i]=1,ans=1,bfs(i);
			if(ans==n)//只要在这改一下就行了
			{
				printf("1");
				return 0;
			}
		}
	printf("0");	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SSL_wujiajie/article/details/86511740