結果

問題 No.1153 ねこちゃんゲーム
ユーザー tamatotamato
提出日時 2020-08-07 23:00:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 874 ms / 2,500 ms
コード長 2,469 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 209,000 KB
最終ジャッジ日時 2024-09-24 23:32:23
合計ジャッジ時間 35,270 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
54,272 KB
testcase_01 AC 44 ms
54,272 KB
testcase_02 AC 62 ms
65,536 KB
testcase_03 AC 62 ms
65,792 KB
testcase_04 AC 61 ms
66,048 KB
testcase_05 AC 65 ms
65,536 KB
testcase_06 AC 61 ms
65,664 KB
testcase_07 AC 866 ms
201,628 KB
testcase_08 AC 779 ms
183,196 KB
testcase_09 AC 812 ms
201,116 KB
testcase_10 AC 770 ms
183,408 KB
testcase_11 AC 818 ms
201,456 KB
testcase_12 AC 813 ms
200,688 KB
testcase_13 AC 744 ms
183,664 KB
testcase_14 AC 787 ms
201,972 KB
testcase_15 AC 778 ms
183,004 KB
testcase_16 AC 804 ms
202,864 KB
testcase_17 AC 818 ms
202,328 KB
testcase_18 AC 783 ms
184,300 KB
testcase_19 AC 839 ms
199,396 KB
testcase_20 AC 792 ms
184,804 KB
testcase_21 AC 813 ms
199,660 KB
testcase_22 AC 814 ms
199,032 KB
testcase_23 AC 796 ms
181,816 KB
testcase_24 AC 807 ms
200,152 KB
testcase_25 AC 846 ms
184,036 KB
testcase_26 AC 874 ms
201,068 KB
testcase_27 AC 772 ms
207,576 KB
testcase_28 AC 826 ms
188,892 KB
testcase_29 AC 871 ms
207,196 KB
testcase_30 AC 814 ms
189,404 KB
testcase_31 AC 769 ms
209,000 KB
testcase_32 AC 575 ms
196,372 KB
testcase_33 AC 549 ms
179,208 KB
testcase_34 AC 576 ms
196,440 KB
testcase_35 AC 519 ms
177,288 KB
testcase_36 AC 553 ms
195,436 KB
testcase_37 AC 289 ms
171,476 KB
testcase_38 AC 292 ms
173,656 KB
testcase_39 AC 326 ms
203,212 KB
testcase_40 AC 295 ms
173,768 KB
testcase_41 AC 293 ms
178,244 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