较为简单的树上博弈论,拓扑排序到x就停下来,其他节点继续拓扑。然后按拓扑下来的节点的奇偶判定答案即可。
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
string solve_game(int n, int x, const vector<pair<int, int>>& edges) {
vector<vector<int>> graph(n + 1);
vector<int> degree(n + 1, 0);
for (const auto& edge : edges) {
int u = edge.first, v = edge.second;
graph[u].push_back(v);
graph[v].push_back(u);
degree[u]++;
degree[v]++;
}
queue<int> q;
vector<bool> removed(n + 1, false);
for (int i = 1; i <= n; ++i) {
if (degree[i] == 1) {
q.push(i);
}
}
if (degree[x] == 1) return "Xiaoyo";
int cnt = 0;
int ok = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
removed[node] = true;
if (node == x){
ok = 1;
continue;
}
cnt ++;
for (int neighbor : graph[node]) {
if (!removed[neighbor]) {
degree[neighbor]--;
degree[node] --;
if (degree[neighbor] == 1) {
q.push(neighbor);
}
}
}
if (!removed[node] && degree[node] == 1) q.push(node);
}
if (ok){
if (cnt & 1) return "Pyrmont";
else return "Xiaoyo";
}
return "Draw";
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
vector<string> results;
while (T--) {
int n, x;
cin >> n >> x;
vector<pair<int, int>> edges(n);
for (int i = 0; i < n; ++i) {
int u, v;
cin >> u >> v;
edges[i] = {u, v};
}
results.push_back(solve_game(n, x, edges));
}
for (const string& result : results) {
cout << result << endl;
}
return 0;
}