(POJ2492)A Bug's Life <并查集判断是否同类>

来源:互联网 发布:fifa online3 mac 编辑:程序博客网 时间:2024/05/16 11:00

A Bug’s Life
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!

分析:
这就是用并查集判断是否为同类的题型。关于这类问题我的这篇博客已经讲解的很清楚了。
http://blog.csdn.net/STILLxjy/article/details/53444044
看懂这篇后,这题就是一模一样的了。
p[]记录根节点,r[]记录与根节点的关系,0表示同类,1表示不同类
用并查集求x,y是否在一棵树上,若在,且r[x]==r[y],这说明存在矛盾。
否则合并x,y

AC代码:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;const int maxn = 2010;int p[maxn],r[maxn];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 unit(int x,int y){    int fx = find_(x);    int fy = find_(y);    p[fx] = fy;    r[fx] = (r[x] + r[y] + 1)%2;}int main(){    int t,n,m,x,y;    int kase = 1;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        for(int i=1;i<=n;i++)        {            p[i] = i;            r[i] = 0;        }        bool flag = true;        for(int i=0;i<m;i++)        {            scanf("%d%d",&x,&y);            if(!flag) continue;            if(find_(x) == find_(y))            {                if(r[x] == r[y])                    flag = false;            }            else unit(x,y);        }        printf("Scenario #%d:\n",kase++);        if(flag) printf("No suspicious bugs found!\n\n");        else printf("Suspicious bugs found!\n\n");    }    return 0;}
1 0
原创粉丝点击