POJ 1182

食物链:

网上看的别人的加过注释的代码,对于rank的解释很好,就摘录了

#include<iostream>
using namespace std;
const int Max = 50005;
 
int n, pa[Max], rank[Max];
 
void make_set(){
    for(int x = 1; x <= n; x ++){
        pa[x] = x;
        rank[x] = 0;   //  开始时集合的根也是x,属于同种类型,故类型差值为0。
    }
}
 
int find_set(int x){
    int tmp = pa[x];
    if(x != pa[x]){
        pa[x] = find_set(pa[x]);
        rank[x] = (rank[x] + rank[tmp]) % 3;
        //rank[x]是x相对原来的根元素tmp(前根节点)的差值,现在的根元素已不是tmp。
        //故要加上tmp对于现在的根元素的类型差值,rank[tmp]为tmp对于现在根元素类型的差值。
    }
    return pa[x];
}
 
void union_set(int x, int y, int w){
    int a = find_set(x);
    int b = find_set(y);
    pa[b] = a;
    rank[b] = (rank[x] - rank[y] + w + 3) % 3;
    //这个公式可用列表推出,列表时必须保证rank[x]不变,由于根节点的rank[a]总为0。
    //然后如果x吃y,则rank[y] = rank[x] + 1,然后可推出rank[b]的改变量。x与y同类也运用这个思路。
}
 
int main(){
    int t, ans = 0;
    scanf("%d %d", &n, &t);
    make_set();
    while(t --){
        int d, x, y;
        scanf("%d %d %d", &d, &x, &y);
        if(x > n || y > n) ans ++;   //  当前的话中X或Y比N大,就是假话。 
        else{
            if(d == 1){
                if(find_set(x) == find_set(y) && rank[x] != rank[y]) ans ++;
                else union_set(x, y, 0);
            }
            if(d == 2){
                if(find_set(x) == find_set(y) && (rank[x]+1) % 3 != rank[y]) ans ++;
                else union_set(x, y, 1);
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值