标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 63050 | Accepted: 18482 |
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
Source
#include<cstdio> #include<iostream> using namespace std; #define maxsize 50050 int pre[maxsize]; int dist[maxsize]; int init(int n) { for(int i=1;i<=n;i++) { pre[i]=i; dist[i]=0; } } int find(int x) { if(x==pre[x]) return x; int tmp=find(pre[x]); dist[x]=(dist[pre[x]]+dist[x])%3; pre[x]=tmp; return tmp; } int Union(int w,int u,int v) { int p=find(u); int q=find(v); if(p!=q) { pre[p]=q; dist[p]=(3+w+dist[v]-dist[u])%3; } else { if((dist[u]-dist[v]+3)%3!=w) return 0; } return 1; } int main() { int n,k; scanf("%d %d",&n,&k); int cnt=0; init(n); int w,u,v; while(k--) { scanf("%d %d %d",&w,&u,&v); if(u>n||v>n||(w==2&&u==v)) {cnt++;} else if(!Union(w-1,u,v)) {cnt++;} } printf("%d\n",cnt); return 0; }
标签:
原文地址:http://www.cnblogs.com/hellohacker/p/5722087.html