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

本文介绍了一种通过并查集算法验证虫子互动是否符合仅异性互动假设的方法。该算法将虫子分为两大类,并检查互动是否违反了这一基本假设。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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!

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

解题思路:看到这道题才觉得 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值