码迷,mamicode.com
首页 > 其他好文 > 详细

食物链

时间:2018-07-19 21:24:47      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:http   targe   get   style   org   show   col   ||   https   

 

https://www.luogu.org/problemnew/show/P2024

#include<cstdio>
int f[50005];
int r[50005];
void init(int n){
    for(int x=1; x<=n; x++){
        f[x]=x; 
        r[x]=0;
    }
}
 int find(int x) {
    if(x==f[x]) return x;
     int t = f[x];
    f[x]=find(f[x]);
    r[x]=(r[x]+r[t])%3; 
    return f[x];
}
void merge(int x, int y, int d){
    int fx=find(x);
    int fy=find(y);
     f[fy]=fx; 
    r[fy]=(r[x]-r[y]+3+(d-1))%3; 
}
int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    init(n);
     int ans=0;
    int d, x, y;
    while(m--){
        scanf("%d%d%d", &d, &x, &y);
        if(x>n || y>n || (d==2 && x==y)) ans++;
         else if(find(x)==find(y)) {
            if(d==1 && r[x]!=r[y]) ans++; 
            if(d==2 && (r[x]+1)%3!=r[y]) ans++; 
        }
        else merge(x, y, d); 
    }
    printf("%d\n", ans);
    return 0;
}

 

食物链

标签:http   targe   get   style   org   show   col   ||   https   

原文地址:https://www.cnblogs.com/Aze-qwq/p/9337824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!