POJ 1182 (weights disjoint-set, vector?)

Food chain
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 108628   Accepted: 32960

Description

There are three types of animals in the animal kingdom A, B, C, three types of animal food chain constitute an interesting ring. A food B, B eat C, C eat A. 
Animals prior N, number to 1-N. Each animal is A, B, C in kind, but we do not know it in the end is what kind of. 
It was carried out with two versions of this trophic animals consisting of N Description: 
The first argument is "1 X Y", represents the X and Y are similar. 
The second argument is "2 X Y", X represents eat Y. 
This person for N animals, with the above two statements, one sentence by sentence to say K, K this sentence some true, some false. When one of the following three words, this sentence is a lie, the truth is otherwise. 
1) if the current true, then some of the previous conflicts, is lie; 
2), then the current X or Y is larger than N, is lie; 
3) X represents eat, then the current X, is lie. 
Your job is given according to N (1 <= N <= 50,000) and K words (0 <= K <= 100,000 ), the total number of output lies. 

Input

The first line of two integers N and K, separated by a space. 
K The following three lines each is a positive integer D, X, Y, separated by a space between the two numbers, where D indicates the type of argument. 
If D = 1, it indicates that X and Y are similar. 
If D = 2, then X represents eat Y.

Output

Only one integer representing the number of lies.

Sample Input

100 7
1 101 1 
2 1 2
2 2 3 
2 3 3 
1 1 3 
2 3 1 
1 5 5

Sample Output

3 
title Chinese do not explain
though I made three or four wa, T out of the (card scanf), but it feels like to see this problem still good writing, drawing on paper will, pay attention to d [] array in the presence of a number of do not forget when (d [] + 3)% 3, because I want to ensure that d is only three numbers, 1,2,0;
1 represents fa [i] eat i
2 in turn
two representatives of ethnic 0

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector>
//const int maxn = 1e5+5;
#define ll long long
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

#define MAX INT_MAX
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
using namespace std;
int fa[51000],d[51000];
int cnt,n,m;
int Find(int x)
{
    //bug;
    if(x == fa[x]) return x;
    int root = Find(fa[x]);
    d[x] = (d[x] + d[fa[x]]+3)%3;
    return fa[x] = root;
}
int main()
{

      ios::sync_with_stdio(false);
    scanf("%d%d",&n,&m);
    FOR(i,1,n) fa[i] = i;
    FOR(i,1,m)
    {
        int k,x,y;
        scanf("%d%d%d",&k,&x,&y);
        if(x > n || y > n) {cnt++;continue;}
        if(k==2 && x==y){cnt++;continue;}
        int fx = Find(x);
        int fy = Find(y);
        if(fx == fy)
        {
            int temp = d[x] - d[y];
            if(k == 1 && temp != 0)
            {
                cnt++;
                continue;
            }
            else if(k == 2 )
            {
                if((d[x]-d[y]+3)%3 !=2 ) cnt++;
            }

        }
        else
        {
           if(k == 1)
           {
               fa[fy] = fx;
               d[fy] = (d[x] - d[y]+3)%3;
           }
           else if(k == 2)
           {
               fa[fy] = fx;
               d[fy] = (d[x] - d[y] + 1+3)%3;
           }
        }
    }
    printf("%d\n",cnt);
}

 


Guess you like

Origin www.cnblogs.com/jrfr/p/11407408.html