結果

問題 No.1153 ねこちゃんゲーム
ユーザー 👑 tamatotamato
提出日時 2020-08-07 23:00:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 994 ms / 2,500 ms
コード長 2,469 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 81,872 KB
実行使用メモリ 208,356 KB
最終ジャッジ日時 2023-10-25 04:07:07
合計ジャッジ時間 38,757 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,748 KB
testcase_01 AC 42 ms
55,748 KB
testcase_02 AC 57 ms
66,048 KB
testcase_03 AC 57 ms
66,180 KB
testcase_04 AC 58 ms
66,128 KB
testcase_05 AC 59 ms
65,988 KB
testcase_06 AC 57 ms
66,124 KB
testcase_07 AC 942 ms
200,828 KB
testcase_08 AC 943 ms
182,808 KB
testcase_09 AC 929 ms
200,356 KB
testcase_10 AC 918 ms
182,964 KB
testcase_11 AC 930 ms
200,756 KB
testcase_12 AC 933 ms
199,876 KB
testcase_13 AC 901 ms
183,080 KB
testcase_14 AC 945 ms
201,452 KB
testcase_15 AC 924 ms
182,392 KB
testcase_16 AC 936 ms
202,652 KB
testcase_17 AC 950 ms
201,692 KB
testcase_18 AC 913 ms
184,004 KB
testcase_19 AC 980 ms
198,948 KB
testcase_20 AC 949 ms
184,180 KB
testcase_21 AC 932 ms
199,272 KB
testcase_22 AC 931 ms
198,432 KB
testcase_23 AC 928 ms
181,140 KB
testcase_24 AC 928 ms
199,464 KB
testcase_25 AC 964 ms
183,372 KB
testcase_26 AC 994 ms
200,428 KB
testcase_27 AC 907 ms
207,012 KB
testcase_28 AC 959 ms
188,348 KB
testcase_29 AC 980 ms
206,756 KB
testcase_30 AC 949 ms
188,792 KB
testcase_31 AC 889 ms
208,356 KB
testcase_32 AC 684 ms
195,436 KB
testcase_33 AC 647 ms
178,468 KB
testcase_34 AC 670 ms
195,648 KB
testcase_35 AC 605 ms
176,808 KB
testcase_36 AC 629 ms
194,752 KB
testcase_37 AC 319 ms
171,048 KB
testcase_38 AC 308 ms
173,004 KB
testcase_39 AC 341 ms
202,768 KB
testcase_40 AC 313 ms
174,020 KB
testcase_41 AC 311 ms
177,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import deque
    input = sys.stdin.buffer.readline

    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        adj[a].append(b)
        adj[b].append(a)

    def mex(dic):
        for i in range(N+1):
            if i not in dic:
                return i
            else:
                if dic[i] == 0:
                    return i

    que = deque()
    que.append(1)
    seen = [-1] * (N+1)
    seen[1] = 0
    par = [0] * (N+1)
    child = [[] for _ in range(N+1)]
    seq = []
    while que:
        v = que.popleft()
        seq.append(v)
        for u in adj[v]:
            if seen[u] == -1:
                seen[u] = seen[v] + 1
                par[u] = v
                child[v].append(u)
                que.append(u)
    seq.reverse()

    G = [0] * (N+1)
    child_G = [{} for _ in range(N+1)]
    for v in seq:
        for u in child[v]:
            g = G[u]
            if g in child_G[v]:
                child_G[v][g] += 1
            else:
                child_G[v][g] = 1
        for i in range(N+1):
            if i not in child_G[v]:
                G[v] = i
                break

    #print(G)
    G_ori = G[:]
    GG = G[:]
    seq.reverse()
    for v in seq:
        if v == 1:
            continue
        p = par[v]
        gv = G[v]
        gp = G[p]
        if child_G[p][gv] != 1:
            GG[v] = gp
            if gp in child_G[v]:
                child_G[v][gp] += 1
            else:
                child_G[v][gp] = 1
        else:
            child_G[p][gv] -= 1
            gp_new = mex(child_G[p])
            GG[v] = gp_new
            if gp_new in child_G[v]:
                child_G[v][gp_new] += 1
            else:
                child_G[v][gp_new] = 1
                G[v] = mex(child_G[v])
            child_G[p][gv] += 1

    g = 0
    for a in A:
        g ^= G[a]
    if g == 0:
        print(-1, -1)
    else:
        #print(G)
        #print(g)
        cat = {x: i+1 for i, x in enumerate(A)}
        for v in cat:
            for u in child[v]:
                if G[v] ^ G_ori[u] == g:
                    print(cat[v], u)
                    exit()
            p = par[v]
            if G[v] ^ GG[v] == g:
                print(cat[v], p)
                exit()


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