1356: Catch
Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 96 Solved: 40
[Submit][Status][Web Board]
Description
A thief is running away!
We can consider
the city where he locates as an undirected graph in which nodes stand for
crosses and edges stand for streets. The crosses are labeled from 0 to N–1.
The tricky thief starts his escaping from cross S. Each moment he moves to
an adjacent cross. More exactly, assume he is at cross u at the moment t. He may
appear at cross v at moment t + 1 if and only if there is a street between cross
u and cross v. Notice that he may not stay at the same cross in two consecutive
moment.
The cops want to know if there’s some moment at which it’s possible
for the thief to appear at any cross in the city.
Input
The input contains multiple test cases:
In
the first line of the input there’s an integer T which is the number of test
cases. Then the description of T test cases will be given.
For any test
case, the first line contains three integers N (≤ 100 000), M (≤ 500 000), and
S. N is the number of crosses. M is the number of streets and S is the index of
the cross where the thief starts his escaping.
For the next M lines, there
will be 2 integers u and v in each line (0 ≤ u, v < N). It means there’s an
undirected street between cross u and cross v.
Output
For each test case, output one line to tell
if there’s a moment that it’s possible for the thief to appear at any cross.
Look at the sample output for output format.
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
HINT
For the first case, just look at the table below. (YES means the thief may appear at the cross at that moment)
For the second input, at any moment, there’s at least one cross that the thief can’t reach.
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<vector> 7 #include<queue> 8 using namespace std; 9 10 vector<int>Q[100002]; 11 int num[100002]; 12 int vis[100002]; 13 bool use[100002]; 14 queue<int>H; 15 16 void add(int x,int y) 17 { 18 num[x]++; 19 Q[x].push_back(y); 20 } 21 bool bfs(int start) 22 { 23 int i,cur,now,x; 24 H.push(start); 25 use[start]=true; 26 vis[start]=0; 27 while( !H.empty() ) 28 { 29 x=H.front(); 30 H.pop(); 31 cur=vis[x]; 32 if(cur==0) cur=1; 33 else if(cur==1) cur=0; 34 35 for(i=0;i<num[x];i++) 36 { 37 now=Q[x][i]; 38 if(use[now]==true) 39 { 40 if( (vis[now]==1&&cur==0 ) || ( vis[now]==0 && cur==1 )) 41 {// 我很奇怪,为什么if( vis[now]!=cur)就错了, 42 //照理说,我的vis[now]的值已经被修改过的,不是-1. 43 return true; 44 } 45 continue; 46 } 47 vis[now]=cur; 48 use[now]=true; 49 H.push(now); 50 } 51 } 52 return false; 53 } 54 int main() 55 { 56 int T,n,m,s,t; 57 int i,x,y; 58 scanf("%d",&T); 59 for(t=1;t<=T;t++) 60 { 61 scanf("%d%d%d",&n,&m,&s); 62 memset(num,0,sizeof(num)); 63 memset(vis,-1,sizeof(vis)); 64 memset(use,false,sizeof(use)); 65 for(i=0;i<=n;i++) 66 Q[i].clear(); 67 for(i=1;i<=m;i++) 68 { 69 scanf("%d%d",&x,&y); 70 add(x,y); 71 add(y,x); 72 } 73 printf("Case %d: ",t); 74 if( bfs(s) ==true) 75 printf("YES "); 76 else printf("NO "); 77 } 78 return 0; 79 }