跳到正文

个人记录

食物链

zyx3939约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define MAXN 150002
#define A (0)
#define B (n)
#define C (n*2)
using namespace std;
int n,k,fa[100010],lies;
int find_set(int x)
{
	if(fa[x]!=x)
 	{
		return fa[x]=find_set(fa[x]);
	}
	return x;
}
void union_set(int x,int y)
{
	fa[find_set(x)]=find_set(y);
}
int main()
{
	cin>>n>>k;
	for(int i=1;i<=3*n;i++)
	{
		fa[i]=i;
	}
	while(k--)
	{
		int op,x,y;
		cin>>op>>x>>y;
		if(x>n||y>n)
		{
			lies++;
			continue;
		}
		if(op==1)
		{
			if(find_set(x+A)==find_set(y+B)||find_set(x+A)==find_set(y+C))
			{
				lies++;
			}
			else
			{
				union_set(x+A,y+A),union_set(x+B,y+B),union_set(x+C,y+C);
			}
		}
		else
		{
			if(find_set(x+A)==find_set(y+A)||find_set(x+A)==find_set(y+C))
			{
				lies++;
			}
			else
			{
				union_set(x+A,y+B),union_set(x+B,y+C),union_set(x+C,y+A);
			}
		}
	}
	cout<<lies;
	return 0;
}
原文