結果
問題 | No.1295 木と駒 |
ユーザー | Kiri8128 |
提出日時 | 2020-11-23 16:28:44 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 402 ms / 2,000 ms |
コード長 | 1,361 bytes |
コンパイル時間 | 176 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 114,508 KB |
最終ジャッジ日時 | 2024-07-23 17:34:32 |
合計ジャッジ時間 | 13,314 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
53,632 KB |
testcase_01 | AC | 44 ms
54,400 KB |
testcase_02 | AC | 51 ms
60,160 KB |
testcase_03 | AC | 45 ms
54,400 KB |
testcase_04 | AC | 46 ms
53,888 KB |
testcase_05 | AC | 47 ms
54,016 KB |
testcase_06 | AC | 45 ms
53,888 KB |
testcase_07 | AC | 44 ms
54,144 KB |
testcase_08 | AC | 43 ms
54,272 KB |
testcase_09 | AC | 45 ms
54,272 KB |
testcase_10 | AC | 45 ms
53,760 KB |
testcase_11 | AC | 44 ms
53,632 KB |
testcase_12 | AC | 46 ms
54,144 KB |
testcase_13 | AC | 45 ms
54,400 KB |
testcase_14 | AC | 346 ms
105,676 KB |
testcase_15 | AC | 346 ms
104,928 KB |
testcase_16 | AC | 350 ms
105,420 KB |
testcase_17 | AC | 335 ms
105,820 KB |
testcase_18 | AC | 337 ms
105,288 KB |
testcase_19 | AC | 262 ms
108,224 KB |
testcase_20 | AC | 273 ms
104,792 KB |
testcase_21 | AC | 299 ms
106,488 KB |
testcase_22 | AC | 298 ms
108,528 KB |
testcase_23 | AC | 324 ms
105,408 KB |
testcase_24 | AC | 327 ms
105,384 KB |
testcase_25 | AC | 270 ms
106,040 KB |
testcase_26 | AC | 200 ms
104,008 KB |
testcase_27 | AC | 210 ms
104,400 KB |
testcase_28 | AC | 335 ms
104,540 KB |
testcase_29 | AC | 210 ms
104,012 KB |
testcase_30 | AC | 206 ms
104,276 KB |
testcase_31 | AC | 208 ms
104,144 KB |
testcase_32 | AC | 313 ms
104,332 KB |
testcase_33 | AC | 302 ms
105,008 KB |
testcase_34 | AC | 402 ms
105,300 KB |
testcase_35 | AC | 308 ms
105,012 KB |
testcase_36 | AC | 344 ms
104,620 KB |
testcase_37 | AC | 320 ms
105,516 KB |
testcase_38 | AC | 329 ms
106,212 KB |
testcase_39 | AC | 300 ms
104,116 KB |
testcase_40 | AC | 304 ms
104,628 KB |
testcase_41 | AC | 259 ms
105,924 KB |
testcase_42 | AC | 271 ms
114,504 KB |
testcase_43 | AC | 263 ms
108,608 KB |
testcase_44 | AC | 264 ms
114,508 KB |
testcase_45 | AC | 257 ms
106,864 KB |
testcase_46 | AC | 266 ms
107,192 KB |
testcase_47 | AC | 258 ms
106,960 KB |
testcase_48 | AC | 261 ms
106,452 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)] 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 def adj_bu(a, i, p): if a >= 4 or (a == 3 and p != MA[i]): return 4 if a == 0 and p == MI[i]: return 0 if i == MA[p] or i == MI[p]: return 2 if i == MA2[p]: return 3 return 4 adj_td = lambda a, i, p: adj_bu(a, p, i) adj_fin = lambda a, i: "Yes" if a <= 2 else "No" ##### 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]) 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) print("\n".join(XX))