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

A Bug's Life POJ - 2492 (带权并查集)

时间:2019-01-28 19:22:43      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:round   tput   gif   from   技术分享   struct   poj   pac   wrong   

A Bug‘s Life POJ - 2492 

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.
 
题意:有一些群体的昆虫,两两一对,每一对都是在一起的,问给出的里面有没有同性恋的昆虫
题解:带权并查集,寻找与根节点的状态是否是一样的(是同性恋还是异性恋)
技术分享图片
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn =  5e4+10;
const int mod = 1e9+7;

struct node
{
    int pre;
    int relation;  //0:同 1:异
}p[maxn];

int find(int x)
{
    int temp;
    if(x == p[x].pre)
        return x;
    temp = p[x].pre;
    p[x].pre = find(temp);
    p[x].relation = (p[x].relation + p[temp].relation + 1) % 2;
    return p[x].pre;
}
void combine(int x,int y)
{
    int xx = find(x);
    int yy = find(y);
    if(xx != yy)
    {
        p[xx].pre = yy;
        p[xx].relation = (p[y].relation - p[x].relation) % 2;
    }
}
int main()
{
   int t;
   scanf("%d",&t);
   int ca=1;
   while(t--)
   {
       int n,m;
       int flag = 0;
       scanf("%d%d",&n,&m);
       for(int i=1;i<=n;i++)
       {
           p[i].pre = i;
           p[i].relation = 1;
       }
       for(int i=0;i<m;i++)
       {
           int a,b;
           scanf("%d %d",&a,&b);
           if(find(a) == find(b))
           {
               if(p[a].relation == p[b].relation)
                   flag = 1;
           }
           else
               combine(a,b);
       }
       if(flag)
            printf("Scenario #%d:\nSuspicious bugs found!\n\n",ca++);
       else
           printf("Scenario #%d:\nNo suspicious bugs found!\n\n",ca++);
   }
}
View Code

 

A Bug's Life POJ - 2492 (带权并查集)

标签:round   tput   gif   from   技术分享   struct   poj   pac   wrong   

原文地址:https://www.cnblogs.com/smallhester/p/10331303.html

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