answer
Be sure to look at the big brother to write detailed explanations * Ultra-kind disjoint-set
Fa array divided into three parts:
a set of A [1 ~ n], the set of B [n + 1 ~ 2n] , set C [2n + 1 ~ 3n]
Similar operations: the same set of internal consolidation
hostile operations: merge different sets, and there direction, for example, u eat v, is written fa [v] = u, can not write anti
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
//种类并查集
//前提 与前面冲突即为假话
int n,m;
int fa[N*3];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main(){
cin>>n>>m;
for (int i = 1; i <= n*3; ++i) {
fa[i]=i;
}
int cnt=0;//统计错误答案
for (int i = 1,op,u,v; i <= m; ++i) {
cin>>op>>u>>v;
if(u>n||v>n){
cnt++;
continue;
}
if(op==1){//同类
if(find(u+n)==find(v) || find(u)==find(v+n))
//不同集合被合并 说明应该是竞争关系
cnt++;
else{
//同一集合内合并
fa[find(u)]=find(v);
fa[find(u+n)]=find(v+n);
fa[find(u+n+n)]=find(v+n+n);
}
}else{//竞争关系 u吃v == fa[v]=u
if(find(u)==find(v)||find(u)==find(v+n))
//同一集合内被合并 or 竞争关系颠倒
cnt++;
else {
fa[find(u+n)]=find(v);
fa[find(u+n+n)]=find(v+n);
fa[find(u)]=find(v+n+n);
}
}
}
printf("%d", cnt);
return 0;
}