I competed in this contest during the October holiday. The first five problems felt easy, but I could not solve the sixth and only earned a few points with brute force.
After reading the editorial, I saw the sixth problem’s idea was something I had not considered. I learned from it, though it was not very difficult.
The seventh problem seemed to require a link-cut tree. I have not learned that yet, so I will return to it later.
The first three problems were too straightforward to write about.
4.Bob’s Game
Given an $n * n$ grid with some unreachable cells and several kings, each king may move up, left, or up-left. It stops at the top-left corner. Determine the winner and the number of winning first moves.
Each king is independent, so compute every king’s Sprague–Grundy value and XOR them. Then check moves in $O(3n)$.
voidsolve() { memset(dp, 0, sizeof(dp)); sa(n); repp(i, 1, n) { scanf("%s", val[i] + 1); repp(j, 1, n) { if (i == 1 && j == 1) { dp[i][j] = 0; continue; } set<int>s; repp(k, 1, 3) { int nx = i + dx3[k]; int ny = j + dy3[k]; if (nx<1 || ny<1 || val[nx][ny] == 'X')continue; s.insert(dp[nx][ny]); } dp[i][j] = sg(s); } } int ans = 0; repp(i, 1, n) { repp(j, 1, n) { if (val[i][j] == 'K')ans ^= dp[i][j]; } } if (ans) { printf("WIN "); int res = 0; repp(i, 1, n) { repp(j, 1, n) { if (val[i][j] == 'K') { int tmp = ans; tmp ^= dp[i][j]; repp(k, 1, 3) { int nx = i + dx3[k]; int ny = j + dy3[k]; if (nx<1 || ny<1 || val[nx][ny] == 'X')continue; int t = tmp ^ dp[nx][ny]; if (t == 0)res++; } } } } printf("%d\n", res); } else { puts("LOSE"); } }
5.Black White Tree
Given a black-and-white tree, cut a subtree so that the absolute difference between black and white vertices is maximised. Return that maximum and the possible root choices.
Assign black vertices 1 and white vertices -1; a subtree’s sum is the difference.
dp[x][1] is the largest difference when selecting this vertex; dp[x][0] is the largest difference when not selecting it.
After finding the branch with the largest difference, reroot there, search again, and select greedily.