判断是否是连通图

连通图

Description

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

Input

n 顶点 (n<=100) 边

Output

1 表示连通
0 表示不边通

Sample Input

5
1 2
2 3
5 4
0 0

Sample Output

0
分析:由于只要判断是否是连接表,所以只需要遍历一遍图,再for循环遍历一遍,只要没有
标记过这个节点,则输出0,否则输出1!

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int b[3510],head[3510],n,s=0,f[3510];
struct node
{
	int y,next;
} v[10005];
void add(int a,int b)
{
	v[++s].y=b;
	v[s].next=head[a];
	head[a]=s;
}
void bfs(int i)
{
	int h=0,tail=1;
	queue<int>st;
	st.push(i);
	f[i]=1;
	while(st.size())
	{
	 	int x=st.front(); 
	 	st.pop();
	 	for(int j=head[x];j;j=v[j].next)
			if(!f[v[j].y])
			{
				f[v[j].y]=1; 
				st.push(v[j].y);
			}
	}
}
int main()
{
	cin>>n;
	int a,b;
	cin>>a>>b;
	while(a!=0||b!=0)
	{
		if(a==0&&b==0) break;
        add(a,b);
		add(b,a);
        cin>>a>>b;
	}
	bfs(1); 
	for(int i=1;i<=n;i++)
	{
		if(f[i]==0)
		{
			cout<<0<<endl;
			return 0;
		}
	}
	cout<<1<<endl;
	return 0;
}

谢谢!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值