Ideas: Write this question https://www.cnblogs.com/2462478392Lee/p/11343747.html it is easy to understand, and came up with the idea. Open a 3 * n array, the x and y kind, predators, and prey do extended domain,
Then using the relationship which do disjoint-set, each case has three. When x and y are the same, the same kind prey, predator predator of x and y, prey and y x. When the prey x y, predator x and y, x of prey
And predator-y y, x of prey. After establishing and check to see how many sets do not meet the current situation and check the set you can find the answer.
#include<cstring> #include<algorithm> #include<vector> #include<map> #include<queue> #include<cstdio> #include<cmath> #define ll long long using namespace std; int fa[2000000]; int get(int k) { return fa[k]==k?k:fa[k]=get(fa[k]); } void merge(int x,int y) { fa[get(x)]=get(y); } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=3*n;i++) { fa[i]=i; } int ans=0; for(int i=1;i<=m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); if(b>n||c>n) ans++; else if(a==1) { if(get(b)==get(c+n)||get(b)==get(c+n+n)) ans++; else { merge(b,c); merge(b+n,c+n); merge(b+n+n,c+n+n); } } else { if(b==c||get(b)==get(c)||get(c+n)==get(b)) ans++; else { merge(b+n,c); merge(b,c+n+n); merge(b+n+n,c+n); } } } printf("%d\n",ans); }