結果

問題 No.2841 Leaf Eater
ユーザー とりゐとりゐ
提出日時 2024-07-17 17:16:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 417 ms / 2,000 ms
コード長 1,977 bytes
コンパイル時間 352 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 111,616 KB
最終ジャッジ日時 2024-07-17 17:46:23
合計ジャッジ時間 5,832 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
54,784 KB
testcase_01 AC 113 ms
77,388 KB
testcase_02 AC 108 ms
77,148 KB
testcase_03 AC 110 ms
77,440 KB
testcase_04 AC 114 ms
77,444 KB
testcase_05 AC 114 ms
77,184 KB
testcase_06 AC 417 ms
78,080 KB
testcase_07 AC 357 ms
79,180 KB
testcase_08 AC 327 ms
84,516 KB
testcase_09 AC 107 ms
111,104 KB
testcase_10 AC 160 ms
111,616 KB
testcase_11 AC 158 ms
111,412 KB
testcase_12 AC 166 ms
111,360 KB
testcase_13 AC 117 ms
84,896 KB
testcase_14 AC 126 ms
85,888 KB
testcase_15 AC 111 ms
85,696 KB
testcase_16 AC 127 ms
92,800 KB
testcase_17 AC 120 ms
90,644 KB
testcase_18 AC 112 ms
82,048 KB
testcase_19 AC 232 ms
78,568 KB
testcase_20 AC 233 ms
79,140 KB
testcase_21 AC 250 ms
78,464 KB
testcase_22 AC 212 ms
78,764 KB
testcase_23 AC 265 ms
79,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def gen(n):
    cand = [[-1 for i in range(n)]]
    for i in range(1, n):
        ncand = []
        for p in cand:
            for j in range(i):
                q = p.copy()
                q[i] = j
                ncand.append(q)
        cand = ncand
    return cand


import random

hash = {}


def tree_hash(p):
    n = len(p)
    H = [0] * n
    for i in range(n - 1, 0, -1):
        if H[i] not in hash:
            hash[H[i]] = random.randint(1, (1 << 40) - 1)
        H[i] = hash[H[i]]
        if i != 0:
            H[p[i]] += H[i]
    return H[0]


memo = {}


def naive(p):
    if tuple(p) in memo:
        return memo[tuple(p)]
    n = len(p)
    leaf = [1] * n
    for i in range(n):
        if p[i] == -1:
            leaf[i] = 0
        else:
            leaf[p[i]] = 0
    leaf_lst = []
    for i in range(n):
        if leaf[i]:
            leaf_lst.append(i)

    m = len(leaf_lst)
    mex = set()
    for bit in range(1, 1 << m):
        q = p.copy()
        for i in range(m):
            if (bit >> i) & 1:
                q[leaf_lst[i]] = -1
        mex.add(naive(q))
    g = 0
    while g in mex:
        g += 1
    memo[tuple(p)] = g
    return g


def solve(p):
    n = len(p)
    child = [[] for i in range(n)]
    for v in range(1, n):
        child[p[v]].append(v)
    d = [0] * n
    for v in range(n):
        if len(child[v]) >= 2:
            d[v] = 0
        for u in child[v]:
            d[u] = d[v] ^ 1

    for v in range(n):
        if len(child[v]) == 0 and d[v] == 1:
            return True
    return False


S = set()
if False:
    for sz in range(1, 11):
        print(sz)
        memo.clear()
        S.clear()
        for p in gen(sz):
            h = tree_hash(p)
            if h not in S:
                S.add(h)
                assert (naive(p) > 0) == solve(p)

T = int(input())
for _ in range(T):
    n = int(input())
    p = [-1] + list(map(lambda x: int(x) - 1, input().split()))
    print(["Second", "First"][solve(p)])
0