poj 1182

题意:有一大堆动物,他们只有三种(A,B,C),A吃B,B吃C,C吃A


给你一堆关系,如果某个关系和之前给出的某个关系冲突就认为这个新给出的关系错误,问一共错了多少组关系


题解:并查集.......直接附代码吧,当时花了好久才看懂怎么写......

#include <stdio.h>
#include <algorithm>
#include <string.h>
#define maxn 50010
using namespace std;
int father[maxn*3];
int find_father(int x){
    if(x==father[x])return x;
    else find_father(father[x]);
}
int same(int x,int y){
    if(find_father(x)==find_father(y))return 1;
    return 0;
}
int unite(int x,int y){
    father[find_father(y)]=find_father(x);
}
int main(){
    int N,K;
    scanf("%d%d",&N,&K);{
        memset(father,0,sizeof(father));
        for(int i=1;i<=N*3;i++)father[i]=i;
        int ans=0;
        int type,x,y;
        while(K--){
            scanf("%d%d%d",&type,&x,&y);
            if(x>N || y>N || x<1 || y<1){
                ans++;
                continue;
            }
            if(type==1){
                if(same(x,y+N) || same(x,y+N*2)){
                    ans++;
                    continue;
                }
                else{
                    unite(x,y);
                    unite(x+N,y+N);
                    unite(x+N*2,y+N*2);
                }
            }
            if(type==2){
                if(same(x,y) || same(x,y+N*2)){
                    ans++;
                    continue;
                }
                else{
                    unite(x,y+N);
                    unite(x+N,y+N*2);
                    unite(x+N*2,y);
                }
            }
        }
        printf("%d\n",ans);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值