跳到正文

个人记录

题解 ladice

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

文章信息

发布
2021年11月18日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

T1 ladice

解题思路

我们把一个物品看做 AiA_i 与 BiB_i 之间的连边。

那么如果加入这条边之后联通块中有超过两个环或者两个环就是不合法的,也就是合法的状态只能是一个基环树和树的森林这样一个形态。

直接并茶几判断连通性,标记一下当前的联通块中是否有环就好了。

code

C++
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define f() cout<<"RP++"<<endl
using namespace std;
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}
const int N=3e5+10;
int n,m,fa[N];
bool vis[N];
int find(int x)
{
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}
#undef int
int main()
{
	#define int register long long
	freopen("ladice.in","r",stdin); freopen("ladice.out","w",stdout);
	m=read(); n=read();
	for(int i=1;i<=n;i++) fa[i]=i;
	while(m--)
	{
		int x,y; x=find(read()); y=find(read());
		if(x==y&&vis[x]){printf("SMECE\n");continue;}
		if(x!=y&&vis[x]&&vis[y]){printf("SMECE\n");continue;}
		if(x==y) vis[x]=true;
		else fa[x]=y,vis[y]|=vis[x];
		printf("LADICA\n");
	}
	return 0;
}
原文