結果
問題 | No.1153 ねこちゃんゲーム |
ユーザー |
![]() |
提出日時 | 2025-06-12 14:22:29 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,846 bytes |
コンパイル時間 | 251 ms |
コンパイル使用メモリ | 82,192 KB |
実行使用メモリ | 177,360 KB |
最終ジャッジ日時 | 2025-06-12 14:22:57 |
合計ジャッジ時間 | 27,322 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 6 WA * 29 TLE * 1 -- * 4 |
ソースコード
import sys from sys import stdin sys.setrecursionlimit(1 << 25) def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 M = int(data[idx]) idx += 1 A = list(map(int, data[idx:idx+M])) idx += M A = [a-1 for a in A] # Read edges edges = [[] for _ in range(N)] for _ in range(N-1): u = int(data[idx])-1 idx += 1 v = int(data[idx])-1 idx += 1 edges[u].append(v) edges[v].append(u) # Compute Grundy numbers. Root the tree at 0 (arbitrary) from collections import deque parent = [-1] * N children = [[] for _ in range(N)] visited = [False] * N q = deque() q.append(0) visited[0] = True while q: u = q.popleft() for v in edges[u]: if not visited[v]: visited[v] = True parent[v] = u children[u].append(v) q.append(v) # Compute Grundy numbers using post-order traversal grundy = [0] * N stack = [(0, False)] while stack: u, processed = stack.pop() if processed: mex = set() for v in children[u]: mex.add(grundy[v]) mex_val = 0 while mex_val in mex: mex_val += 1 grundy[u] = mex_val else: stack.append((u, True)) for v in reversed(children[u]): stack.append((v, False)) # Compute XOR of all cats' grundy numbers xor = 0 for a in A: xor ^= grundy[a] if xor == 0: print("-1 -1") else: # Find the best move for Ebi-chan found = False best_move = (-1, -1) for i in range(M): v = A[i] for u in edges[v]: if u == parent[v]: continue # moving back to parent may not be optimal, but need to consider all new_xor = xor ^ grundy[v] ^ grundy[u] if new_xor == 0: found = True best_move = (i+1, u+1) break if found: break # If not found in children, check parent if not found: for i in range(M): v = A[i] if parent[v] != -1: u = parent[v] new_xor = xor ^ grundy[v] ^ grundy[u] if new_xor == 0: found = True best_move = (i+1, u+1) break if found: print(f"{best_move[0]} {best_move[1]}") else: # If no move found (unlikely as xor is non-zero) print("1 -1") if __name__ == '__main__': main()