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

A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 31006   Accepted: 10159

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组关系,问其中是否有同性恋现象 = =
分析:并查集
首先说明w[i]表示 i 和其父亲节点的关系,0 表示同性,1 表示异性,每次得到一组关系的时候,首先判断根节点是否相同,不同则表示这两个虫子没有发生过关系,合并就行,如果根节点相同,再判断这两个虫子和根节点的关系,如果和根节点的关系相等,则找出了suspicous bug。
 
合并更新关系的时候运用了向量偏移,u的根节点是x,v的根节点是y,p[y]=x代表y的父亲节点是x,那么只用更新根节点即可,(更新y其他的点会再合并中再更新),y到v,v到u,u到x就是y与x的关系。
w[y]=1-w[u]-w[v]因为这里可能出现负数所以w[y]=(1-w[u]-w[v]+2)%2 可以在纸上模拟一下
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=2000+5;
int p[MAXN],w[MAXN];
int kase,n,m;
bool flag;

void init()
{
    memset(p,-1,sizeof(p));
    memset(w,0,sizeof(w));
    flag=false;
}
int findfa(int x)
{
    if(p[x]==-1) return x;
    int tmp=p[x];
    p[x]=findfa(p[x]);
    w[x]=(w[x]+w[tmp])%2;
    return p[x];
}
int main()
{
    //freopen("in.txt","r",stdin);
    int Case=0;
    scanf("%d",&kase);
    while(kase--)
    {
        scanf("%d %d",&n,&m);
        init();
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            int x=findfa(u);
            int y=findfa(v);

            if(flag) continue;

            if(x==y)
            {
                if(w[u]==w[v])
                    flag=true;
            }
            else
            {
                p[y]=x;
                w[y]=(1-w[u]-w[v]+2)%2;
            }
        }
        printf("Scenario #%d:
",++Case);
        if(flag) printf("Suspicious bugs found!

");
        else printf("No suspicious bugs found!

");
    }
    return 0;
}
View Code
 
原文地址:https://www.cnblogs.com/clliff/p/4699897.html