标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 62711 | Accepted: 18404 |
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include<algorithm> using namespace std; const int maxn=100007; const int INF=0x3f3f3f3f; int father[maxn]; int val[maxn]; int Find(int x) { if(father[x]==-1)return x; int tmp=Find(father[x]); val[x]+=val[father[x]]; val[x]%=3; return father[x]=tmp; } int main() { int n, m; scanf("%d %d", &n, &m); int ans=0; memset(father, -1, sizeof(father)); memset(val, 0, sizeof(val)); while(m--) { int D, x, y; scanf("%d %d %d", &D, &x, &y); if(x>n || y>n) { ans++; continue; } int a=Find(x); int b=Find(y); if(a==b) { if(D==1&&val[x]!=val[y])ans++; if(D==2&&(val[x]+1)%3!=val[y])ans++; } else { if(D==1) { father[b]=a; val[b]=val[x]-val[y]; val[b]=(val[b]+3)%3; } else { father[b]=a; val[b]=val[x]-val[y]+1; val[b]=(val[b]+3)%3; } } } printf("%d\n", ans); return 0; }
标签:
原文地址:http://www.cnblogs.com/w-y-1/p/5720833.html