結果

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

ソースコード

diff #

def main():
    import sys
    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 parent and g for each node
    parent = [0] * (n + 1)
    g = [0] * (n + 1)
    visited = [False] * (n + 1)
    stack = [(1, -1, False)]
    
    while stack:
        u, p, visited_flag = stack.pop()
        if visited_flag:
            mex_set = set()
            for v in edges[u]:
                if v == p:
                    continue
                mex_set.add(g[v])
            mex = 0
            while mex in mex_set:
                mex += 1
            g[u] = mex
        else:
            parent[u] = p
            visited[u] = True
            stack.append((u, p, True))
            for v in edges[u]:
                if v != p and not visited[v]:
                    stack.append((v, u, False))
    
    # Calculate total XOR
    total = 0
    for ai in a:
        total ^= g[ai]
    
    if total == 0:
        print("-1 -1")
        return
    
    # Find the first possible move
    for idx in range(m):
        u = a[idx]
        target = g[u] ^ total
        # Check each neighbor, excluding parent
        found = False
        for v in edges[u]:
            if v == parent[u]:
                continue
            if g[v] == target:
                print(idx + 1, v)
                return
    
    # If no move found, which shouldn't happen as per game theory
    print("-1 -1")

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