結果
| 問題 | 
                            No.1153 ねこちゃんゲーム
                             | 
                    
| コンテスト | |
| ユーザー | 
                             gew1fw
                         | 
                    
| 提出日時 | 2025-06-12 19:32:50 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,153 bytes | 
| コンパイル時間 | 227 ms | 
| コンパイル使用メモリ | 82,692 KB | 
| 実行使用メモリ | 143,184 KB | 
| 最終ジャッジ日時 | 2025-06-12 19:33:22 | 
| 合計ジャッジ時間 | 26,229 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 6 WA * 29 TLE * 1 -- * 4 | 
ソースコード
import sys
from collections import defaultdict
def main():
    sys.setrecursionlimit(1 << 25)
    N, M = map(int, sys.stdin.readline().split())
    A = list(map(int, sys.stdin.readline().split()))
    edges = [[] for _ in range(N+1)]
    for _ in range(N-1):
        u, v = map(int, sys.stdin.readline().split())
        edges[u].append(v)
        edges[v].append(u)
    
    # Build tree and compute parent and children for each node
    # Let's root the tree at node 1
    root = 1
    parent = [0] * (N+1)
    children = [[] for _ in range(N+1)]
    stack = [(root, 0)]
    while stack:
        u, p = stack.pop()
        parent[u] = p
        for v in edges[u]:
            if v != p:
                children[u].append(v)
                stack.append((v, u))
    
    # Compute Grundy numbers using post-order traversal
    G = [0] * (N+1)
    visited = [False] * (N+1)
    stack = [(root, False)]
    while stack:
        u, processed = stack.pop()
        if processed:
            s = set()
            for v in children[u]:
                s.add(G[v])
            mex = 0
            while mex in s:
                mex += 1
            G[u] = mex
        else:
            stack.append((u, True))
            for v in reversed(children[u]):
                stack.append((v, False))
    
    # Compute XOR of Grundy numbers for all cats
    xor_sum = 0
    for a in A:
        xor_sum ^= G[a]
    
    if xor_sum == 0:
        print("-1 -1")
        return
    
    # Find the optimal move
    for i in range(M):
        a = A[i]
        current_xor = xor_sum ^ G[a]
        # For each child of a, compute new_xor and check if it is zero
        for v in edges[a]:
            if v == parent[a]:
                continue
            s = set()
            for child in children[v]:
                s.add(G[child])
            mex = 0
            while mex in s:
                mex += 1
            new_g = mex
            new_xor = current_xor ^ new_g
            if new_xor == 0:
                print(i+1, v)
                return
    
    # If no move found, which shouldn't happen
    print("-1 -1")
if __name__ == "__main__":
    main()
            
            
            
        
            
gew1fw