Created
January 2, 2017 12:47
-
-
Save sharmaeklavya2/fb9751ba0d9d5df883d4d29288db8315 to your computer and use it in GitHub Desktop.
Codeforces problem 750F
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Pass the path to a hack file as a command-line argument | |
import sys | |
from collections import defaultdict | |
fobj = open(sys.argv[1]) | |
t = int(fobj.readline()) | |
print(t, flush=True) | |
for ti in range(t): | |
h = int(fobj.readline()) | |
print(h, flush=True) | |
graph = defaultdict(list) | |
for i in range(2**h - 2): | |
u, v = [int(x) for x in fobj.readline().split()] | |
graph[u].append(v) | |
graph[v].append(u) | |
root = None | |
leaves_count = 0 | |
for u, vlist in graph.items(): | |
vlist.reverse() | |
if len(vlist) == 2: | |
if root is None: | |
root = u | |
else: | |
raise Exception("error: Found multiple nodes with degree 2.") | |
elif len(vlist) == 1: | |
leaves_count += 1 | |
elif len(vlist) > 3: | |
raise Exception("error: Found a node with degree > 3.") | |
if root is None: | |
raise Exception("error: Can't find a vertex with degree 2.") | |
if leaves_count != 2**(h-1): | |
raise Exception("error: Inconsistent number of leaves.") | |
moves = 0 | |
while True: | |
words = input().split() | |
if len(words) != 2: | |
print("Verdict: WA (Incorrect command format)", file=sys.stderr, flush=True) | |
break | |
try: | |
cmd, node = words[0], int(words[1]) | |
except ValueError: | |
print("Verdict: WA (Incorrect number format)", file=sys.stderr, flush=True) | |
if cmd == '!': | |
if node != root: | |
print("Verdict: WA (Incorrect root)", file=sys.stderr, flush=True) | |
else: | |
print("Verdict: AC", file=sys.stderr, flush=True) | |
break | |
elif cmd == '?': | |
moves += 1 | |
print(len(graph[node]), flush=True) | |
print(*(graph[node]), flush=True) | |
else: | |
print("Verdict: WA (Incorrect command)", file=sys.stderr, flush=True) | |
print('Moves: {}\n'.format(moves), file=sys.stderr, flush=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment