InternBootcamp/internbootcamp/bootcamp/cgameonleaves/cgameonleaves.py
2025-05-23 15:27:15 +08:00

182 lines
4.8 KiB
Python
Executable file

"""#
### 谜题描述
Ayush and Ashish play a game on an unrooted tree consisting of n nodes numbered 1 to n. Players make the following move in turns:
* Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to 1.
A tree is a connected undirected graph without cycles.
There is a special node numbered x. The player who removes this node wins the game.
Ayush moves first. Determine the winner of the game if each player plays optimally.
Input
The first line of the input contains a single integer t (1 ≤ t ≤ 10) — the number of testcases. The description of the test cases follows.
The first line of each testcase contains two integers n and x (1≤ n ≤ 1000, 1 ≤ x ≤ n) — the number of nodes in the tree and the special node respectively.
Each of the next n-1 lines contain two integers u, v (1 ≤ u, v ≤ n, u ≠ v), meaning that there is an edge between nodes u and v in the tree.
Output
For every test case, if Ayush wins the game, print \"Ayush\", otherwise print \"Ashish\" (without quotes).
Examples
Input
1
3 1
2 1
3 1
Output
Ashish
Input
1
3 2
1 2
1 3
Output
Ayush
Note
For the 1st test case, Ayush can only remove node 2 or 3, after which node 1 becomes a leaf node and Ashish can remove it in his turn.
For the 2nd test case, Ayush can remove node 2 in the first move itself.
Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution.
```python
FAST_IO = 1
if FAST_IO:
import io, sys, atexit
rr = iter(sys.stdin.read().splitlines()).next
sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
def solve(n, x, arr):
num = 0
for a, b in arr:
if x in (a, b):
num+=1
if num<=1:
return False
remain = n - num-1
if (remain +num) % 2==0:
return True
return False
t = rri()
for i in range(t):
n, x = rrm()
arr = []
for _ in range(n-1):
con = rrm()
arr.append(con)
ans = solve(n,x,arr)
if ans:
print(\"Ashish\")
else:
print(\"Ayush\")
```
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
import re
import random
from collections import defaultdict
from bootcamp import Basebootcamp
class Cgameonleavesbootcamp(Basebootcamp):
def __init__(self, min_nodes=1, max_nodes=1000, **kwargs):
super().__init__(**kwargs)
self.min_nodes = min_nodes
self.max_nodes = max_nodes
def case_generator(self):
n = random.randint(self.min_nodes, self.max_nodes)
edges = []
if n > 1:
nodes = set(range(1, n+1))
connected = {random.choice(list(nodes))}
while len(connected) < n:
u = random.choice(list(nodes - connected))
v = random.choice(list(connected))
edges.append((u, v))
connected.add(u)
x = random.randint(1, n)
return {
'n': n,
'x': x,
'edges': edges
}
@staticmethod
def prompt_func(question_case):
n = question_case['n']
x = question_case['x']
edges = question_case['edges']
input_lines = [f"1\n{n} {x}"] + [f"{u} {v}" for u, v in edges]
return (
"Determine the winner of the tree node removal game between Ayush and Ashish.\n\n"
"Game Rules:\n"
"1. Players alternate turns removing leaf nodes (degree ≤ 1)\n"
"2. The player who removes node x wins\n"
"3. Ayush moves first\n\n"
f"Test Case Input (n={n}, x={x}):\n" +
'\n'.join(input_lines) +
"\n\nAnswer format: [answer]Winner[/answer]"
)
@staticmethod
def extract_output(output):
matches = re.findall(r'\[answer\]\s*(Ayush|Ashish)\s*\[/answer\]', output, re.IGNORECASE)
return matches[-1].strip().title() if matches else None
@classmethod
def _verify_correction(cls, solution, identity):
n = identity['n']
x = identity['x']
edges = identity['edges']
# 计算特殊节点x的度数
degree = sum(1 for u, v in edges if u == x or v == x)
# 胜负判定规则
if n == 1:
correct = "Ayush"
elif degree <= 1:
correct = "Ayush" if (n % 2 == 1) else "Ashish"
else:
total_moves = (n - degree - 1) + degree # 等效n-1
correct = "Ashish" if (total_moves % 2 == 0) else "Ayush"
return solution.strip().lower() == correct.lower()