【并查集】hdu 1829 A Bug's Life

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7507    Accepted Submission(s): 2417


Problem 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.
 
题目意思:给定一系列数对,例如a和b,表示a和b不是同一种性别,然后不断的给出这样的数对,问有没有性别不对的情况
例如给定:
1    2
3    4
1    3
那这里就是说1和2不是同种性别,3和4也不是同种性别,1和3不是同种性别,那这样就说明1和3是同一种性别,2和4是同一种性别,所以没有任何歧义,这时候输出No suspicious bugs found!
但是例如
1 2
2 3
1 3
1和2不同性别,2和3不同性别,那么1和3同一性别的,但是第三组数对又表明1和3不同性别,
所以这里就出现了3或者1的性别出现了歧义,也就是说条件矛盾,这时候输出Suspicious bugs found!

很明显的分组并查集的题目,可以考虑开两个并查集,然后合并的时候要合并两次,这样在合并之前判断是否冲突,
如果不冲突就进行合并,否则不需要继续合并

具体:
pre数组开的大小为最大数的两倍,这样其实就是两组并查集(1-MAX, MAX + 1-2*MAX),
这两组分别代表了不同的性别组,如果输入的两个数在同一组中具有相同的根,则就能说明冲突。
例如输入a,b,此时要把(a,MAX + b)及( b,MAX + a)合并起来,试想下次如果输入了c,b,
则很明显会把(c,MAX + b)以及(b,MAX + c),合并起来,
这时候我们就会发现其实a和c具有了相同的根,所以当如果在输入a,c时,
就会发现a和c在同一组中具有了相同的根,这就是冲突了


#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX = 2000;

int pre[2 * MAX + 5];
bool mark;

void init(int n)
{
    int i;
    (author:CSDN:凌风)
    for (i = 1; i <= MAX + n; ++i)
    {
        pre[i] = i;
    }
    mark = true;
}
int root(int x)
{
    if (x != pre[x])
    {
        pre[x] = root(pre[x]);
    }
    return pre[x];
}
void merge(int x, int y)
{
    int fx, fy;
    fx = root(x);
    fy = root(y - MAX);

    if (fx == fy)
    {
        mark = false;
        return;
    }

    fy = root(y);
    if (fx != fy)
    {
        pre[fx] = pre[fy];
    }
}
int main()
{
    int t, i, n, m, x, y, k;
    scanf("%d", &t);
    for (i = 1; i <= t; ++i)
    {
        scanf("%d %d", &n, &m);
        init(n);
        for (k = 1; k <= m; ++k)
        {
            scanf("%d %d", &x, &y);
            if (mark)
            {
                merge(x, y + MAX);
                merge(y, x + MAX);
            }
        }
        printf("Scenario #%d:\n", i);
        if (mark)
        {
            printf("No suspicious bugs found!\n");
        }
        else
        {
            printf("Suspicious bugs found!\n");
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值