HDU-1829-A Bug's Life (种类并查集)

Problem 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!

[分析]
种类并查集经典题目

首先在并查集中加入rank数组,表示当前节点与根节点之间的关系
0为同性,1为异性。
若a和b两者交配则union_set(a,b)
出现同集合且同性则为异常。

这里来说一下三个函数

第一个make_set()初始化函数,首先初始化并查集,然后初始化rank,全部置0,表示自己与自己同性别。

第二个讲union_set()
合并两个集合会影响子树的rank,所以rank需要更新。
如a,b两个节点交配,那么总是需要把一个的根节点,插到另一个根节点下,所以首先更新根节点。
不妨设a的根节点为x,b的根节点为y。所以a和x的关系为rank[a],b和y的关系为rank[b]
那么,x和y的关系是什么呢?因为a和x的关系是rank[a],又因为a和b的关系为异性(因为交配),所以x和b的关系就是rank[a]+1了(可以理解为一种传递性,同性的同性是同性,同性的异性是异性,异性的同性是异性,异性的异性是同性,和奇偶性一样),再因为b和y的关系是rank[b],所以x和y的关系就是(rank[a]+rank[b]+1)%2;

第三个讲find_set()
union_set()只将根节点的rank更新了,但是子树没有更新。这个就在 find _set里面更新。
因为,如果在union_set马上将所有子树更新是没有必要的,有的节点根本用不到。

因为没有更新子树,所以不仅仅是rank没有更新,fa也没有更新(fa数组指向根节点),所以,fa所指向的根节点是旧的那个根节点。所以我们可以根据上面union_set所描述的传递性质再写一个式子。
这里不妨设a为某个未更新的节点,x为旧的根节点,w为新的根节点。未更新的节点fa[a]指向旧根节点,
根节点的fa[x]指向新根,所以a和x的关系为rank[a],x和w的关系为rank[x]
所以式子为(rank[a]+rank[fa[a]])%2;

[代码]

#include <cstdio>
#define MAXN 2010

int rank[MAXN];
int fa[MAXN];

int t, m, n;
void make_set()
{
    for (int i = 0; i <= n; i++) {
        fa[i] = i;
        rank[i] = 0;
    }
}

int find_set(int x) { 
    if (x != fa[x]) {
        int px = find_set(fa[x]);
        rank[x] = (rank[x] + rank[fa[x]]) % 2;
        fa[x] = px;
    }
    return fa[x];
}

int union_set(int x, int y) 
{
    int px = find_set(x);
    int py = find_set(y);
    if (px == py) 
    {
        if ((rank[x] + rank[y]) % 2 == 0)return 1;
    }
    else 
    {
        fa[py] = px;
        rank[py] = (rank[x] + rank[y] + 1) % 2; 
    }
    return 0;
}

int main() 
{
    scanf("%d", &t);
    for (int k = 1; k <= t; k++) 
    {
        scanf("%d%d", &n, &m);
        make_set();
        int flag = 0;
        while (m--) {
            int x, y;
            scanf("%d%d", &x, &y);
            if (union_set(x, y)) {
                flag = 1;
            }
        }


        if (flag) {
            printf("Scenario #%d:\nSuspicious bugs found!\n\n", k);
        }
        else {
            printf("Scenario #%d:\nNo suspicious bugs found!\n\n", k);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值