|
CatchTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2228 Accepted Submission(s): 1073
Problem 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)
![]()
Source
|
题目大意:一个小偷在逃跑,判断他是否在一个时间点有在任何一个街道出现的可能(注意:他每一次必须移动到下一个街道)
解题思路:判断所给的连通图是否存在奇环(奇数个点构成的环)即可,这个用二分图来判断就可以了,因为存在奇环的一定不是二分图
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;
int n, m, cnt, flag;
int head[100010], color[100010];
struct edge
{
int to;
int next;
}e[500010*2];
void add(int u, int v)
{
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt ++;
}
void dfs(int k)
{
for(int i = head[k]; i != -1; i = e[i].next) {
int en = e[i].to;
if(!color[en]) {
color[en] = color[k]*-1;
dfs(en);
}else {
if(color[en] == color[k]) flag = 1;
}
}
}
int main()
{
int x, y, s, T, t = 1;
scanf("%d", &T);
while(T --) {
flag = 0;
scanf("%d%d%d", &n, &m, &s);
mem0(color);
mem1(head);
for(int i = 0; i < m; i ++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
color[s] = 1;
dfs(s);
if(flag) printf("Case %d: YES\n", t++);
else printf("Case %d: NO\n", t++);
}
return 0;
}