hdu 1829 A Bug's Life

来源:互联网 发布:用友u8安装数据库 编辑:程序博客网 时间:2024/06/05 18:01

http://acm.hdu.edu.cn/showproblem.php?pid=1829

A Bug's Life
Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. 
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

Output

The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

Sample Input

23 31 22 31 34 21 23 4

Sample Output

Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!

Hint

Huge input,scanf is recommended.

准确点说应该是读入的这对数x,y,到树根的距离的奇偶性是不可以相同的,因为如果他们到根的奇偶性相同,就说明他们是同性
搜别人的博客,别人都说这是道水题,可是我还是不会,好伤心伤心哭哭哭

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 125000#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8int n, m, f[N], r[N];int Find (int x){    int t = f[x];    if (x != f[x])    {        f[x] = Find (f[x]);        r[x] = (r[x] + r[t]) % 2;    }    return f[x];}int main (){    int t, k = 1;    cin >> t;    while (t--)    {        cin >> n >> m;        for (int i=1; i<=n; i++)        {            f[i] = i;            r[i] = 0;        }        int a, b, flag = 1;        for (int i=0; i<m; i++)        {            cin >> a >> b;            int x = Find (a), y = Find (b);            if (x == y)            {                if (r[a] == r[b])//性别相同                    flag = 0;            }            else            {                f[x] = y;//性别不同,就把两个归为一个根节点                r[x] = (r[a] + r[b] + 1) % 2;            }        }        cout << "Scenario #" << k++ << ':' << endl;        if (!flag) cout << "Suspicious bugs found!" << endl;        else cout << "No suspicious bugs found!" << endl;        cout << endl;    }    return 0;}


0 0