POJ 2492 A Bug's Life【并查集的简单应用同类的判断】

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 23484   Accepted: 7639

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

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany

题意:给你 N 条虫 , 和它们间的 M 条关系。

      每条关系 X  Y 表示 X 和 Y 不同类 ,在检验每条关系的时候,一旦发现了矛盾

      也就是当前的这组 X 和 Y 根据前面的关系已经判定是同类的,但是这里给出的关系又是应该不同类,

      则输出 Suspicious bugs found!

如果从未发生矛盾,则输出No suspicious bugs found!


算法:并查集的简单应用,判断是否是同类。

注意:输出格式【换行,和属于第几组数据】

思路:用数组 r[]记录与父亲节点的关系,0 则同类 1 则异类

用并查集检验是否有关系:如果有关系,r[]同则矛盾。

如果没有关系,则合并两棵子树。

I Accepted 156 KB 719 ms C++ 1105 B 2013-04-10 19:46:59

#include<cstdio>

const int maxn = 2000+10;

int p[maxn]; //记录父节点
int r[maxn]; //记录与父节点关系, 0 同类, 1异类

int find(int x)
{
    if(x == p[x]) return x;

    int t = p[x];
    p[x] = find(p[x]);
    r[x] = (r[x]+r[t])%2; //每次回溯更新一次父节点,相应更新关系
    return p[x];
}

void Union(int x, int y)
{
    int fx = find(x);
    int fy = find(y);

    p[fx] = fy; //任意
    r[fx] = (r[x]+1+r[y])%2; //r[]没有方向
}
void set(int n)
{
    for(int i = 1; i <= n; i++)
    {
        p[i] = i;
        r[i] = 0;
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    for(int i = 1; i <= T; i++)
    {
        int n, m;
        scanf("%d%d", &n, &m);

        set(n);

        int x, y;
        bool flag = true;
        while(m--)
        {
            scanf("%d%d", &x, &y); //本应不同类
            if(find(x) == find(y))
            {
                if(r[x] == r[y]) //如果同类
                {
                    flag = false;
                    continue;
                }
            }
            else Union(x, y);
        }
        printf("Scenario #%d:\n", i);
        if(flag) printf("No suspicious bugs found!\n");
        else printf("Suspicious bugs found!\n");
        printf("\n");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/freezhan/p/3219076.html