to sum up
Note that negative treatment
note multiple sets of test cases that he was not noticed, HDU actually gave me shows WA, rather than TLE
other is processing the leaf node to the root of the distance
const int N=5e4+5;
int fat[N],dis[N];
int find(int x)
{
if(fat[x]==x)
return x;
int root=find(fat[x]);
dis[x]+=dis[fat[x]];
return fat[x]=root;
}
int mod(int x)
{
return (x%300+300)%300;
}
signed main()
{
IOS;
//file();
int n,m;
while(cin>>n>>m)
{
for(int i=1; i<=n; i++)
fat[i]=i,dis[i]=0;
int ans=0;
while(m--)
{
int a,b,x;
cin>>a>>b>>x;
int ta=find(a);
int tb=find(b);
if(ta==tb)
{
int num=mod(dis[b]-dis[a]);
if(num!=x%300)
ans++;
}
else
{
fat[tb]=ta;
dis[tb]=dis[a]+x-dis[b];
}
}
cout<<ans<<endl;
}
return 0;
}