E - 食物链

时间:2023-12-16 19:02:50

题目链接:https://cn.vjudge.net/contest/66964#problem/E

关系式:

ra->rb=(ra->b + b->rb )%3;

ra->b=(a->b - a->ra + b )%3;

ra->rb=(a->b + b->rb -a->ra+3)%3;

a->rb=a->b+b->rb;

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<stdio.h>
using namespace std;
# define maxn 50000+10
# define inf 0x3f3f3f3f
# define ll long long
int n,m;
int father[maxn];
int r[maxn];
int Find(int t)
{
int temp=father[t];
if(father[t]!=t)
{
father[t]=Find(father[t]);
r[t]=(r[t]+r[temp])%3;//向量
}
return father[t];
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
father[i]=i;
r[i]=0;
}
int ans=0;
while(m--)
{
int d,t1,t2;
scanf("%d%d%d",&d,&t1,&t2);
int w1=Find(t1);
int w2=Find(t2);
if((t1==t2&&d==2)||t1>n||t2>n)ans++;
else if(w1==w2&&(d-1+r[t2])%3!=r[t1])ans++;
else if(w1!=w2)
{
father[w1]=w2;
r[w1]=(r[t2]+d-1-r[t1]+3)%3;
}
}
printf("%d\n",ans);
return 0;
}