UV 12033, c12033
Link to the Q & A 12033-Game of CS
Given a graph, 0 is the root node, each side has a length, two people take turns to operate, each time for an edge color, the length of the previous unit, when the color of an edge is full, it is counted as removing the entire subtree. Determine if the first hand wins.
Solution: SG theorem. For the current node u, the length of the byte vertex v and the u-v edge is l each time.
When l is 1: sg (u) ^ = (sg (v) + 1)
When l is an odd number: It is necessary to judge sg (v) parity, odd-1, even + 1;
When l is an even number: sg (u) ^ = sg (v)
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 1005;int N, W[maxn][maxn];vector<int> g[maxn];int dfs (int u, int p) { int ret = 0; for (int i = 0; i < g[u].size(); i++) { int& v = g[u][i]; if (v != p) { int sg = dfs(v, u); if (W[u][v] == 1) ret ^= (sg+1); else if (W[u][v]&1) ret ^= (sg + (sg&1 ? -1 : 1)); else ret ^= sg; } } return ret;}int main () { int cas, u, v, w; scanf("%d", &cas); for (int k = 1; k <= cas; k++) { scanf("%d", &N); for (int i = 0; i < N; i++) g[i].clear(); for (int i = 1; i < N; i++) { scanf("%d%d%d", &u, &v, &w); g[u].push_back(v); g[v].push_back(u); W[u][v] = W[v][u] = w; } printf("Case %d: %s\n", k, dfs(0, -1) ? "Emily" : "Jolly"); } return 0;}