1、题目:
2、代码:
#include<stdio.h>
#define maxn 50007
int pre[maxn];
int que[maxn];
int m,n,k,i;
int find(int x)//查找
{
int t;
if(x!=pre[x])
{
t=pre[x];
pre[x]=find(pre[x]);
que[x]=(que[x]+que[t])%m;
}
return pre[x];
}
void mix(int a,int b,int d)//合并
{
int fa=find(a);
int fb =find(b);
if(fa!=fb)
{
pre[fa]=fb;
}
que[fa]=(m-que[a]+d+que[b])%m;
}
int main()
{
scanf("%d%d%d",&m,&n,&k);
for(i=1;i<=n;i++)
{
pre[i]=i;
que[i]=0;
}
int d,a,b;
int ans=0;
for(i=1;i<=k;i++)
{
scanf("%d%d%d",&d,&a,&b);
if(a>n||b>n||d>=m)
{
ans++;
continue;
}
int fa=find(a);
int fb=find(b);
if(fa==fb)
{
if((que[a]+m-que[b])%m!=d)
{
ans++;
}
}
else
{
mix(a,b,d);
}
}
printf("%d
",ans);
return 0;
}