您的位置:首页 > 其它

AtCoder Regular Contest 079 F - Namori Grundy 乱搞

2018-02-20 14:44 489 查看

题意

给出一棵外向环套树,问能否给每一个点定一个权值,使得每个点的权值都满足其恰好是该点所有后继节点的mex。

n<=200000

分析

对于不在环上的节点,它的权值是唯一确定的。对于环上的任意一个点,它的权值有两种可能,一种是它的所有非环上后继的mex,另一种是第二大的未出现过的数。那么只要枚举两种情况,看是否合法即可。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=200005;

int n,cnt,last
,p
,sg
,p1,p2,tim,arr
;
bool vis
;
struct edge{int to,next;bool del;}e
;

int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}

void addedge(int u,int v)
{
e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;
}

int get_mex(int x)
{
int ans;
for (int i=last[x];i;i=e[i].next) if (!e[i].del) vis[sg[e[i].to]]=1;
for (int i=0;;i++) if (!vis[i]) {ans=i;break;}
for (int i=last[x];i;i=e[i].next) if (!e[i].del) vis[sg[e[i].to]]=0;
return ans;
}

void dfs(int x)
{
arr[x]=tim;
for (int i=last[x];i;i=e[i].next)
if (!arr[e[i].to]) dfs(e[i].to);
else if (arr[e[i].to]==tim) p1=x,p2=e[i].to,e[i].del=1;
sg[x]=get_mex(x);
}

int main()
{
n=read();
for (int i=1;i<=n;i++) p[i]=read(),addedge(p[i],i);
for (int i=1;i<=n;i++) if (!arr[i]) tim++,dfs(i);
int s1=get_mex(p1);
vis[s1]=1;
int s2=get_mex(p1);
vis[s1]=0;
if (sg[p2]!=s1) {puts("POSSIBLE");return 0;}
sg[p1]=s2;
int x=p1;
while (x!=p2) x=p[x],sg[x]=get_mex(x);
if (sg[p2]==s1) puts("POSSIBLE");
else puts("IMPOSSIBLE");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: