标签:ted body == strong include clu void += scan
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 78551 | Accepted: 23406 |
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
思路:带权并查集,但重点还是利用向量思维进行关系的推导,比如 x->y = x->z+z->d+d->y;将关系拆解然后利用其它已知的关系推导要求的。
实现代码:
#include<iostream> #include<cstdio> const int M = 50010; int pre[M],rel[M]; using namespace std; void init(int n){ for(int i=0;i<=n;i++){ pre[i] = i; rel[i] = 0; } } int find(int x){ if(x==pre[x]) return x; int temp = pre[x]; pre[x] = find(temp); rel[x] = (rel[x]+rel[temp])%3; return pre[x]; } int mix(int d,int x,int y){ int fx = find(x); int fy = find(y); if(fx==fy){ if((rel[x]-rel[y]+3)%3==d-1) return 0; return 1; } pre[fx] = fy; //连接两个父节点 rel[fx] = (-rel[x]+d-1+rel[y]+3)%3; return 0; } int main() { int n,m,ans=0,d1,x1,y1; scanf("%d%d",&n,&m); init(n); for(int i=0;i<m;i++){ scanf("%d%d%d",&d,&x,&y); if(x1==y1&&d1==2) ans++; else if(x1>n||y1>n) ans++; else ans+=mix(d1,x1,y1); } printf("%d",ans); }
标签:ted body == strong include clu void += scan
原文地址:http://www.cnblogs.com/kls123/p/7804427.html