64,648
社区成员
发帖
与我相关
我的任务
分享
答案源代码 就是不知道其思想 希望高人解读
#include <iostream>
#include <cstdio>
using namespace std;
int set[50010],h[50010];
int find(int x)
{
if( set[x]==-1 ) return x;
int t=find(set[x]);
h[x]=(h[x]+h[set[x]])%3;
set[x]=t;
return set[x];
}
int comb(int x,int y,int z)
{
int a,b;
a=find(x);
b=find(y);
if(a==b) return (h[x]+z)%3!=h[y];
set[a]=b;
h[a]=(6+h[y]-z-h[x])%3;
return 0;
}
int main()
{
int i,n,k,x,y,ans,d;
while(scanf("%d%d",&n,&k)!=EOF)
{
ans=0;
for( i=1; i<=n; i++)
{
set[i]=-1;
h[i]=0;
}
while( k-- )
{
scanf("%d%d%d",&d,&x,&y);
if( x>n||y>n )
{
ans++;
continue;
}
if( comb(x,y,d-1) ) ans++;
}
printf("%d\n",ans);
}
return 0;
}