結果

問題 No.1153 ねこちゃんゲーム
ユーザー gew1fw
提出日時 2025-06-12 19:22:48
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,904 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,352 KB
実行使用メモリ 171,228 KB
最終ジャッジ日時 2025-06-12 19:23:35
合計ジャッジ時間 26,024 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 6 WA * 29 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

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]
    
    # Build adjacency list
    adj = [[] for _ in range(N)]
    for _ in range(N-1):
        u = int(data[idx])-1
        idx +=1
        v = int(data[idx])-1
        idx +=1
        adj[u].append(v)
        adj[v].append(u)
    
    # Compute grundy numbers
    grundy = [0] * N
    parent = [-1] * N
    # Using iterative post-order traversal
    stack = []
    root = 0  # arbitrary choice
    stack.append( (root, -1, False) )
    while stack:
        u, p, processed = stack.pop()
        if not processed:
            stack.append( (u, p, True) )
            parent[u] = p
            for v in adj[u]:
                if v != p:
                    stack.append( (v, u, False) )
        else:
            # Compute children's grundy numbers
            s = set()
            for v in adj[u]:
                if v != p:
                    s.add(grundy[v])
            mex = 0
            while mex in s:
                mex +=1
            grundy[u] = mex
    
    # Compute total XOR
    total_xor = 0
    for a in A:
        total_xor ^= grundy[a]
    
    if total_xor == 0:
        print("-1 -1")
        return
    
    # Find the first possible move
    for i in range(M):
        u = A[i]
        current_g = grundy[u]
        target_g = total_xor ^ current_g
        # Check all adjacent nodes except parent
        for v in adj[u]:
            if v != parent[u]:
                if grundy[v] == target_g:
                    print(i+1, v+1)
                    return
    # If no move found (shouldn't happen)
    print("-1 -1")
    return

if __name__ == "__main__":
    main()
0