結果
問題 | No.2892 Lime and Karin |
ユーザー | adapchi |
提出日時 | 2024-09-13 23:40:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,836 ms / 8,000 ms |
コード長 | 1,385 bytes |
コンパイル時間 | 2,909 ms |
コンパイル使用メモリ | 81,900 KB |
実行使用メモリ | 142,364 KB |
最終ジャッジ日時 | 2024-09-13 23:42:01 |
合計ジャッジ時間 | 93,698 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
53,084 KB |
testcase_01 | AC | 37 ms
53,416 KB |
testcase_02 | AC | 36 ms
52,528 KB |
testcase_03 | AC | 35 ms
52,688 KB |
testcase_04 | AC | 38 ms
53,648 KB |
testcase_05 | AC | 40 ms
54,268 KB |
testcase_06 | AC | 42 ms
55,212 KB |
testcase_07 | AC | 39 ms
54,320 KB |
testcase_08 | AC | 48 ms
61,104 KB |
testcase_09 | AC | 46 ms
56,068 KB |
testcase_10 | AC | 39 ms
53,548 KB |
testcase_11 | AC | 37 ms
52,620 KB |
testcase_12 | AC | 42 ms
53,740 KB |
testcase_13 | AC | 40 ms
54,476 KB |
testcase_14 | AC | 1,702 ms
94,100 KB |
testcase_15 | AC | 1,641 ms
93,944 KB |
testcase_16 | AC | 2,439 ms
100,592 KB |
testcase_17 | AC | 1,017 ms
89,756 KB |
testcase_18 | AC | 2,451 ms
102,172 KB |
testcase_19 | AC | 3,009 ms
105,968 KB |
testcase_20 | AC | 628 ms
84,156 KB |
testcase_21 | AC | 2,074 ms
97,088 KB |
testcase_22 | AC | 2,285 ms
100,724 KB |
testcase_23 | AC | 1,381 ms
91,256 KB |
testcase_24 | AC | 3,161 ms
108,396 KB |
testcase_25 | AC | 3,051 ms
109,384 KB |
testcase_26 | AC | 2,959 ms
106,188 KB |
testcase_27 | AC | 3,163 ms
108,652 KB |
testcase_28 | AC | 3,318 ms
110,032 KB |
testcase_29 | AC | 3,137 ms
106,560 KB |
testcase_30 | AC | 3,218 ms
108,132 KB |
testcase_31 | AC | 3,201 ms
107,484 KB |
testcase_32 | AC | 3,179 ms
108,036 KB |
testcase_33 | AC | 3,068 ms
106,020 KB |
testcase_34 | AC | 479 ms
120,780 KB |
testcase_35 | AC | 503 ms
120,720 KB |
testcase_36 | AC | 512 ms
120,484 KB |
testcase_37 | AC | 538 ms
120,472 KB |
testcase_38 | AC | 502 ms
120,548 KB |
testcase_39 | AC | 3,259 ms
135,872 KB |
testcase_40 | AC | 3,301 ms
138,532 KB |
testcase_41 | AC | 3,802 ms
140,500 KB |
testcase_42 | AC | 3,836 ms
142,364 KB |
testcase_43 | AC | 3,828 ms
140,072 KB |
testcase_44 | AC | 1,258 ms
89,408 KB |
testcase_45 | AC | 2,334 ms
99,576 KB |
testcase_46 | AC | 1,400 ms
90,656 KB |
testcase_47 | AC | 1,802 ms
93,760 KB |
testcase_48 | AC | 1,033 ms
89,528 KB |
testcase_49 | AC | 2,009 ms
95,200 KB |
testcase_50 | AC | 1,772 ms
111,092 KB |
testcase_51 | AC | 1,707 ms
110,784 KB |
testcase_52 | AC | 1,959 ms
115,332 KB |
testcase_53 | AC | 2,115 ms
115,512 KB |
testcase_54 | AC | 2,356 ms
114,448 KB |
ソースコード
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)) stk.extend((v, u, 0) for v in G[u] if not used[v] and v != p) else: size[u] = 1 + sum(size[v] for v in G[u] if not used[v] and v != p) p = None s = size[x] try: while True: x, p = next(y for y in G[x] if not used[y] and y != p and size[y] * 2 > s), x except StopIteration: return x def paths(x, p): stk = [(x, p, S[x])] res = [] while stk: u, p, d = stk.pop() res.append(d) stk.extend((v, u, d + S[v]) for v in G[u] if not used[v] and v != p) return res def pairs(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()) if S[u] == 1: ans += 1 vs = [v for v in G[u] if not used[v]] stk.extend(vs) ds = [paths(v, u) for v in vs] ans += sum(x + S[u] > 0 for d in ds for x in d) ans += (pairs(sum(ds, start=[]), S[u]) - sum(pairs(d, S[u]) for d in ds)) // 2 used[u] = True print(ans)