题解 ladice
T1 ladice
解题思路
我们把一个物品看做
那么如果加入这条边之后联通块中有超过两个环或者两个环就是不合法的,也就是合法的状态只能是一个基环树和树的森林这样一个形态。
直接并茶几判断连通性,标记一下当前的联通块中是否有环就好了。
code
#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;
}