Catch
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 859Accepted Submission(s): 432
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.
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.
先开始没想清楚,后来看了下别人的思路
发现只要判断一下连通性和二分图就可以了的说。。
唉。。我的图论太烂了。。
我的代码:
#include<stdio.h> #include<vector> #include<algorithm> #include<string.h> #define maxn 100005 using namespace std; int vis[maxn]; vector<int>graph[maxn]; int n,m; vector<int>que; void init(){ int i; for(i=0;i<=n;i++) graph[i].clear(); memset(vis,0,sizeof(vis)); que.clear(); } bool bfs(){ int i; while(!que.empty()) { int tempnode=que.front(); que.erase(que.begin()); for(i=0;i<graph[tempnode].size();i++) { int p=graph[tempnode][i]; if(vis[p]==0) { vis[p]=((vis[tempnode]==1)?2:1); que.push_back(p); } else { if(vis[p]==vis[tempnode]) return false; } } } return true; } void dfs(int u){ int i; vis[u]=1; for(i=0;i<graph[u].size();i++) { int v=graph[u][i]; if(vis[v]==0) dfs(v); } return; } int main(){ int s,t,T,i; int a,b; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%d%d%d",&n,&m,&s); init(); for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); graph[a].push_back(b); graph[b].push_back(a); } que.push_back(s); vis[s]=1; bool flag1=bfs(); bool flag2=true; memset(vis,0,sizeof(vis)); dfs(s); for(i=0;i<n;i++) if(vis[i]==0) flag2=false; if(!flag1&&flag2) printf("Case %d: YES\n",t); else printf("Case %d: NO\n",t); } return 0; }