标签:
Description
Input
Output
Sample Input
2 3 3 0 0 1 0 2 1 2 2 1 0 0 1
Sample Output
Case 1: YES Case 2: NO
题意:判断是否存在一个时候任何点都可达。
奇数环存在意义就是可以改变奇偶性。奇数走奇数环变偶数,偶数走奇数环变奇数。
所以有奇数环条件就可以满足。判断方法是经典的染色法。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1e5+100;
int t,n,m,st,flag;
int head[maxn],cnt;
struct node{
int u,v;
int next;
}e[maxn*10];
int col[maxn];
void init()
{
cnt=0;
CLEAR(head,-1);
CLEAR(col,-1);
}
void addedge(int u,int v)
{
e[cnt].u=u;
e[cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
bool BFS()
{
queue<int>q;
q.push(st);
col[st]=1;
while(!q.empty())
{
int xx=q.front();
q.pop();
for(int i=head[xx];i!=-1;i=e[i].next)
{
int to=e[i].v;
if(col[to]==-1)
{
q.push(to);
col[to]=!col[xx];
}
else if(col[to]==col[xx])
return true;
}
}
return false;
}
int main()
{
int x,y;
int cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&st);
init();
while(m--)
{
scanf("%d%d",&x,&y);
addedge(x,y);addedge(y,x);
}
if(BFS()) printf("Case %d: YES\n",cas++);
else printf("Case %d: NO\n",cas++);
}
return 0;
}
/*
2
3 3 0
0 1
0 2
1 2
2 1 0
0 1
*/
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/45442359