poj2492--A Bug's Life(并查集变形)

A Bug‘s Life

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 28703   Accepted: 9350

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个配对(男<->女)的情况。问数据有没有错误(出现同性配对)?

也就是说每一个给出的ab在不同的阵营,假设查询出现了在同一阵营就出现了错误。

c数组记录属于某一个阵营。d数组记录它所属的阵营的敌对阵营。

对每一组数据出现后。更新两个数组,注意:

假设a属于x,b属于y,那么应该讲d[y]归并到x中,y归并到d[x]中

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int c[2100] , d[2100] ;
int find1(int a)
{
    if( c[a] != a )
    {
        c[a] = find1(c[a]) ;
        d[a] = d[ c[a] ] ;
    }
    return c[a] ;
}
int main()
{
    int t , tt , i , j , n , m , a , b , flag ;
    scanf("%d", &t);
    for(tt = 1 ; tt <= t ; tt++)
    {
        scanf("%d %d", &n, &m);
        for(i = 1 ; i <= n ; i++)
            c[i] = i ;
        memset(d,-1,sizeof(d));
        flag = 0 ;
        while(m--)
        {
            scanf("%d %d", &a, &b);
            if( flag ) continue ;
            int x , y ;
            x = find1(a) ; y = find1(b) ;
            if(x == y)
                flag = 1 ;
            else if( d[x] == -1 && d[y] == -1 )
            {
                d[x] = y ; d[y] = x ;
            }
            else if( d[x] != -1 )
            {
                c[y] = d[x] ;
                if( d[y] != -1 )
                {
                    int xx = find1(d[y]);
                    c[xx] = x ;
                    d[xx] = y ;
                }
                d[y] = x ;
            }
            else
            {
                c[x] = d[y] ;
                if( d[x] != -1 )
                {
                    int yy = find1(d[x]);
                    c[yy] = y ;
                    d[yy] = x ;
                }
                d[x] = y ;
            }
        }
        printf("Scenario #%d:\n", tt);
        if( flag )
            printf("Suspicious bugs found!\n\n");
        else
            printf("No suspicious bugs found!\n\n");
    }
    return 0;
}

poj2492--A Bug's Life(并查集变形)

时间: 2024-12-23 11:25:25

poj2492--A Bug&#39;s Life(并查集变形)的相关文章

POJ2492 A Bug&#39;s Life 【并查集】

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 29011   Accepted: 9451 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

POJ2492 A Bug&#39;s Life (并查集)

本文出自:http://blog.csdn.net/svitter 题意: 给出昆虫编号,看昆虫能否交配,如果出现同性交配或者自我交配的情况,则出现BUG. 输入输出分析: 1.输入输出数据: input: 2 3 3 1 2 2 3 1 3 4 2 1 2 3 4 output: Scenario #1: Suspicious bugs found! Scenario #2: No suspicious bugs found! 第一行给出的是测试数据的个数,随后跟着n, m.n是昆虫个数,m是

【POJ】2492 A bug&#39;s life ——种类并查集

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

POJ 2492 A Bug&#39;s Life (并查集)

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 in

POJ2492A Bug&#39;s Life【并查集+根节点偏移】

大意: 告诉你一些关系 a b 代表 a 和 b 是异性 问这些关系中有没有错误的语句 分析: 有并查集维护其是否在同一个集合之中  在开一个num数组来维护对于根节点的偏移量 在find的时候只需要递归到根节点返回的过程中把num数组进行维护就可以了 在进行合并的时候我们需要考虑到把一个点的根节点并到另一个的根节点上 所以要根据u和v的偏移关系推广到u和v的根节点的关系之上 代码: 1 #include <iostream> 2 #include <cstdio> 3 #incl

hdu1829A Bug&#39;s Life(种类并查集)

传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 1 #include<bits/stdc++.h> 2 using namespace std; 3 int f[2005],n,m,rankk[2005]; 4 bool flag; 5 inline void init() 6 { 7 flag=false; 8 for(int i=0; i<=n; ++i)

hdoj 1829 A bug&#39;s life 种类并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有两种性别,每次m次操作,每次给出(a,b),如果a和b是同性别就出现了错误,也就是说出现了判断它有两种性别的错误.我的策略同样是两个集合,用并查集维护两个集合之间的关系.具体证明请看我的上一篇博客,关于这种做法的正确性的证明. 代码如下: 1 #include<bits/stdc++.h> 2 t

HDU1829 - A Bug&#39;s Life 分组并查集

HDU1829 - A Bug's Life : http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:首先输入T,接下来是T组测试数据.每组测试数据第一行输入N,M代表共有N个人,接下来有M行输入.每行有两个数a和b,代表编号为a和b的两个人是异性.输入结束后判断是否存在错误的信息.如N = 3,M = 3,接下来是3组输入.1  2,2  3,1  3.此组数据是错的,因为1 和 3都和2是异性,说明1 和 3是同性,但第三组数据又说1和3是异性

poj2492--A Bug&#39;s Life(并查集变形)

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28703   Accepted: 9350 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders