結果

問題 No.1153 ねこちゃんゲーム
ユーザー gew1fw
提出日時 2025-06-12 14:24:31
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,471 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 121,328 KB
最終ジャッジ日時 2025-06-12 14:25:03
合計ジャッジ時間 23,639 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 7 WA * 28 TLE * 1 -- * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

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)
    
    # Compute Grundy numbers
    G = [0] * (N + 1)
    in_degree = [0] * (N + 1)
    for u in range(1, N+1):
        in_degree[u] = len(edges[u])
    
    # Initialize queue with leaves
    q = deque()
    for u in range(1, N+1):
        if in_degree[u] == 1:
            q.append(u)
    
    # Process nodes using BFS
    while q:
        u = q.popleft()
        s = set()
        for v in edges[u]:
            s.add(G[v])
        mex = 0
        while mex in s:
            mex += 1
        G[u] = mex
        for v in edges[u]:
            in_degree[v] -= 1
            if in_degree[v] == 1:
                q.append(v)
    
    # Compute XOR of all cats' starting positions
    xor = 0
    for a in A:
        xor ^= G[a]
    
    if xor == 0:
        print("-1 -1")
        return
    
    # Find a move to make the XOR zero
    for i in range(M):
        u = A[i]
        for v in edges[u]:
            if (xor ^ G[u] ^ G[v]) == 0:
                print(i + 1, v)
                return
    
    # If no such move found (should not happen)
    print("-1 -1")

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