Description
Input
Output
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
#include<iostream> #include<cstdio> using namespace std; int N,K,relation,a,b,count; int p[50010],r[50010]; void make(){ for(int i=1;i<=N;i++){ p[i]=i; r[i]=0; } } int find(int x){ int temp=p[x]; if(x==p[x])return p[x]; p[x]=find(p[x]); r[x]=(r[x]+r[temp])%3; return p[x]; } void unionSet(int x,int y,int px,int py){ p[px]=py; r[px]=(r[y]-r[x]+3+relation-1)%3; } int main(){ cin>>N>>K; int pa,pb; count=0; make(); while(K--){ scanf("\n%d%d%d",&relation,&a,&b); pa=find(a); pb=find(b); if(a>N||b>N||(relation==2&&a==b)){count++;continue;} if(pa!=pb){unionSet(a,b,pa,pb);continue;} if((r[a]-r[b]+3)%3!=(relation-1)){count++;continue;} } cout<<count<<endl; }