HDU 3478 Catch【二分图+思维】

来源:互联网 发布:弹弓淘宝网 编辑:程序博客网 时间:2024/06/14 22:17

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

题意:n,m,s分别表示地点的数,边的个数,出发点,可以看成无向图,问你从s点出发一直不断地跑,问你是否存在一个时间点,使得他的位置在任意地点都有可能.
思路:(题意稍微难懂点)其实用笔画画,就是问你是否存在二分图,存在NO(稍微注意下,多个图的情况),否则YES.

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <cmath>#include <queue>using namespace std;const int MAXN = 5e5 + 10;int vis[MAXN];vector<int> v[MAXN];bool flag;void dfs(int x, int col) {    vis[x] = col;    for(int i = 0; i < v[x].size(); i++) {        int cnt = v[x][i];        if(vis[cnt] == vis[x]) flag = true;        else if(vis[cnt] == -1) dfs(cnt, !col);    }}void init() {    memset(vis, -1, sizeof(vis));    for(int i = 0; i < MAXN; i++) {        v[i].clear();     }}int main() {    int T, p = 1;    scanf("%d", &T);    while(T--) {        init();        int n, m, s, p1, p2;        flag = false;        scanf("%d %d %d", &n, &m, &s);        for(int i = 1; i <= m; i++) {            scanf("%d %d", &p1, &p2);            v[p1].push_back(p2);            v[p2].push_back(p1);        }        dfs(s, 0);        for(int i = 0; i < n; i++) {            if(vis[i] == -1) {                 flag = false; //不止一张图                 break;            }        }        printf("Case %d: ", p++);        puts(flag ? "YES" : "NO");    }    return 0;}