結果

問題 No.922 東北きりきざむたん
ユーザー ikdikd
提出日時 2019-11-09 19:59:28
言語 Nim
(2.0.0)
結果
AC  
実行時間 315 ms / 2,000 ms
コード長 2,575 bytes
コンパイル時間 3,928 ms
コンパイル使用メモリ 69,132 KB
実行使用メモリ 45,688 KB
最終ジャッジ日時 2023-10-13 07:30:21
合計ジャッジ時間 9,331 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 94 ms
22,908 KB
testcase_10 AC 70 ms
9,156 KB
testcase_11 AC 83 ms
17,816 KB
testcase_12 AC 41 ms
22,724 KB
testcase_13 AC 27 ms
8,532 KB
testcase_14 AC 139 ms
30,312 KB
testcase_15 AC 31 ms
23,412 KB
testcase_16 AC 248 ms
35,500 KB
testcase_17 AC 249 ms
34,500 KB
testcase_18 AC 254 ms
34,920 KB
testcase_19 AC 253 ms
35,252 KB
testcase_20 AC 256 ms
34,604 KB
testcase_21 AC 266 ms
34,824 KB
testcase_22 AC 274 ms
35,240 KB
testcase_23 AC 302 ms
35,436 KB
testcase_24 AC 315 ms
34,896 KB
testcase_25 AC 284 ms
38,356 KB
testcase_26 AC 286 ms
37,876 KB
testcase_27 AC 281 ms
38,676 KB
testcase_28 AC 104 ms
28,608 KB
testcase_29 AC 274 ms
45,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils, strutils

proc fill(g: seq[seq[int]], i, p, r: int, root, dep: var seq[int]) =
  root[i] = r
  for j in g[i]:
    if j != p:
      dep[j] = dep[i] + 1
      fill(g, j, i, r, root, dep)

proc calc(g: seq[seq[int]], i, p: int, freq: seq[int64], cnt: var seq[int64]) =
  cnt[i] = freq[i]
  for j in g[i]:
    if j != p:
      calc(g, j, i, freq, cnt)
      cnt[i] += cnt[j]

proc solve(g: seq[seq[int]], i, p, r: int, d: int64, cnt: seq[int64], mn: var int64) =
  mn = min(mn, d)
  for j in g[i]:
    if j != p:
      solve(g, j, i, r, d - cnt[j] + (cnt[r] - cnt[j]), cnt, mn)

proc calc_par(g: seq[seq[int]], i, p: int, ppar: var seq[seq[int]]) =
  ppar[0][i] = p
  for j in g[i]:
    if j != p:
      calc_par(g, j, i, ppar)

proc get_lca(aa, bb, r: int, dep: seq[int], ppar: seq[seq[int]]):int =
  var (a, b) = (aa, bb)
  if dep[a] > dep[b]:
    swap(a, b)
  for j in 0..<20:
    if (((dep[b] - dep[a]) shr j) and 1) == 1:
      b = ppar[j][b]
  if a == b:
    return a
  for j in countdown(19, 0):
    if ppar[j][a] != ppar[j][b]:
      a = ppar[j][a]
      b = ppar[j][b]
  return ppar[0][a]

proc main() =
  var n, m, q: int
  (n, m, q) = stdin.readLine.strip.split.map(parseInt)
  var g = newSeqWith(n, newSeq[int]())
  for i in 0..<m:
    var a, b: int
    (a, b) = stdin.readLine.strip.split.map(parseInt)
    g[a - 1].add(b - 1)
    g[b - 1].add(a - 1)
  type P = tuple[a, b: int]
  var pts = newSeq[P](q)
  for i in 0..<q:
    var a, b: int
    (a, b) = stdin.readLine.strip.split.map(parseInt)
    pts[i] = (a - 1, b - 1)

  var 
    root = newSeqWith(n, -1)
    dep = newSeq[int](n)
  for i in 0..<n:
    if root[i] < 0:
      root[i] = i
      fill(g, i, -1, root[i], root, dep)
  var freq = newSeq[int64](n)
  var ans: int64 = 0
  for p in pts:
    let (a, b) = p
    if root[a] == root[b]:
      discard
      # あとで
    else:
      freq[a] += 1
      freq[b] += 1
  var dist = newSeq[int64](n)
  for i in 0..<n:
    dist[root[i]] += dep[i] * freq[i]
  var cnt = newSeq[int64](n)
  for i in 0..<n:
    if i == root[i]:
      calc(g, i, -1, freq, cnt)
      var mn = dist[i]
      solve(g, i, -1, i, dist[i], cnt, mn)
      ans += mn
  
  var ppar = newSeqWith(20, newSeqWith(n, -1))
  for i in 0..<n:
    if i == root[i]:
      calc_par(g, i, -1, ppar)
  for j in 0..<20:
    for i in 0..<n:
      let p = ppar[j][i]
      if p >= 0:
        ppar[j + 1][i] = ppar[j][p]
  for p in pts:
    let (a, b) = p
    if root[a] == root[b]:
      let c = get_lca(a, b, root[a], dep, ppar)
      ans += dep[a] + dep[b] - dep[c] * 2
  echo ans

main()
0