连通图
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;
}