From de4085def0dcb34d124278b743fb2f2e478f76c1 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Mon, 4 Nov 2024 19:43:07 +0530 Subject: [PATCH 1/5] py sol for Game on Tree --- solutions/advanced/ac-GameOnTree.mdx | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/solutions/advanced/ac-GameOnTree.mdx b/solutions/advanced/ac-GameOnTree.mdx index 0077393b81..70b03e7f70 100644 --- a/solutions/advanced/ac-GameOnTree.mdx +++ b/solutions/advanced/ac-GameOnTree.mdx @@ -71,4 +71,34 @@ int main() { ``` + + +```py +import sys, pypyjit + +# improves performance for deeply recursive functions by allowing PyPy to skip unrolling +pypyjit.set_param('max_unroll_recursion=-1') + +sys.setrecursionlimit(10**7) + +def dfs(u: int, p: int) -> int: + sg = 0 + for v in g[u]: + if v != p: + sg ^= dfs(v, u) + 1 + + return sg + +n = int(input()) +g = [[] for _ in range(n + 1)] + +for _ in range(n - 1): + u, v = map(int, input().split()) + g[u].append(v) + g[v].append(u) + +print("Alice" if dfs(1, 0) else "Bob") +``` + + From 4b299358bdd939746b3c26ed4422ef5bbfb8c41f Mon Sep 17 00:00:00 2001 From: freakin23 Date: Mon, 4 Nov 2024 19:44:36 +0530 Subject: [PATCH 2/5] add comments --- solutions/advanced/ac-GameOnTree.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solutions/advanced/ac-GameOnTree.mdx b/solutions/advanced/ac-GameOnTree.mdx index 70b03e7f70..df77552a70 100644 --- a/solutions/advanced/ac-GameOnTree.mdx +++ b/solutions/advanced/ac-GameOnTree.mdx @@ -85,6 +85,8 @@ def dfs(u: int, p: int) -> int: sg = 0 for v in g[u]: if v != p: + # Adding 1 because the edge from u to v increases the size of + # each stack by 1 sg ^= dfs(v, u) + 1 return sg From 438072790492c660b102b305152d9ff6b35aed2f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:20:43 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/advanced/ac-GameOnTree.mdx | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/solutions/advanced/ac-GameOnTree.mdx b/solutions/advanced/ac-GameOnTree.mdx index df77552a70..3f74ccf719 100644 --- a/solutions/advanced/ac-GameOnTree.mdx +++ b/solutions/advanced/ac-GameOnTree.mdx @@ -77,27 +77,29 @@ int main() { import sys, pypyjit # improves performance for deeply recursive functions by allowing PyPy to skip unrolling -pypyjit.set_param('max_unroll_recursion=-1') +pypyjit.set_param("max_unroll_recursion=-1") sys.setrecursionlimit(10**7) + def dfs(u: int, p: int) -> int: - sg = 0 - for v in g[u]: - if v != p: + sg = 0 + for v in g[u]: + if v != p: # Adding 1 because the edge from u to v increases the size of # each stack by 1 - sg ^= dfs(v, u) + 1 + sg ^= dfs(v, u) + 1 + + return sg - return sg n = int(input()) g = [[] for _ in range(n + 1)] for _ in range(n - 1): - u, v = map(int, input().split()) - g[u].append(v) - g[v].append(u) + u, v = map(int, input().split()) + g[u].append(v) + g[v].append(u) print("Alice" if dfs(1, 0) else "Bob") ``` From a9a47de5223dac42cf8ed96623c30b196cadae3e Mon Sep 17 00:00:00 2001 From: freakin23 Date: Mon, 4 Nov 2024 20:37:56 +0530 Subject: [PATCH 4/5] added warning --- solutions/advanced/ac-GameOnTree.mdx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/solutions/advanced/ac-GameOnTree.mdx b/solutions/advanced/ac-GameOnTree.mdx index 3f74ccf719..94bc30f714 100644 --- a/solutions/advanced/ac-GameOnTree.mdx +++ b/solutions/advanced/ac-GameOnTree.mdx @@ -73,6 +73,10 @@ int main() { + +Submit the solution in PyPy to avoid MLE. + + ```py import sys, pypyjit From 074fedb76fc0172665c2782e8f71403f9cf3a5b6 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Mon, 4 Nov 2024 12:55:23 -0800 Subject: [PATCH 5/5] Update ac-GameOnTree.mdx --- solutions/advanced/ac-GameOnTree.mdx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/solutions/advanced/ac-GameOnTree.mdx b/solutions/advanced/ac-GameOnTree.mdx index 94bc30f714..9603884821 100644 --- a/solutions/advanced/ac-GameOnTree.mdx +++ b/solutions/advanced/ac-GameOnTree.mdx @@ -7,6 +7,8 @@ author: Aakash Gokhale [Official Editorial (Japanese)](https://atcoder.jp/contests/agc017/editorial) +## Explanation + Since the game in the problem seems similar to Nim, let's try to use Sprague-Grundy numbers. To solve the problem, for a certain root with subtrees, $s_1, s_2, ..., s_k$, we can try to convert all these subtrees into a stack of some size. @@ -39,6 +41,7 @@ This makes sense because whichever edge that Alice chooses, Bob can choose the o #include #include #include + using namespace std; int main() { @@ -74,7 +77,10 @@ int main() { -Submit the solution in PyPy to avoid MLE. + +The below solution only works when you submit with PyPy. +Submitting with normal Python will result in MLE. + ```py