hdu3038 题解

链接:hdu3038

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,x,y) for(register int i=x;i<=y;++i)
#define repd(i,x,y) for(regsiter int i=x;i>=y;--i)
#define ll long long
using namespace std;
const int N=1e6+7;
int f[N],rl[N],n,m,t,a,b,ans;

int (int x){
if(f[x]==x)return x;
int t=find(f[x]);
rl[x]+=rl[f[x]];
return f[x]=t;
}

inline void un(int x,int y){
int a=find(x);
int b=find(y);
if(a==b&&rl[y]-rl[x]!=t){ans++; return ;}
f[b]=a;
rl[b]=rl[x]-rl[y]+t;
}

int main(){
while(~scanf("%d%d",&n,&m)){
rep(i,1,n)f[i]=i;
rep(i,1,m){
scanf("%d%d%d",&a,&b,&t);
un(--a,b);
}
printf("%d",ans);
}
return 0;
}