题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478
题解:使某一时刻到达该图任意顶点:1.图是联通的,2.无向图的每一个顶点必须保证能够在奇时刻或偶时刻可以进行访问.(二部图的顶点两个顶点集合只能在奇或偶时刻对齐进行访问)
#include <cstdio>
#include <cstring>
#include <vector>
#define MAXN 100002
using namespace std;
vector<int> mp[MAXN];
int color[MAXN],n;
int Que[MAXN];
int Scan()
{
char ch;
int ret=0;
while((ch=getchar())<'0'||ch>'9');
while(ch>='0'&&ch<='9')
{
ret=ret*10+(ch-'0');
ch=getchar();
}
return ret;
}
int BFS(int s)
{
int start=0,end=1;
int num,i,temp,x,flag,size;
flag=0;
num=1;
memset(color,-1,sizeof(color));
Que[0]=s;
color[s]=0;
while(start<end)
{
x=Que[start];
size=mp[x].size();
for(i=0;i<size;++i)
{
temp=mp[x][i];
if(color[x]==color[temp])
{
flag=1;//不是二分图
continue;
}
if(color[temp]==-1)
{
num++;
color[temp]=color[x]^1;
Que[end++]=temp;
}
}
start++;
}
if(flag&&(num==n))
return 1;
else
return 0;
}
int main()
{
int i,m,s,test,u,v,ans,cases=1;
scanf("%d",&test);
while(test--)
{
scanf("%d %d %d",&n,&m,&s);
for(i=0;i<=n;++i)
mp[i].clear();
while(m--)
{
u=Scan();
v=Scan();
mp[u].push_back(v);
mp[v].push_back(u);
}
printf("Case %d: ",cases++);
if(BFS(s))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}