1. 程式人生 > 其它 >poj 1182(注意合併操作中relation的計算)

poj 1182(注意合併操作中relation的計算)

/*
100 7
1 101 1 
2 1 2
2 2 3 
2 3 3 
1 1 3 
2 3 1 
1 5 5
*/
#include<iostream>
using namespace std;
#define maxn 50005
struct node{
    int parent;
    int relation;
}p[maxn];
int find(int x){
    if(p[x].parent==x)
        return x;
    int tmp = p[x].parent;
    p[x].parent = find(tmp);
    p[x].relation 
= (p[x].relation+p[tmp].relation)%3; return p[x].parent; } int main(){ int n,k,num = 0,d,r,s; scanf("%d%d",&n,&k); for(int i=1;i<n+1;i++){ p[i].parent = i; p[i].relation = 0; } while(k--){ scanf("%d%d%d",&d,&r,&s); if(r>n||s>n){ num
++; continue; } if(d==2&&r==s){ num++; continue; } int px = find(r); int py = find(s); if(px!=py){ p[px].parent = py; p[px].relation = (d-1+p[s].relation-p[r].relation+3)%3; //擱置 }
else{ if(d==1&&p[r].relation!=p[s].relation){ num++; continue; } if(d==2&&(p[r].relation-p[s].relation+3)%3!=1){//擱置 num++; continue; } } } printf("%d\n",num); return 0; }