結果
問題 | No.2892 Lime and Karin |
ユーザー | adapchi |
提出日時 | 2024-09-13 23:12:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 2,302 ms / 8,000 ms |
コード長 | 1,446 bytes |
コンパイル時間 | 308 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 130,760 KB |
最終ジャッジ日時 | 2024-09-13 23:13:23 |
合計ジャッジ時間 | 63,500 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
52,992 KB |
testcase_01 | AC | 42 ms
52,608 KB |
testcase_02 | AC | 37 ms
52,864 KB |
testcase_03 | AC | 37 ms
52,992 KB |
testcase_04 | AC | 40 ms
53,376 KB |
testcase_05 | AC | 39 ms
53,376 KB |
testcase_06 | AC | 41 ms
53,760 KB |
testcase_07 | AC | 38 ms
53,120 KB |
testcase_08 | AC | 43 ms
54,016 KB |
testcase_09 | AC | 50 ms
54,400 KB |
testcase_10 | AC | 40 ms
53,248 KB |
testcase_11 | AC | 39 ms
52,736 KB |
testcase_12 | AC | 43 ms
52,736 KB |
testcase_13 | AC | 42 ms
53,760 KB |
testcase_14 | AC | 1,218 ms
91,696 KB |
testcase_15 | AC | 1,172 ms
90,020 KB |
testcase_16 | AC | 1,588 ms
94,824 KB |
testcase_17 | AC | 715 ms
84,704 KB |
testcase_18 | AC | 1,658 ms
96,596 KB |
testcase_19 | AC | 2,064 ms
101,372 KB |
testcase_20 | AC | 479 ms
81,580 KB |
testcase_21 | AC | 1,356 ms
92,996 KB |
testcase_22 | AC | 1,549 ms
94,412 KB |
testcase_23 | AC | 1,007 ms
87,936 KB |
testcase_24 | AC | 2,265 ms
101,344 KB |
testcase_25 | AC | 2,222 ms
103,736 KB |
testcase_26 | AC | 2,218 ms
99,844 KB |
testcase_27 | AC | 2,218 ms
100,524 KB |
testcase_28 | AC | 2,114 ms
102,612 KB |
testcase_29 | AC | 2,190 ms
101,612 KB |
testcase_30 | AC | 2,302 ms
102,836 KB |
testcase_31 | AC | 2,124 ms
101,436 KB |
testcase_32 | AC | 2,197 ms
101,540 KB |
testcase_33 | AC | 2,082 ms
101,812 KB |
testcase_34 | AC | 396 ms
111,004 KB |
testcase_35 | AC | 371 ms
110,548 KB |
testcase_36 | AC | 398 ms
111,712 KB |
testcase_37 | AC | 429 ms
111,272 KB |
testcase_38 | AC | 405 ms
111,672 KB |
testcase_39 | AC | 1,944 ms
125,128 KB |
testcase_40 | AC | 1,996 ms
127,456 KB |
testcase_41 | AC | 2,162 ms
130,760 KB |
testcase_42 | AC | 2,090 ms
129,856 KB |
testcase_43 | AC | 2,172 ms
129,052 KB |
testcase_44 | AC | 919 ms
88,480 KB |
testcase_45 | AC | 1,712 ms
97,020 KB |
testcase_46 | AC | 1,026 ms
89,580 KB |
testcase_47 | AC | 1,170 ms
91,312 KB |
testcase_48 | AC | 791 ms
85,972 KB |
testcase_49 | AC | 1,303 ms
93,512 KB |
testcase_50 | AC | 1,218 ms
105,284 KB |
testcase_51 | AC | 1,220 ms
106,220 KB |
testcase_52 | AC | 1,422 ms
106,980 KB |
testcase_53 | AC | 1,418 ms
107,200 KB |
testcase_54 | AC | 1,405 ms
108,392 KB |
ソースコード
from bisect import bisect_left N = int(input()) G = [[] for _ in range(N)] for _ in range(N - 1): u, v = [int(x) - 1 for x in input().split()] G[u].append(v) G[v].append(u) S = [1 if x == '1' else -1 for x in input()] used = [False] * N size = [0] * N def centroid(x): stk = [(x, None, 0)] while stk: u, p, t = stk.pop() if t == 0: stk.append((u, p, 1)) for v in G[u]: if not used[v] and v != p: stk.append((v, u, 0)) else: size[u] = 1 + sum(size[v] for v in G[u] if not used[v] and v != p) p = None s = size[x] while True: y = next((y for y in G[x] if not used[y] and y != p and size[y] * 2 > s), None) if y is None: break x, p = y, x return x def totals(x, p): stk = [(x, p, S[x])] res = [] while stk: u, p, d = stk.pop() res.append(d) for v in G[u]: if not used[v] and v != p: stk.append((v, u, d + S[v])) return res def f(xs, y): xs.sort() res = 0 i = len(xs) for x in xs: while i >= 1 and x + xs[i - 1] + y > 0: i -= 1 res += len(xs) - i return res stk = [0] ans = 0 while stk: u = centroid(stk.pop()) ds = [] val = 0 if S[u] == 1: ans += 1 for v in G[u]: if used[v]: continue stk.append(v) d = totals(v, u) ans += sum(x + S[u] > 0 for x in d) val -= f(d, S[u]) ds.extend(d) val += f(ds, S[u]) ans += val // 2 used[u] = True print(ans)