Codeforces Round #646 (Div. 2) C. Game On Leaves(树上博弈)

题目链接:https://codeforces.com/contest/1363/problem/C

题意

有一棵 $n$ 个结点的树,每次只能取叶子结点,判断谁能最先取到结点 $x$ 。

题解

除非结点 $x$ 一开始就为叶子结点,否则二人一定会取到只剩 $3$ 个结点,且中间结点为 $x$ 的情况。

所以,判断结点 $x$ 是否为叶子结点,否则再判断 $n - 3$ 的奇偶性即可。

代码

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n, x; cin >> n >> x;
    int deg_x = 0;    
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        deg_x += (u == x) or (v == x);
    }
    if (deg_x <= 1)
        cout << "Ayush" << "\n";
    else
        cout << ((n - 3) & 1 ? "Ayush" : "Ashish") << "\n";
}

int main() {
    int t; cin >> t;
    while (t--) solve();
}

 

posted @ 2020-06-01 10:45  Kanoon  阅读(190)  评论(0编辑  收藏  举报