• hdu 3038带权并查集


    #include<stdio.h>
    #include<string.h>
    #define N  200100
    struct node {
    int x,count;
    }pre[N];
    int find(int n) {
    if(n!=pre[n].x) {
    int h=pre[n].x;
    pre[n].x=find(pre[n].x);
    pre[n].count=pre[n].count+pre[h].count;
    }
    return pre[n].x;
    }
    int Union(int x,int y,int d) {
          int a=find(x);
     int b=find(y);
     if(a==b) {
     if(pre[x].count-pre[y].count==d)
     return 1;
     return 0;
     }
     else {
     pre[a].x=b;
     pre[a].count=pre[y].count-pre[x].count+d;
     return 1;
     }
    }
    int main() {
    int n,m,a,b,c,count,sum,flag,i;
    while(scanf("%d%d",&n,&m)!=EOF) {
    for(i=0;i<=n;i++) {
    pre[i].x=i;
    pre[i].count=0;
    }
    count=0;
    while(m--) {
    scanf("%d%d%d",&a,&b,&c);
    a--;
    if(Union(a,b,c)==0)
    count++;
    }
    printf("%d ",count);
    }
    return 0;
    }

  • 相关阅读:
    radioButton
    事件分发
    自定义属性
    自定义View
    拦截来电
    Messenger信使
    使用AIDL远程调用服务中的方法
    利用接口调用服务中特定的方法
    Service服务
    BroadcastReceiver广播接受者
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410886.html
Copyright © 2020-2023  润新知