結果

問題 No.922 東北きりきざむたん
ユーザー ikdikd
提出日時 2019-11-09 19:27:31
言語 Nim
(2.0.2)
結果
WA  
実行時間 -
コード長 1,646 bytes
コンパイル時間 4,606 ms
コンパイル使用メモリ 69,400 KB
実行使用メモリ 30,856 KB
最終ジャッジ日時 2023-10-13 07:29:39
合計ジャッジ時間 9,156 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,356 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,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 24 ms
11,252 KB
testcase_13 AC 21 ms
5,236 KB
testcase_14 WA -
testcase_15 AC 14 ms
11,172 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 79 ms
14,116 KB
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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 main() =
  var n, m, q: int
  (n, m, q) = stdin.readLine.split.map(parseInt)
  var g = newSeqWith(n, newSeq[int]())
  for i in 0..<m:
    var a, b: int
    (a, b) = stdin.readLine.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.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]:
      ans += dep[a] + dep[b]
    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
  echo ans

main()
0