码迷,mamicode.com
首页 > 其他好文 > 详细

poi 1182

时间:2020-10-07 21:22:07      阅读:29      评论:0      收藏:0      [点我收藏+]

标签:else   tmp   printf   cst   strong   ++i   amp   rgba   const   

食物链 || 带权并查集

#include <cstdio>
using namespace std;
const int maxn=5e4+3;

int f[maxn], w[maxn];

void init(int n)
{
    for(int i = 1; i <= n; ++i) f[i] = i, w[i] = 0;
}

int find(int x)
{
    if(x == f[x]) return x;
    int tmp = f[x];
    f[x] = find(f[x]);
    w[x] = (w[x] + w[tmp] + 3) % 3;
    return f[x];
}

int main()
{
    int n, k, d, x, y, cnt = 0;
    scanf("%d %d", &n, &k);
    init(n);
    for(int i = 0; i < k; ++i)
    {
        scanf("%d %d %d", &d, &x, &y);
        if(x > n || y > n || (d == 2 && x == y))
        {
            ++cnt; continue;
        }
        int xx = find(x), yy = find(y);
        if(xx == yy)
        {
            if((w[x] - w[y] + 3) % 3 != d - 1) ++cnt;
        }
        else
        {
            f[xx] = yy;
            w[xx] = (w[y] - w[x] + d - 1 + 3) % 3;
        }
    }
    printf("%d\n", cnt);
}

 

poi 1182

标签:else   tmp   printf   cst   strong   ++i   amp   rgba   const   

原文地址:https://www.cnblogs.com/Maxx-el/p/13776996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!