解题思路:
见https://www.cnblogs.com/kkkkahlua/p/7660024.html
简单说就是只要不是二分图就有可能出现在任何地方,是的话,那未来的行动模式就确定了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
int v,next;
}s[1000009];
int head[100009],cnt,color[100009];
int n,m,st;
void add(int u,int v)
{
s[cnt].v=v;
s[cnt].next=head[u];
head[u]=cnt++;
s[cnt].v=u;
s[cnt].next=head[v];
head[v]=cnt++;
}
bool dfs(int p,int c)
{
int t;
color[p]=c;
for(int i=head[p];i!=-1;i=s[i].next)
{
t=s[i].v;
if(!color[t])
{
if(!dfs(t,-c))
{
return false;
}
}else
{
if(color[t]!=-color[p])
{
return false;
}
}
}
return true;
}
int main()
{
//freopen("t.txt","r",stdin);
int T,ca=1,u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&st);
cnt=0;
fill(head,head+n,-1);
fill(color,color+n,0);
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
}
if(!dfs(st,1))
{
printf("Case %d: YES\n",ca++);
}else
{
printf("Case %d: NO\n",ca++);
}
}
return 0;
}