Category disjoint-set [noi2001 food chain]

Topic links: http://cogs.pro:8081/cogs/problem/problem.php?pid=pxNJzxVPU

Title three animals, A eat B, B eat C, C eat A

A food that is B, A is B natural enemies, and so on.

Therefore there may be a set of three, 0-N means an animal, NN + N represent their food, N + NN * 3 represents a predator.

code show as below:

#define _CRT_SECURE_NO_WARNINGS 
#include <the iostream> 
#include <cstdio>
 #define MAXN 50100
 the using  namespace STD;
 int N, K, D, X, Y;
 int ANS = 0 ;
 int pre [* MAXN . 3 ];              // n-kind is n + n is food, n * 3 is the natural enemy
 
int find(int x)
{
    if (x == pre[x])
        return x;
    return pre[x] = find(pre[x]);
}
 
void Union(int x, int y)
{
    pre[find(x)] = find(y);
}
 
int main ()
{
    freopen("eat.in", "r", stdin);
    freopen("eat.out", "w", stdout);
    scanf("%d %d", &N, &K);
    for (int i = 0; i < N * 3; i++)
        pre[i] = i;
 
    while (K--)
    {
        scanf("%d %d %d", &D, &x, &y);
        if (x > N || y > N)
            ans ++ ;
        else
        {
            if (D == 1)
            {        // x or y is a natural enemy of the food is x y 
                IF (Find (x + N) == Find (y) || Find (N * x + 2 ) == Find (y))
                {
                    years ++ ;
                    continues ;
                }
                the else   // the XY its kind, xy food is similar, xy predators are similar 
                {
                    Union (x, y);
                    Union (x + N, y + N);
                    Union(x + (N << 1), y + (N << 1));
                }
            }
            else
            {
                IF (x == Y || Find (x) == Find (Y) || Find (x) == Find (N + Y))    // Y and x are food grade 
                {
                    years ++ ;
                    continues ;
                }
                the else  // predators x and y are the same, the same x and y of food, predators of x and y is the same kind of food 
                {
                    Union(x, y + (N << 1));
                    Union (x + N, y);
                    Union(x + (N << 1), y + N);
                }
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/kxxy/p/11721368.html