結果
問題 | No.2892 Lime and Karin |
ユーザー | adapchi |
提出日時 | 2024-09-13 23:31:20 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,936 ms / 8,000 ms |
コード長 | 1,395 bytes |
コンパイル時間 | 360 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 144,400 KB |
最終ジャッジ日時 | 2024-09-13 23:33:00 |
合計ジャッジ時間 | 96,227 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
52,352 KB |
testcase_01 | AC | 41 ms
52,480 KB |
testcase_02 | AC | 42 ms
52,608 KB |
testcase_03 | AC | 42 ms
51,840 KB |
testcase_04 | AC | 44 ms
52,608 KB |
testcase_05 | AC | 45 ms
53,376 KB |
testcase_06 | AC | 47 ms
54,272 KB |
testcase_07 | AC | 49 ms
52,352 KB |
testcase_08 | AC | 53 ms
60,032 KB |
testcase_09 | AC | 53 ms
54,912 KB |
testcase_10 | AC | 47 ms
53,248 KB |
testcase_11 | AC | 42 ms
52,096 KB |
testcase_12 | AC | 46 ms
53,376 KB |
testcase_13 | AC | 47 ms
54,144 KB |
testcase_14 | AC | 1,804 ms
98,324 KB |
testcase_15 | AC | 1,751 ms
96,380 KB |
testcase_16 | AC | 2,511 ms
101,096 KB |
testcase_17 | AC | 1,057 ms
88,984 KB |
testcase_18 | AC | 2,534 ms
104,892 KB |
testcase_19 | AC | 2,974 ms
104,916 KB |
testcase_20 | AC | 671 ms
83,756 KB |
testcase_21 | AC | 2,141 ms
99,204 KB |
testcase_22 | AC | 2,411 ms
98,800 KB |
testcase_23 | AC | 1,387 ms
93,316 KB |
testcase_24 | AC | 3,244 ms
110,956 KB |
testcase_25 | AC | 3,198 ms
109,384 KB |
testcase_26 | AC | 3,123 ms
106,016 KB |
testcase_27 | AC | 3,237 ms
108,528 KB |
testcase_28 | AC | 3,372 ms
109,132 KB |
testcase_29 | AC | 3,218 ms
108,460 KB |
testcase_30 | AC | 3,378 ms
108,776 KB |
testcase_31 | AC | 3,220 ms
106,436 KB |
testcase_32 | AC | 3,129 ms
107,532 KB |
testcase_33 | AC | 3,092 ms
108,324 KB |
testcase_34 | AC | 519 ms
120,232 KB |
testcase_35 | AC | 529 ms
120,720 KB |
testcase_36 | AC | 584 ms
120,748 KB |
testcase_37 | AC | 555 ms
120,592 KB |
testcase_38 | AC | 590 ms
120,548 KB |
testcase_39 | AC | 3,439 ms
136,396 KB |
testcase_40 | AC | 3,429 ms
138,204 KB |
testcase_41 | AC | 3,849 ms
142,452 KB |
testcase_42 | AC | 3,761 ms
144,400 KB |
testcase_43 | AC | 3,936 ms
141,168 KB |
testcase_44 | AC | 1,317 ms
89,732 KB |
testcase_45 | AC | 2,419 ms
101,340 KB |
testcase_46 | AC | 1,491 ms
91,824 KB |
testcase_47 | AC | 1,826 ms
94,044 KB |
testcase_48 | AC | 1,112 ms
88,404 KB |
testcase_49 | AC | 2,084 ms
96,344 KB |
testcase_50 | AC | 1,792 ms
109,636 KB |
testcase_51 | AC | 1,764 ms
109,960 KB |
testcase_52 | AC | 2,175 ms
116,076 KB |
testcase_53 | AC | 2,257 ms
115,364 KB |
testcase_54 | AC | 2,150 ms
114,316 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] 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 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(sum(x + S[u] > 0 for x in d) for d in ds) ans += (pairs(sum(ds, start=[]), S[u]) - sum(pairs(d, S[u]) for d in ds)) // 2 used[u] = True print(ans)