結果
問題 | No.922 東北きりきざむたん |
ユーザー | maspy |
提出日時 | 2020-05-16 13:19:30 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 1,360 ms / 2,000 ms |
コード長 | 2,131 bytes |
コンパイル時間 | 301 ms |
コンパイル使用メモリ | 13,056 KB |
実行使用メモリ | 60,796 KB |
最終ジャッジ日時 | 2024-09-22 06:45:25 |
合計ジャッジ時間 | 18,697 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 30 ms
11,008 KB |
testcase_01 | AC | 31 ms
11,264 KB |
testcase_02 | AC | 31 ms
11,008 KB |
testcase_03 | AC | 30 ms
11,264 KB |
testcase_04 | AC | 33 ms
11,136 KB |
testcase_05 | AC | 32 ms
11,136 KB |
testcase_06 | AC | 34 ms
11,392 KB |
testcase_07 | AC | 32 ms
11,264 KB |
testcase_08 | AC | 34 ms
11,264 KB |
testcase_09 | AC | 341 ms
34,816 KB |
testcase_10 | AC | 192 ms
24,296 KB |
testcase_11 | AC | 291 ms
30,892 KB |
testcase_12 | AC | 251 ms
34,116 KB |
testcase_13 | AC | 106 ms
19,272 KB |
testcase_14 | AC | 528 ms
49,592 KB |
testcase_15 | AC | 248 ms
35,200 KB |
testcase_16 | AC | 813 ms
57,208 KB |
testcase_17 | AC | 841 ms
57,344 KB |
testcase_18 | AC | 876 ms
57,208 KB |
testcase_19 | AC | 845 ms
57,208 KB |
testcase_20 | AC | 835 ms
57,212 KB |
testcase_21 | AC | 910 ms
59,796 KB |
testcase_22 | AC | 928 ms
59,712 KB |
testcase_23 | AC | 1,291 ms
57,520 KB |
testcase_24 | AC | 1,360 ms
57,652 KB |
testcase_25 | AC | 1,270 ms
59,040 KB |
testcase_26 | AC | 1,267 ms
59,256 KB |
testcase_27 | AC | 1,286 ms
58,952 KB |
testcase_28 | AC | 403 ms
51,132 KB |
testcase_29 | AC | 1,270 ms
60,796 KB |
ソースコード
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N, M, Q = map(int, readline().split()) G = [[] for _ in range(N + 1)] for _ in range(M): a, b = map(int, readline().split()) G[a].append(b) G[b].append(a) root = [0] * (N + 1) parent = [0] * (N + 1) depth = [0] * (N + 1) stack = [] order = [] for r in range(1, N + 1): if root[r]: continue root[r] = 0 depth[r] = 1 stack = [r] while stack: v = stack.pop() root[v] = r order.append(v) for w in G[v]: if w == parent[v]: continue parent[w] = v depth[w] = depth[v] + 1 stack.append(w) def dist_func(): K = 18 P = [parent] for i in range(1, K): A = P[-1] P.append([A[v] for v in A]) def LCA(u, v): du, dv = depth[u], depth[v] if du < dv: du, dv = dv, du u, v = v, u n = du - dv for i in range(K): if n & 1: u = P[i][u] n >>= 1 if u == v: return u for i in range(K - 1, -1, -1): u1, v1 = P[i][u], P[i][v] if u1 == v1: continue u, v = u1, v1 return parent[u] def dist(u, v): w = LCA(u, v) return depth[u] + depth[v] - 2 * depth[w] return dist dist = dist_func() dp_size = [0] * (N + 1) dp_sum = [0] * (N + 1) m = map(int, read().split()) answer = 0 for a, b in zip(m, m): if root[a] == root[b]: answer += dist(a, b) else: dp_size[a] += 1 dp_size[b] += 1 for v in order[::-1]: p = parent[v] dp_sum[p] += dp_sum[v] + dp_size[v] dp_size[p] += dp_size[v] comp_cost = [0] * (N + 1) for v in order: r, p = root[v], parent[v] if v == r: comp_cost[v] = dp_sum[v] continue lower, upper = dp_size[v], dp_size[r] - dp_size[v] dp_sum[v] = dp_sum[p] + upper - lower if comp_cost[r] > dp_sum[v]: comp_cost[r] = dp_sum[v] answer += sum(comp_cost) print(answer)