結果

問題 No.1153 ねこちゃんゲーム
ユーザー Kiri8128Kiri8128
提出日時 2020-06-29 02:50:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 949 ms / 2,500 ms
コード長 1,390 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 144,152 KB
最終ジャッジ日時 2023-10-12 09:53:41
合計ジャッジ時間 45,480 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,732 KB
testcase_01 AC 97 ms
71,648 KB
testcase_02 AC 105 ms
72,268 KB
testcase_03 AC 105 ms
72,072 KB
testcase_04 AC 106 ms
72,064 KB
testcase_05 AC 105 ms
72,384 KB
testcase_06 AC 106 ms
72,068 KB
testcase_07 AC 863 ms
137,040 KB
testcase_08 AC 854 ms
135,584 KB
testcase_09 AC 891 ms
135,916 KB
testcase_10 AC 853 ms
136,552 KB
testcase_11 AC 875 ms
139,492 KB
testcase_12 AC 870 ms
135,244 KB
testcase_13 AC 858 ms
136,900 KB
testcase_14 AC 889 ms
136,456 KB
testcase_15 AC 840 ms
136,384 KB
testcase_16 AC 873 ms
135,204 KB
testcase_17 AC 885 ms
136,436 KB
testcase_18 AC 883 ms
139,220 KB
testcase_19 AC 885 ms
135,068 KB
testcase_20 AC 904 ms
136,788 KB
testcase_21 AC 949 ms
140,708 KB
testcase_22 AC 868 ms
135,540 KB
testcase_23 AC 869 ms
133,980 KB
testcase_24 AC 913 ms
139,348 KB
testcase_25 AC 920 ms
139,816 KB
testcase_26 AC 896 ms
135,456 KB
testcase_27 AC 818 ms
136,128 KB
testcase_28 AC 837 ms
135,200 KB
testcase_29 AC 844 ms
136,384 KB
testcase_30 AC 866 ms
137,848 KB
testcase_31 AC 809 ms
137,952 KB
testcase_32 AC 720 ms
142,716 KB
testcase_33 AC 761 ms
143,508 KB
testcase_34 AC 728 ms
143,620 KB
testcase_35 AC 645 ms
144,152 KB
testcase_36 AC 637 ms
143,084 KB
testcase_37 AC 336 ms
137,148 KB
testcase_38 AC 310 ms
132,584 KB
testcase_39 AC 332 ms
138,484 KB
testcase_40 AC 328 ms
136,056 KB
testcase_41 AC 300 ms
133,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = lambda: sys.stdin.readline().rstrip()
N, M = map(int, input().split())
A = [int(a) - 1 for a in input().split()]
X = [[] for i in range(N)]
for _ in range(N - 1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)
P = [-1] * N
Q = deque([0])
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            X[a].remove(i)
            deque.append(Q, a)

G = [0] * N # Grundy Number
L = [[] for _ in range(N)]
for i in R[::-1]:
    m = min(len(X[i]) + 2, 20)
    L[i] = [0] * m
    for j in X[i]:
        if G[j] < m:
            L[i][G[j]] += 1
    for k in range(m):
        if not L[i][k]:
            G[i] = k
            break
G_bu = G[:]
for i in R[1:]:
    m = min(len(X[i]) + 2, 20)
    g = G[i]
    p = P[i]
    pg = G[p]
    if g < pg and L[p][g] == 1:
        pg = g
    if pg < m:
        L[i][pg] += 1
    if pg == g:
        for k in range(g, m):
            if not L[i][k]:
                G[i] = k
                break

s = 0
for a in A:
    s ^= G[a]

if s == 0:
    print(-1, -1)
else:
    for i, a in enumerate(A):
        if G[a] ^ s < G[a]:
            for b in X[a]:
                if G[a] ^ s == G_bu[b]:
                    print(i+1, b+1)
                    exit()
            print(i+1, P[a] + 1)
            exit()
0