結果
問題 | No.1295 木と駒 |
ユーザー | Kiri8128 |
提出日時 | 2020-11-23 16:23:53 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 417 ms / 2,000 ms |
コード長 | 2,398 bytes |
コンパイル時間 | 416 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 114,760 KB |
最終ジャッジ日時 | 2024-07-23 17:34:15 |
合計ジャッジ時間 | 13,983 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
54,400 KB |
testcase_01 | AC | 42 ms
54,400 KB |
testcase_02 | AC | 46 ms
60,928 KB |
testcase_03 | AC | 43 ms
54,016 KB |
testcase_04 | AC | 42 ms
54,272 KB |
testcase_05 | AC | 41 ms
54,528 KB |
testcase_06 | AC | 42 ms
54,656 KB |
testcase_07 | AC | 42 ms
54,016 KB |
testcase_08 | AC | 42 ms
54,144 KB |
testcase_09 | AC | 42 ms
54,272 KB |
testcase_10 | AC | 43 ms
54,144 KB |
testcase_11 | AC | 43 ms
54,784 KB |
testcase_12 | AC | 43 ms
54,144 KB |
testcase_13 | AC | 43 ms
55,040 KB |
testcase_14 | AC | 322 ms
105,676 KB |
testcase_15 | AC | 326 ms
105,188 KB |
testcase_16 | AC | 332 ms
105,548 KB |
testcase_17 | AC | 328 ms
105,568 KB |
testcase_18 | AC | 335 ms
105,440 KB |
testcase_19 | AC | 268 ms
108,484 KB |
testcase_20 | AC | 269 ms
105,684 KB |
testcase_21 | AC | 301 ms
106,484 KB |
testcase_22 | AC | 297 ms
108,904 KB |
testcase_23 | AC | 325 ms
105,408 KB |
testcase_24 | AC | 312 ms
105,256 KB |
testcase_25 | AC | 295 ms
106,308 KB |
testcase_26 | AC | 196 ms
104,228 KB |
testcase_27 | AC | 205 ms
104,272 KB |
testcase_28 | AC | 368 ms
105,304 KB |
testcase_29 | AC | 208 ms
104,020 KB |
testcase_30 | AC | 210 ms
104,148 KB |
testcase_31 | AC | 209 ms
104,144 KB |
testcase_32 | AC | 328 ms
104,724 KB |
testcase_33 | AC | 319 ms
105,264 KB |
testcase_34 | AC | 417 ms
105,164 KB |
testcase_35 | AC | 309 ms
105,396 KB |
testcase_36 | AC | 302 ms
105,008 KB |
testcase_37 | AC | 312 ms
106,152 KB |
testcase_38 | AC | 341 ms
106,596 KB |
testcase_39 | AC | 290 ms
104,628 KB |
testcase_40 | AC | 300 ms
104,748 KB |
testcase_41 | AC | 262 ms
106,688 KB |
testcase_42 | AC | 262 ms
114,760 KB |
testcase_43 | AC | 274 ms
109,508 KB |
testcase_44 | AC | 272 ms
114,512 KB |
testcase_45 | AC | 262 ms
105,808 KB |
testcase_46 | AC | 274 ms
107,328 KB |
testcase_47 | AC | 277 ms
105,672 KB |
testcase_48 | AC | 276 ms
105,680 KB |
ソースコード
import sys input = sys.stdin.readline from collections import deque N = int(input()) X = [[] for i in range(N)] for i in range(N-1): x, y = map(int, input().split()) x, y = x-1, y-1 X[x].append(y) X[y].append(x) MI = [min(x) for x in X] MA = [max(x) for x in X] MA2 = [max([y for y in x + [-1] if y != ma]) for x, ma in zip(X, MA)] if False: print("MI =", MI) print("MA =", MA) print("MA2 =", MA2) 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) ##### Settings unit = 0 merge = lambda a, b: a + b ## ↓ この2つは必ず対称的にする(じゃないとバグる) adj_bu = lambda a, i: a * inv(SIZE[i]) % mod adj_td = lambda a, i, p: a * inv(N-SIZE[i]) % mod # SIZEのときはi、それ以外の頂点情報のときはpを使う def adj_bu(a, i, p): if a >= 4: return 4 if a == 0 and p == MI[i]: return 0 if a == 3 and p != MA[i]: return 4 if i == MA[p] or i == MI[p]: return 2 if i == MA2[p]: return 3 return 4 def adj_td(a, i, p): return adj_bu(a, p, i) adj_fin = lambda a, i: "Yes" if a <= 2 else "No" ##### if False: nn = 200200 fa = [1] * (nn+1) fainv = [1] * (nn+1) for i in range(nn): fa[i+1] = fa[i] * (i+1) % mod fainv[-1] = pow(fa[-1], mod-2, mod) for i in range(nn)[::-1]: fainv[i] = fainv[i+1] * (i+1) % mod inv = lambda i: fainv[i] * fa[i-1] % mod print("fa =", fa[:20]) print("fainv =", fainv[:20]) if False: SIZE = [1] * N for i in R[1:][::-1]: SIZE[P[i]] += SIZE[i] print("SIZE =", SIZE) # Size of subtree ME = [unit] * N XX = [0] * N TD = [unit] * N for i in R[1:][::-1]: p = P[i] XX[i] = adj_bu(ME[i], i, p) #################### ME[p] = merge(ME[p], XX[i]) XX[R[0]] = adj_fin(ME[R[0]], R[0]) if False: print("ME =", ME) # Merge before adj print("XX =", XX) # Bottom-up after adj for i in R: ac = TD[i] for j in X[i]: TD[j] = ac ac = merge(ac, XX[j]) ac = unit for j in X[i][::-1]: TD[j] = adj_td(merge(TD[j], ac), j, i) ac = merge(ac, XX[j]) XX[j] = adj_fin(merge(ME[j], TD[j]), j) if False: print("TD =", TD) # Top-down after adj print("XX =", XX) # Final Result print("\n".join(XX))