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

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

时间:2017-06-07 09:56:44      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:name   namespace   his   adt   comm   time   body   track   spi   

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&#39;s Life(并查集变形)

标签:name   namespace   his   adt   comm   time   body   track   spi   

原文地址:http://www.cnblogs.com/mthoutai/p/6955163.html

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