食物链OpenJ_Bailian - 1182

 //当init在n,k输入之后时不会有输出(在Clion和Qt上是这样的),不过提交了能AC

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 50000+5;

int pre[N*3];//自己,吃 , 被吃
int n, k;

void init()
{

    for(int i=0; i<=N*3; i++){
        pre[i] = i;
    }

}

int Find(int a)
{
    return (pre[a]==a) ? a : Find(pre[a]);
}
/*
int Find(int x)//查询
{
    while(pre[x] != x)
        x = pre[x];
    return x;
}
 */
void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);

    if(a != b)
        pre[b] = a;
    //   pre[Find(b)] = Find(a);
}


int main()
{
 //   cin >> n >> k;
    scanf("%d %d", &n, &k);

    init();

    int ans = 0;

    while(k--){
        int a, b, c;

    //    cin >> a >> b >> c;

        scanf("%d %d %d", &a, &b, &c);

        if(b > n || c > n || (a == 2 && b == c)) {
            ans++;
//            continue;
        }

        else if(a == 1){
            //conflict
            if(Find(b) == Find(c+n) || Find(b) == Find(c+2*n)){
                ans++;
//                continue;
            }
//b,c是同类
            else{
                Union(b, c);
                Union(b+n, c+n);
                Union(b+2*n, c+2*n);
            }

        }

       else if(a == 2){
            if(Find(b) == Find(c+n) || Find(b) == Find(c)){
                ans++;
   //             continue;
            }
            //b, c属于不同类且符合b吃c
            else{
                Union(b, c+n*2);//b和吃c的
                Union(b+2*n, c+n);//吃b的和被c吃的
                Union(b+n, c);//b吃的和c
            }
        }

    }
//    cout << ans << endl;

    printf("%d\n", ans);

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值