题目链接

拓展域并查集:
拆点拆成3种,A,B, C
判断是否满足用==,不可以用!=间接判断

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define rep(i,a,b) for(int i = a;i <= b;i++)
using namespace std;
const int N = 15*1e4+100;
 
int fa[N],n,k;
 
int get(int x)
{
	if(x == fa[x]) return x;
	return fa[x] = get(fa[x]);
}
 
int main()
{
	scanf("%d%d",&n,&k);
	rep(i,1,3*n) fa[i] = i;
	int cnt = 0;
	rep(i,1,k)
	{
		int x,y,z;
		scanf("%d%d%d",&z,&x,&y);
		int fx1 = get(x), fx2 = get(x+n), fx3 = get(x+2*n);
		int fy1 = get(y), fy2 = get(y+n), fy3 = get(y+2*n);
		if(y > n || x > n){cnt++; continue;}
		if(z == 1)
		// 不是同类的6种情况
		{	
			if(fx1 == fy2 || fx1 == fy3 || fx2 == fy1 || fx2 == fy3 || fx3 == fy1 || fx3 == fy2)
			{
				cnt++;
				continue;
			}
			fa[fx1] = fy1, fa[fx2] = fy2, fa[fx3] = fy3;
		}
		else{
		//不满足a--》b的7种情况
			if(y == x || fx1 == fy1 || fx1 == fy3 || fx2== fy1 || fx2 == fy2 || fx3== fy2 || fx3==sy3) 
			{
				cnt++;
				continue;
			}
			fa[fx1] = fy2, fa[fx2] = fy3, fa[fx3] = fy1;
		}
	}
	printf("%d\n",cnt);
	return 0;
}
 posted on 2019-08-25 11:33  谁是凶手1703  阅读(56)  评论(0)    收藏  举报