题解:CF1970C2 Game on Tree (Medium)

· · 题解

题解:CF1970C2 Game on Tree (Medium)

很明显,把起点当做根后。

每一步只能从父亲移动到儿子。

所以令 f_i 代表从 i 点出发,不能走向父亲的所有方案中,先手是否能赢。

当 i 可以走向先手失败的局面,则说明该局面为先手获胜。

也就是若 f_s = 0,则 f_i = 1,其中 s 为 i 的儿子。

#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 2e5 + 5;

int n, m, d[kMaxN];
vector<int> e[kMaxN];
int s;

bool S(int x, int f) {
    if (d[x] == -1) {
        d[x] = 1;
        for (int i : e[x]) {
            (i != f) && (d[x] &= !S(i, x));
        }
    }
    return d[x];
}

int main() {
    cin >> n >> m;
    fill(d + 1, d + n + 1, -1);
    for (int i = 1, x, y; i < n; i++) {
        cin >> x >> y;
        e[x].push_back(y), e[y].push_back(x);
    }
    cin >> s;
    cout << (S(s, 0) ? "Hermione" : "Ron");
    return 0;
}