扩展并查集——POJ - 1182

题目含义

找出与之前的话不符的假话的数目

题目分析

简单的扩展并查集

题目代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=5e4+7;
int f[maxn*3];
int n,k,d,x,y;
int get(int x){
    if(f[x]==x)return x;
    else return f[x]=get(f[x]);
}
int main(){
    scanf("%d%d",&n,&k);
    int ans=0;
    for(int i=1;i<=n;i++)
        f[i]=i;
    for(int i=0;i<k;i++){
        scanf("%d%d%d",&d,&x,&y);
        if(x>n||y>n){
            ans++;
            continue;
        }
        int fx_sel=get(x),fx_eat=get(x+n),fx_ene=get(x+n+n);
        int fy_sel=get(y),fy_eat=get(y+n),fy_ene=get(y+n+n);
        if(d==1){
            if(fx_sel==fy_eat||fx_sel==fy_ene){
                ans++;
                continue;
            }
            f[fy_sel]=fx_sel;
            f[fy_eat]=fx_eat;
            f[fy_ene]=fx_ene;
        }
        else if(d==2){
            if(fx_sel==fy_sel||fx_sel==fy_eat||x==y){
                ans++;
                continue;
            }
            f[fy_sel]=fx_eat;
            f[fy_ene]=fx_sel;
            f[fy_eat]=fx_ene;
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/helman/p/11233921.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值