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<cstdio> #include<cstring> using namespace std; int n,d,sum; int father[50010],relx[50010]; int find(int x) { int rx; if (x!=father[x]) { rx=find(father[x]); relx[x]=(relx[x]+relx[father[x]])%3; father[x]=rx; } return father[x]; } int un(int x,int y) { int rx=find(x),ry=find(y); if (rx==ry) { if ((relx[y]-relx[x]+3)%3==d-1) return 0; return 1; } father[ry]=rx; relx[ry]=(relx[x]-relx[y]+d+2)%3; return 0; } int main() { int k,x,y,i; sum=0; scanf("%d%d",&n,&k); for (i=1;i<=n;i++) {father[i]=i;relx[i]=0;} while (k--) { scanf("%d%d%d",&d,&x,&y); if (x>n||y>n||(x==y&&d==2)) {sum++;continue;} if (un(x,y)==1) sum++; } printf("%d\n",sum); }
原文:http://www.cnblogs.com/pblr/p/4692468.html