POJ 1182 食物链(带权并查集)

题目链接:POJ 1182 食物链

没学带权并查集时候做过一次这个题,没做出来,然后看了题解被那些看起来很巧妙的通项公式吓到了,放弃了。现在再做这个题,发现这个题是可以不用那些通项公式的,完全可以把几种情况列出来做。

其实把一些情况列出来之后连蒙带猜也是可以得出来通项公式的,并没有我开始想的那么复杂。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAX_N = 50000 + 100;
int n, k, p[MAX_N], _rank[MAX_N];

int _find(int x)
{
    if(p[x] != x)
    {
        int root = _find(p[x]);
        _rank[x] = (_rank[x] + _rank[p[x]]) % 3;
        return p[x] = root;
    }
    return x;
}
int main()
{
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        p[i] = i;
    int o, a, b, u, v, res = 0;
    for(int i = 0; i < k; i++)
    {
        scanf("%d%d%d", &o, &a, &b);
        if((o == 2) && (a == b) || a > n || b > n)
        {
            res++;
            continue;
        }
        u = _find(a);
        v = _find(b);
        if(u == v)
        {
            /*if(o == 1)
                if(!(_rank[a] == _rank[b]))
                    res++;
            if(o == 2)
                if(!((_rank[b] == 1 && _rank[a] == 2) || (_rank[b] == 2 && _rank[a] == 0) || (_rank[b] == 0 && _rank[a] == 1)))
                    res++;*/
             if((_rank[a] - _rank[b] + 3) % 3 != o - 1)
                res++;
            continue;
        }
        p[u] = v;
        _rank[u] = (_rank[b] - _rank[a] + o - 1 + 3) % 3;
    }
    printf("%d\n", res);
    return 0;
}

算是今天才彻底搞懂吧。看了http://blog.csdn.net/niushuai666/article/details/6981689 15/11/17

/**
relation[x] 表示rootx->x的值 注意不要搞反
x->y=1 x吃y
x->y=2 y吃x
x->y=0 x与y是同类
根据向量得到所求relation的值
*/

#include <iostream>
#include <cstdio>
#include <cstring>

const int MAX_N = 50000 + 100;

using namespace std;

struct Ani
{
    int pa, re;
};

Ani anis[MAX_N];

int _find(int x)
{
    if(x == anis[x].pa)
        return x;
    int tempf = anis[x].pa;
    anis[x].pa = _find(anis[x].pa);
    anis[x].re = (anis[tempf].re + anis[x].re) % 3;
    return anis[x].pa;
}

int main()
{
    int n, k, sum = 0;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
    {
        anis[i].pa = i;
        anis[i].re = 0;
    }
    int d, x, y;
    for(int i = 0; i < k; i++)
    {
        scanf("%d%d%d", &d, &x, &y);
        if(x > n || y > n)
        {
            sum++;
            continue;
        }
        if(d == 1)
        {
            int xf = _find(x);
            int yf = _find(y);
            if(xf != yf)
            {
                anis[xf].pa = yf;
                anis[xf].re = (anis[y].re + 3 - anis[x].re) % 3;
            }
            else
            {
                int now = (3 - anis[x].re + anis[y].re) % 3;
                if(now != d - 1)
                    sum++;
            }
        }
        else
        {
            int xf = _find(x);
            int yf = _find(y);
            if(xf != yf)
            {
                anis[xf].pa = yf;
                anis[xf].re = (anis[y].re + 3 - (d - 1) + 3 - anis[x].re) % 3;
            }
            else
            {
                int now = (3 - anis[x].re + anis[y].re) % 3;
                if(now != d - 1)
                    sum++;
            }
        }

    }
    printf("%d\n", sum);
    return 0;
}


你可能感兴趣的:(POJ 1182 食物链(带权并查集))