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


A Bug's Life

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述
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.
输入
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 10000) 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.
输出
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.
样例输入
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
样例输出
Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!
题意:
输入n个bug,bug之间有interaction,当前假设异性之间才interaction,但是需要验证,给定这些interaction对,判定是否满足假设
如果男<->女和女<->男,满足条件,如果存在男<->男或者女<->女,则假设不满足
分析:

采用并查集拓展,加入relation[]数组,其中relation[x]表示x和父节点关系,relation[x] = 0表示x和父节点关系为同性,relation[x] = 1表示x和父节点关系为异性

关于并查集,注意两个概念:按秩合并、路径压缩。
1、按秩合并
由于并查集一般是用比较高效的树形结构来表示的,按秩合并的目的就是防止产生退化的树(也就是类似链表的树),
用一个数组记录各个元素的高度(也有的记录各个元素的孩子的数目,具体看哪种能给解题带来方便),
然后在合并的时候把高度小的嫁接到高度大的上面,从而防止产生退化的树。
2、路径压缩
而另一个数组记录各个元素的祖先,这样就防止一步步地递归查找父亲从而损失的时间。因为并查集只要搞清楚各个元素所在的集合,
而区分不同的集合我们用的是代表元素(也就是树根),所以对于每个元素我们只需保存其祖先,从而区分不同的集合。
而我们这道题并没有使用纯正的并查集算法,而是对其进行了扩展,
我们并没有使用“1、按秩合并”(当然你可以用,那样就需要再开一个数组)
我们从“1、按秩合并”得到启示,保存“秩”的数组保存的是    元素相对于父节点的关系  ,我们岂不可以利用这种关系
(即相对于父节点的不同秩值来区分不同的集合),从而可以把两个集合合并成一个集合。
(注:此代码 relation=0 代表 和父节点同一性别)

 
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10000 + 10;
int father[maxn];    //记录父节点
int relation[maxn];    //relation[x]记录x与父节点之间的关系
//其中relation[x] = 0表示x和父节点关系为同性,relation[x] = 1表示为异性

void init()
{
    memset(relation, 0, sizeof(relation));
    for (int i = 0; i < maxn; i++)
        father[i] = i;
}

int Find(int x)
{
    int temp = father[x];
    if(temp == x)
        return x;
    father[x] = Find(temp);
    relation[x] = (relation[x] + relation[temp]) % 2;    //根据老的父节点和新父节点关系,修改relation[x]值
    return father[x];
}

void Union(int a, int b)
{
    int x = Find(a);
    int y = Find(b);
    if (x == y)
        return;
    father[x] = y;
    relation[x] = (relation[a] - relation[b] + 1) % 2;
    return;
}

int main()
{
    int t, cas, n, m, a, b;
    scanf("%d", &t);
    for (cas = 1; cas <= t; cas++){
        init();
        int flag = 1;
        scanf("%d%d", &n, &m);
        for (int i = 0; i < m; i++){
            scanf("%d%d", &a, &b);
            if (Find(a) == Find(b)){
                if (relation[a] != (relation[b] + 1) % 2)     //如果不满足异性关系,有矛盾
                    flag = 0;
            }
            else
                Union(a, b);
        }
        if (flag)
            printf("Scenario #%d:\nNo suspicious bugs found!\n\n", cas);
        else
            printf("Scenario #%d:\nSuspicious bugs found!\n\n", cas);
    }
    return 0;
}
        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值