题意:中文题
思路;最详细的题解
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 50000+10
#define LL long long
int cas=1,T;
int pre[maxn];
int r[maxn];
int Find(int x)
{
if (x!=pre[x])
{
int f = pre[x];
pre[x]=Find(pre[x]);
r[x]=(r[x]+r[f])%3;
return pre[x];
}
else
return x;
}
int n;
void init()
{
for (int i = 0;i<=n;i++)
{
pre[i]=i;
r[i]=0;
}
}
int main()
{
//freopen("in","r",stdin);
//scanf("%d",&T);
int k;
scanf("%d%d",&n,&k);
init();
int ans = 0;
while (k--)
{
int d,x,y;
scanf("%d%d%d",&d,&x,&y);
int fx = Find(x);
int fy = Find(y);
if (x>n || y>n)
{
ans++;
continue;
}
if (fx==fy)
{
if (d==1 && r[x]!=r[y])
ans++;
if (d==2 && ((r[x]+1)%3) != r[y])
ans++;
}
else
{
if (d==1)
{
pre[fy]=fx;
r[fy]=(r[x]-r[y]+3)%3;
}
else if (d==2)
{
pre[fy]=fx;
r[fy]=(r[x]-r[y]+1+3)%3;
}
}
}
printf("%d\n",ans);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}