「Namori Grundy」题解

1.前言

最开始想复杂了,听 lydd 说不算很难后又重新审视了一下自己的思路,确实想复杂了hh。


2.题解

首先,非环上的点的值是能确定的。(我就是这里想复杂了,认为叶子节点可以取任何值)

归纳法。

假设所有的子节点都是确定的,那么这个节点的值一定是子节点值集合的 \(mex\) (\(mex\):最小的不属于集合的自然数)。

归纳到叶子,叶子的值一定为0,是一个确定值。

得证。


现在考虑环上的点。

记环上的点 \(i\) 的在环上的儿子编号为 \(Son_i\)

我们先让环上的点不考虑 \(Son_i\) 。那么现在得到了一个值,记作 \(b_i\)

现在有两种情况

①全部 b 值都相同

那么

如果 \(Son_i\)\(b_i\),则 \(i\) 不为 \(b_i\)
如果 \(Son_i\) 不为 \(b_i\),则 \(i\)\(b_i\)

所以这样的环只有两种状态,任意选一个点 \(s\),枚举这个点是 \(b_i\) 还是不是 \(b_i\)

容易得到:奇环一定无解,偶环一定有解。

②有 b 值不相同

那么一定存在一个点,他的儿子的 \(b\) 值大于他的 \(b\) 值(反证法:不可能出现 \(b_1 < b_2 < b_3 ... < b_n < b_1\)),所以这个点不会受他的儿子的影响(即对于任何点 \(b_i \leq a_i\),因为你给一个点多加了一个儿子,那么他的 \(mex\) 只会增加)。

以这个点 (\(a_i = b_i\)) 跑一遍,判断满不满足要求就行了。

容易得到:这种情况一定有解(当然你也可以像我这个 \(sb\) 一样去跑一遍)。

#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define ULL unsigned long long

template <typename T> void read (T &x) { x = 0; T f = 1;char tem = getchar ();while (tem < '0' || tem > '9') {if (tem == '-') f = -1;tem = getchar ();}while (tem >= '0' && tem <= '9') {x = (x << 1) + (x << 3) + tem - '0';tem = getchar ();}x *= f; return; }
template <typename T> void write (T x) { if (x < 0) {x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0'); }
template <typename T> void print (T x, char ch) { write (x); putchar (ch); }
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }

const int Maxn = 2 * 1e5;

int n;

vector <int> g[Maxn + 5];
void add (int x, int y) {
	g[x].push_back (y);
	
//	cout << x << " " << y << endl;
}

struct UFS {
    int fa[Maxn + 5];

    UFS () {
        for (int i = 1; i <= Maxn; i++)
            fa[i] = i;
    }
    int FindSet (int x) {
        if (fa[x] != x)
            fa[x] = FindSet(fa[x]);
        return fa[x];
    }
    void UnionSet (int x, int y) {
        int u = FindSet (x), v = FindSet (y);
        if (u == v)
            return;
        fa[u] = v;
    }
}st;

deque <int> block;
bool vis[Maxn + 5];
int fa[Maxn + 5];
void Tarjan (int u) {
	vis[u] = 1;
	for (auto v : g[u]) {
		if (vis[v] == 0) {
			fa[v] = u;
			Tarjan (v);
		}
		else if (block.size () == 0) {
			if (u == v) {
				block.push_back (u);
				continue;
			}
			int p = u; fa[v] = u;
			block.push_back (p);
			do {
				p = fa[p];
				block.push_back (p);
			} while (p != v);
		}
	}
}
int dp[Maxn + 5];
bool flag[Maxn + 5];
bool cmp (int x, int y) {
	return dp[x] < dp[y];
}
void Tree (int u) {
	for (auto v : g[u]) {
		if (flag[v]) continue;
		Tree (v);
	}
	sort (g[u].begin (), g[u].end (), cmp);
	int Up = 0;
	for (auto v : g[u]) {
		if (flag[v]) continue;
		if (dp[v] == Up)
			Up++;
		else if (dp[v] > Up)
			break;
	}
	dp[u] = Up;
}
bool solve () {
	Tarjan (st.FindSet (1));
	for (auto u : block)
		flag[u] = 1;
	for (auto u : block)
		Tree (u); 
	
	#define md(x,y) (x < 0 ? x + y : x)
	int s = -1, len = block.size ();
	for (int i = 0; i < len; i++) {
		if (dp[block[md (i - 1, len)]] > dp[block[i]])
			{ s = block[i]; break; }
	}
	if (s == -1)
		return (block.size () & 1) ^ 1; 
	/* 情况2的判法1 
	else
		return 1; 
	*/
//	/* 情况2的判法2
	while (block.front () != s)
		{ block.push_back (block.front ()); block.pop_front (); }
	for (int i = 1; i < len; i++) {
		int u = block[i];
		int Up = 0; 
		sort (g[u].begin (), g[u].end (), cmp);
		for (auto v : g[u]) {
			if (dp[v] == Up)
				Up++;
			else if (dp[v] > Up)
				break;
		}
		dp[u] = Up;
	}
	int Up = 0;
	sort (g[s].begin (), g[s].end (), cmp);
	for (auto v : g[s]) {
		if (dp[v] == Up)
			Up++;
		else if (dp[v] > Up) 
			break;
	}
	return Up == dp[s];
//	*/
}

int main () {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x; cin >> x;
		add (x, i);
		st.UnionSet (i, x); 
	}
	cout << (solve () ? "POSSIBLE" : "IMPOSSIBLE");
	return 0;
}
posted @ 2021-09-21 14:28  C2022lihan  阅读(25)  评论(2编辑  收藏  举报