結果
問題 | 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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 23 |
ソースコード
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)])