結果

問題 No.168 ものさし
ユーザー t8m8⛄️t8m8⛄️
提出日時 2016-11-27 22:47:46
言語 Nim
(2.0.2)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,582 bytes
コンパイル時間 4,052 ms
コンパイル使用メモリ 69,196 KB
実行使用メモリ 37,084 KB
最終ジャッジ日時 2023-09-12 07:43:21
合計ジャッジ時間 6,746 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
11,628 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 37 ms
11,700 KB
testcase_12 AC 122 ms
29,608 KB
testcase_13 AC 173 ms
37,076 KB
testcase_14 AC 173 ms
37,024 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 10 ms
4,980 KB
testcase_19 AC 169 ms
29,272 KB
testcase_20 AC 182 ms
37,084 KB
testcase_21 AC 180 ms
37,064 KB
testcase_22 AC 181 ms
37,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils, sequtils, algorithm, math

type
  DisjointSet = ref object
    par: seq[int]

proc newDisjointSet*(n: int): DisjointSet =
  result = DisjointSet(par: newSeq[int](n))
  for i in 0..n-1:
    result.par[i] = i

proc find*(self: DisjointSet, x: int): int =
  if self.par[x] == x:
    result = x
  else:
    self.par[x] = self.find(self.par[x])
    result = self.par[x]

proc same*(self: DisjointSet, x, y: int): bool =
  result = self.find(x) == self.find(y)

proc unite*(self: DisjointSet, x, y: int) =
  let
    s = self.find(x)
    t = self.find(y)
  if s != t: self.par[s] = t

proc `$`*(self: DisjointSet): string =
  result = $self.par

proc kruskal*(n: int, s, t, c: seq[int]): int64 =
  var
    m = s.len
    edges = newSeq[int64](m)

  for i in 0..m-1:
    edges[i] = c[i].int64 shl 32 or i
  edges.sort(cmp[int64])

  var ds = newDisjointSet(n)

  for i in 0..m-1:
    var cur = cast[int32](edges[i])
    if not ds.same(s[cur], t[cur]):
      ds.unite(s[cur], t[cur])
      result = c[cur].int64
    if ds.same(0, n-1): break

when isMainModule:
  var
    n = stdin.readLine.parseInt
    p = newSeqWith(n, newSeq[int](2))

  for i in 0..n-1:
    p[i] = stdin.readLine.split.map(parseInt)

  var
    s: seq[int] = @[]
    t: seq[int] = @[]
    c: seq[int] = @[]
  for i in 0..n-1:
    for j in i+1..n-1:
      s.add(i)
      t.add(j)
      var
        dx = p[i][0] - p[j][0]
        dy = p[i][1] - p[j][1]
        dd = dx*dx + dy*dy
        d = sqrt(dd.float).int64
      while dd > d*d: d += 1
      c.add(int((d+9) div 10 * 10))


  echo kruskal(n, s, t, c)
0