比较明显的SG函数。
设d[x]表示先手轮到x点时的取胜情况。
当子状态有必败状态时,该状态为必胜状态。
当子状态全部为必胜状态,该状态为必败状态。
#include <bits/stdc++.h> #include <unordered_map> using namespace std; typedef long long ll; typedef unsigned long long ull; #ifdef LOCAL #define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n" #define TIME cout << "RuningTime: " << clock() << "ms\n", 0 #else #define TIME 0 #endif #define hash_ 1000000009 #define Continue(x) { x; continue; } #define Break(x) { x; break; } const int mod = 998244353; const int N = 1e4 + 10; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; #define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; } ll fpow(ll a, int b, int mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } vector<int>G[N]; int sg[N]; void dfs(int x, int fa) { int flag = 1; for (auto &i : G[x]) { if (i == fa) continue; dfs(i, x); flag &= sg[i]; } if (!flag) sg[x] = 1; } int main() { #ifdef LOCAL freopen("E:/input.txt", "r", stdin); #endif int kcase = 0; int t; cin >> t; while (t--) { memset(sg, 0, sizeof sg); int n, m; scanf("%d%d", &n, &m); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(m, -1); if (sg[m]) puts("Gen"); else puts("Dui"); for (int i = 1; i <= n; i++) G[i].clear(); } return TIME; }