一开始默认为0,如果有捕食关系调整d[x]
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
vector<int> vx;
inline void divide() {sort(vx.begin(),vx.end());vx.erase(unique(vx.begin(),vx.end()),vx.end());}
inline int mp(int x) {return upper_bound(vx.begin(),vx.end(),x)-vx.begin();}
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
const int N = 50100;
//d[i]表示i到根节点的异或值是1还是0
int f[N],d[N];
int find(int u)
{
if(f[u] == u) return u;
int t = find(f[u]);
d[u] += d[f[u]], d[u]%=3;
return f[u] = t;
}
void merge(int u,int v,int st)
{
int f1 = find(u), f2 = find(v);
//要实现的效果为(d[f1]+d[u])%3 = (d[v]+st)%3
d[f1] = (d[v]+st-d[u]+3)%3, f[f1] = f2;
}
void solve()
{
int n,k;
cin>>n>>k;
int cnt = 0;
for(int i=1;i<=n;++i) f[i] = i;
for(int i=0;i<k;++i)
{
int st,u,v;
cin>>st>>u>>v;
if(u>n||v>n||u==v&&st==2) cnt++;
else if(find(u) == find(v))
{
if((d[u]-d[v]+3)%3 != st-1)
{
//cout<<u<<' '<<v<<' '<<d[u]<<' '<<d[v]<<'\n';
cnt++;
}
}
else
{
merge(u,v,st-1);
}
}
cout<<cnt<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
//cin>>T;
while(T--)
{
solve();
}
}
标签:食物链,typedef,return,int,查集,st,vx,find
From: https://www.cnblogs.com/ruoye123456/p/18439531