以前做的时候好像是照抄的。。今天再做一次头脑果然还是挺乱>,<,明明只是并查集

 1 #include<bits/stdc++.h>
 2 #define inc(i,l,r) for(i=l;i<=r;i++)
 3 #define dec(i,l,r) for(i=l;i>=r;i--)
 4 #define inf 1e9
 5 #define NM 50000+5
 6 #define mem(a) memset(a,0,sizeof(a))
 7 using namespace std;
 8 int i,x,y,t,n,m,f[NM],d[NM],v[NM],s;
 9 int find(int x){
10     if(f[x]==x)return x;
11     int t=find(f[x]);
12     d[x]=(d[f[x]]-v[x]+3+d[x])%3;
13     v[x]=d[t];
14     return f[x]=t;
15 }
16 int main(){
17     scanf("%d%d",&n,&m);
18     inc(i,1,n)f[i]=i;
19     inc(i,1,m){
20         scanf("%d%d%d",&t,&x,&y);
21         if(x>n||y>n){
22             s++;continue;
23         }
24         int a=find(x),b=find(y);
25         if(a==b){
26             if(t==1&&d[x]!=d[y])s++;
27             else if(t==2&&(d[x]-d[y]+3)%3!=1)s++;
28         }else{
29             f[a]=y;
30             if(t==1){
31                 d[a]=(d[a]+d[y]-d[x]+3)%3;
32             }else{
33                 d[a]=(d[a]+d[y]-d[x]+4)%3;
34             }
35             v[a]=d[y];
36         }
37     }
38     printf("%d\n",s);
39     return 0;
40 }
View Code

 

posted on 2015-08-11 19:56  onlyRP  阅读(172)  评论(0编辑  收藏  举报