码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2492 A Bug's Life

时间:2014-08-02 23:33:14      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:acm   并查集   种类并查集   

A Bug‘s Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28121   Accepted: 9142

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!

传送门:点击打开链接

大致题意:
解释一下第一个样例吧:
3,3表示3个数(1,2,3),接下来3行表示每两个数之间的关系;
1 2表示1,2是恋爱关系(就当成这个意思理解吧);
2 3表示2,3是恋爱关系;
1 3表示1,3是恋爱关系;
所以这3个人中存在同性恋,输出“Suspicious bugs found!”;
对于不存在同性恋的情况输出“No suspicious bugs found!”;
现在,题目就是要求我们判断是否存在同性恋。

解题思路:
分组并查集(种类并查集)。我们可以这样思考a b存在恋爱关系,表示他们是异性,之间相互矛盾,用集合A表示与a是相同性别
的元素,集合B表示与a是不同性别的元素,这样的话a放在A集合,a+n(表示与a相异的性别)放在B集合,b放在B集合,b+n放在
A集合。如果在统计时发现,a和a+n或是b和b+n在相同的集合里面就是出现了矛盾的情况,即存在同性恋,反之,不存在。

代码:

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 2010;
int set[MAXN<<1];

int find(int p)
{
    if(set[p] < 0) return p;
    return set[p] = find(set[p]);
}

void join(int p, int q)
{
    p = find(p); q = find(q);
    if(p != q) set[p] = q;
}

int main()
{
    int t, m, n, w = 1;
    scanf("%d", &t);
    while(t--)
    {
        memset(set, -1, sizeof(set));
        scanf("%d%d" , &n, &m);
        bool flag = false;//没有矛盾情况
        while(m--)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            join(a, b+n);
            join(b, a+n);
            if(find(a)==find(a+n) || find(b)==find(b+n))
                flag = true;
        }
        if(1 != w) printf("\n");
        printf("Scenario #%d:\n", w++);
        printf("%s\n", flag ? "Suspicious bugs found!" : "No suspicious bugs found!");
    }
    return 0;
}

POJ 2492 A Bug's Life,布布扣,bubuko.com

POJ 2492 A Bug's Life

标签:acm   并查集   种类并查集   

原文地址:http://blog.csdn.net/u010084308/article/details/38351277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!