并查集 A Bug's Life

10 篇文章 0 订阅
5 篇文章 0 订阅
A Bug's Life
Time Limit: 4000 ms Case Time Limit: 4000 ms Memory Limit: 64 MB
Total Submission: 30 Submission Accepted: 17
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!

题意:一个教授做研究,bug这种生物之间是否有同性恋,给出带有序号bug对,即给出(1,2)代表1号bug与2号bug交配了(即认为1,2是异性),在所有情况中如果存在同性交配的,说明bug这种生物之间是存在同性恋的。这是一个并查集的拓展,将同性的合并,但给出数据却是异性的一对,所以建立一个sex数组,比如初始第一组一对bug(1,2),记sex[1] = 2, sex[2] = 1;第二组数据一对bug(2,3),已知sex[2] = 1,所以合并sex[2]和3,即1,3是同性;若第二组是(1,3)同理,已知sex[1],合并sex[1]和3.

这样合并的问题就解决了,剩下只要再输入bug对(a,b)时判断一下他们是不是同性(根节点是否相同)即可,是同性即同性恋。

#include<cstdio>
using namespace std;

const int maxn = 1000010;

int p[maxn], sex[maxn];

int Find(int x){//查找
    return x == p[x] ? x : p[x] = Find(p[x]);
}

void Union(int x, int y){//合并
    int x_root = Find(x);
    int y_root = Find(y);
    if(x_root != y_root)
        p[x_root] = Find(p[y_root]);
}

int main(){
    int n, m, t, a, b, k;
    scanf("%d", &t);
    for(int i = 1; i <= t; i++){
        k = 0;
        scanf("%d%d", &n, &m);
        for(int j = 0; j <= n; j++){
            p[j] = j;//初始化
            sex[j] = 0;//初始化所有性别为0
        }
        for(int j = 0; j < m; j++){
            scanf("%d%d", &a, &b);
            int a_root = Find(a);
            int b_root = Find(b);
            if(a_root == b_root){//如果同性,即同性恋
                k = 1;
                continue;
            }
            if(!sex[a]) sex[a] = b;//如果a的性别未知,记sex[a] = b;
            else Union(sex[a], b);//如果a的性别已知,合并sex[a]和b(同性)
            if(!sex[b]) sex[b] = a;//如果b的性别未知,记sex[b] = a;
            else Union(sex[b], a);//如果b的性别已知,合并sex[b]和a(同性)
        }
        if(k) printf("Scenario #%d:\nSuspicious bugs found!\n\n", i);
        else printf("Scenario #%d:\nNo suspicious bugs found!\n\n", i);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值