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

来源:互联网 发布:linux重启监听服务 编辑:程序博客网 时间:2024/04/29 14:45

A Bug's Life

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

23 31 22 31 34 21 23 4

Sample Output

Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!

题目大意:某个教授在研究某种虫子的性取向(好污。。。),他认为这种虫子只与异性交流,不与同性交流。现在给出虫子之间的交流关系,验证教授的猜想是否正确。

解题思路:看到这道题才觉得 POJ1182食物链 真是经典。这道题和食物链差不多,只不过这道题将虫子分为两大类,而食物链将动物分成三类。这道题思路和食物链大体相同,就是如果a、b之间发生交流,那么合并a和b + n、a + n和b。每次合并之前要判断这次要合并的两个虫子是否合法,即判断a和b、a + n和b + n是否在同一组,如果在同一组说明不合法的交流发生了,设置flag为1,注意这时不要break。。

代码如下:

#include <algorithm>#include <cctype>#include <climits>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <queue>#include <set>#include <stack>#include <vector>#define EPS 1e-6#define INF INT_MAX / 10#define LL long long#define MOD 100000000#define PI acos(-1.0)const int maxn = 4005;int par[maxn],rank[maxn];void init(int n){    for(int i = 1;i <= n;i++){        par[i] = i;        rank[i] = 0;    }}int find(int x){    return par[x] == x ? x : par[x] = find(par[x]);}void unite(int x,int y){    x = find(x);    y = find(y);    if(x == y)        return ;    if(rank[x] < rank[y])        par[x] = y;    else{        par[y] = x;        if(rank[x] == rank[y])            rank[x]++;    }}bool same(int x,int y){    return find(x) == find(y);}int main(){    int t,ncase,n,m;    ncase = 0;    scanf("%d",&t);    while(t--){        bool flag = 0;        scanf("%d %d",&n,&m);        init(2 * n);        int a,b;        while(m--){            scanf("%d %d",&a,&b);            if(same(a,b) || same(a + n,b + n)){                flag = 1;                //break;            }            else{                unite(a,b + n);                unite(a + n,b);            }        }        printf("Scenario #%d:\n",++ncase);        printf("%s\n\n",flag ? "Suspicious bugs found!" : "No suspicious bugs found!");    }    return 0;}


0 0
原创粉丝点击