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; }
相关文章推荐
- 【构造】AtCoder Regular Contest 079 F - Namori Grundy
- AtCoder Regular Contest 079
- 【构造】AtCoder Regular Contest 079 D - Decrease (Contestant ver.)
- AtCoder Regular Contest 079
- 【贪心】AtCoder Regular Contest 079 E - Decrease (Judge ver.)
- Atcoder AtCoder Regular Contest 079 E - Decrease (Judge ver.)
- AtCoder Regular Contest 076 E - Connected? 乱搞
- AtCoder Regular Contest 079-C - Cat Snuke and a Voyage
- AtCoder Regular Contest 079-D - Decrease (Contestant ver.)
- AtCoder Regular Contest 079 D - Decrease (Contestant ver.)
- AtCoder Regular Contest 064 F - Rotated Palindromes 乱搞
- AtCoder Regular Contest 079 C D E
- AtCoder Regular Contest 079-E - Decrease (Judge ver.)
- AtCoder Regular Contest 077 被虐记&题解
- AtCoder Regular Contest 078
- AtCoder Regular Contest 085 C题题解
- AtCoder Regular Contest 088 E - Papple Sort(树状数组+结论)
- AtCoder Regular Contest 090 D People on a Line【dfs + bfs】
- AtCoder Regular Contest 067 F - Yakiniku Restaurants 乱搞
- AtCoder Regular Contest 065 Shuffling 动态规划