Submission #25769156


Source Code Expand

when NimMajor <= 0 and NimMinor <= 18: import future else: import sugar

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
when defined release: {.checks: off.}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from typetraits import arity
from sequtils import map, mapIt, newSeqWith, toSeq
from strutils import split, parseInt, parseFloat, parseBool, parseEnum, parseBiggestInt
when NimMajor == 0 and NimMinor >= 14: from strutils import parseBiggestUInt

import macros

from terminal import setForegroundColor, ForegroundColor, resetAttributes
proc warn(message: string) =
  when not defined release:
    stderr.setForegroundColor(fgYellow)
    stderr.write("注意: ")
    stderr.resetAttributes
    stderr.writeLine message

when not declared parseBiggestUInt:
  proc parseBiggestUInt(s: string): uint64 = uint64(parseInt(s))
when not declared SomeFloat:
  type SomeFloat = float | float64 | float32
when not defined nimHasRunnableExamples:
  template runnableExamples*(body: untyped) = discard

proc parseT(s: string; T: typedesc): auto =
  ## `parse` 系関数のジェネリック版
  ## `T` が `SomeOrdinal | SomeFloat` (subranges を含む) でない場合,そのまま `s` を返す
  runnableExamples:
    doAssert parseT("12", int) == 12
    doAssert parseT("12", uint) == 12
    doAssert parseT("12", int64) == 12
    doAssert parseT("12", float32) == 12.0
    doAssert parseT("Yes", bool) == true

  when T is SomeSignedInt: cast[T](parseBiggestInt(s))
  elif T is SomeUnsignedInt: cast[T](parseBiggestUInt(s))
  elif T is SomeFloat: cast[T](parseFloat(s))
  elif T is enum: parseEnum[T](s)
  elif T is bool: parseBool(s)
  elif T is char: s[0]
  else: s

proc unpackWithParse*(input: openArray[string]; T: typedesc[tuple]): T =
  ## 文字列配列を `T` で指定された `tuple` に `parse` しながら変換する
  runnableExamples:
    let t = unpackWithParse(@["1", "1", "1", "1", "1"], 4, tuple[a: int8, b: uint32, c: float64, d: bool])
    doAssert int8 is t.a.type and t.a == 1
    doAssert uint32 is t.b.type and t.b == 1
    doAssert float64 is t.c.type and t.c == 1.0
    doAssert bool is t.d.type and t.d == true
    doAssert tuple[a: int8, b: uint32, c: float64, d: bool] is t.type

  var i = 0
  for x in result.fields:
    if i > input.high:
      warn "元の配列の長さが " & $T.arity & " 未満だから,一部デフォルト値になってるよ"
      break
    x = parseT(input[i], x.type)
    i.inc
  result

proc input(T: typedesc[string]): string = stdin.readLine
proc input(T: typedesc[SomeOrdinal | SomeFloat | char]): auto = input(string).parseT(T)
proc input(T: typedesc[seq[string]]): auto = input(string).split
proc input(T: typedesc[seq[char]]): auto = toSeq(input(string).items)
proc input[E: SomeOrdinal | SomeFloat](T: typedesc[seq[E]]): auto = input(seq[string]).mapIt(it.parseT(E.type))
proc input(T: typedesc[tuple]): auto = input(seq[string]).unpackWithParse(T)

proc toTupleType(parTuple: NimNode): NimNode {.compileTime.} =
  ## `nnkPar` で表現されてる名前付きタプルを `tuple[]` 形式に変換する
  ## `nnkPar` が名前付きタプルじゃない場合は,そのまま返す
  runnableExamples:
    static:
      doAssert((quote do: (a: int, b: float)).toTupleType == (quote do: tuple[a: int, b: float]))
      doAssert((quote do: (a, b: int)).toTupleType == (quote do: tuple[a, b: int]))
      doAssert((quote do: (int, int)).toTupleType == (quote do: (int, int)))
      doAssert((quote do: ()).toTupleType == (quote do: ()))

  if parTuple.len == 0 or parTuple.findChild(it.kind == nnkExprColonExpr) == nil: # () or (T, U, ...)
    result = parTuple
  else:
    result = newTree(nnkTupleTy)
    var identDefs = newTree(nnkIdentDefs)
    for field in parTuple:
      if field.kind == nnkIdent: # (a, b, ..., x: T)  の a, b, ... 部分 (x 以外)
        identDefs.add(field)
      field.copyChildrenTo(identDefs)
      if field.kind != nnkIdent: # (..., x: T, y: ...) の x: T 部分
        identDefs.add(newEmptyNode())
        result.add(identDefs)
        identDefs = newTree(nnkIdentDefs)

proc seqInputCall(bracketTree: NimNode): NimNode {.compileTime.} =
  ## `seq[N, seq[M, ..., [seq[T]]...]]` を `newSeqWith(N, newSeqWith(M, ..., input(seq[T])...))` にする

  if bracketTree.kind != nnkBracketExpr:
    case bracketTree.kind:
      of nnkPar: # x: (Field0: ...)
        result = newCall("input", bracketTree.toTupleType)
      else: # x: T
        result = newCall("input", bracketTree)
  else:
    case bracketTree.len:
      of 2: # seq[N, ... seq[T] ...] の seq[T]
        if bracketTree[^1].kind == nnkBracketExpr:
          error("seq[seq[T]] みたいな書き方はできないって言ったでしょ! seq[N, seq[T]] みたいに書き直してっ")
        result = newCall("input", bracketTree)
      of 3: # それ以外 (seq[N, ...])
        result = newCall("newSeqWith", bracketTree[1], seqInputCall(bracketTree[^1]))
      else:
        error("変な入力があるよ")

proc appendedProcCallBlocks(procCalls: NimNode; i: int): NimNode =
  ## `procCalls[i]` の関数呼び出しを
  ## .. code-block:: Nim
  ##   block:
  ##     var it = procCall(...)
  ## という形に変換しながらつなげていく
  ## 最後だけは
  ## .. code-block:: Nim
  ##   block:
  ##     var it = lastProcCall(...)
  ##     it
  ## というようにして `it` を返すようにする

  let it = ident("it")
  let procCall = procCalls[i]
  result = newStmtList(quote do:
    var `it` = `procCall`
  )
  if i == procCalls.len - 1: # 最後の要素だけは it を返すようにする
    result.add(it)
  else:
    result.add(appendedProcCallBlocks(procCalls, i + 1))
  result = newBlockStmt(result)

proc inputsImpl(pre, post: NimNode): NimNode {.compileTime.} =
  ## pre で指定された変数に post の結果を入れる

  # input() 部分の生成
  var inputCall: NimNode
  case pre.kind:
    of nnkPar: # (x, y, ...): ...
      result = newTree(nnkVarTuple)
      pre.copyChildrenTo(result)
      result.add(newEmptyNode())
      case post[0].kind:
        of nnkPar: # (x, y, ...): (T, T, ...)
          inputCall = newCall("input", post[0].toTupleType)
        of nnkTupleTy: # (x, y, ...): tuple[Field0: ...]
          inputCall = newCall("input", post)
        else: # (x, y, ...): T
          var parTupleTy = newTree(nnkPar)
          for _ in 0..<pre.len: parTupleTy.add(post[0]) # (int, int, int, ...) みたいなのを作ってる
          inputCall = newCall("input", parTupleTy)
    else: # x: ...
      result = newTree(nnkIdentDefs).add(pre).add(newEmptyNode())
      case post[0].kind:
        of nnkPar: # x: (Field0: ...)
          inputCall = newCall("input", post[0].toTupleType)
        of nnkBracketExpr:
          inputCall = seqInputCall(post[0])
        else: # x: T
          inputCall = newCall("input", post[0])

  # 関数部分の生成(ある場合)と結合
  if post.len > 1:
    let it = ident("it")
    var itStmts = when NimMajor == 0 and NimMinor < 17: newStmtList(quote do: (var `it` = `inputCall`)) # 入力の読み込み
                  else: newStmtList(quote do: (var `it` {.used.} = `inputCall`))
    itStmts.add(appendedProcCallBlocks(post, 1)) # 関数の適用
    result.add(newTree(nnkStmtListExpr, newBlockStmt(itStmts))) # 結合
  else:
    result.add(inputCall) # 結合

macro inputs*(body: untyped): untyped =
  ## 入力を受け取る
  ## 宣言の後に `it` を用いた式を(`y` みたいに用いなくてもいいんだけど)いくつでもかくことができ,
  ## その返り値が変数の値となる
  ## 式中で型が変わってもいい(下の例の `u`, `y` とか見たいな感じ)
  ##
  ## .. code-block:: Nim
  ##   inputs:
  ##     a: int
  ##     b: string
  ##     c: (x: char, y: float)
  ##     d: tuple[x: char, y: float]
  ##     e: (u, v: BiggestInt)
  ##     f: tuple[u, v: int]
  ##     g: seq[int]
  ##     h: seq[a, seq[int]]
  ##     i: seq[a, (s, t: int)]
  ##     (j, k, l): int
  ##     (m, n): (char, float)
  ##     o: seq[a, string]
  ##     (p): int
  ##     q: seq[int]; it.sorted(system.cmp)
  ##     r: seq[float]; it.sorted(cmp).filterIt(it > 0)
  ##     s: seq[char]
  ##     t: seq[2, seq[a, int]]
  ##     u: string; parseInt(it); abs(-it)
  ##     (_, v): (a: char, b: char)
  ##     (w, x): tuple[a, b: char]
  ##     y: int; "hoge"

  expectKind(body, nnkStmtList)
  result = newTree(nnkStmtList)
  var letSection = newTree(nnkLetSection)
  for decl in body:
    case decl[0].kind:
      of nnkIdent: # x: ...
        letSection.add(inputsImpl(decl[0], decl[1]))
      of nnkPar:
        expectMinLen(decl[0], 1)
        if decl[0].len == 1: # (x): ... これは x: ... と同じ扱いにしたい
          letSection.add(inputsImpl(decl[0][0], decl[1]))
        else: # (x, y, ...): ...
          letSection.add(inputsImpl(decl[0], decl[1]))
      else:
        expectKind(decl[0], {nnkIdent, nnkPar})

  result.add(letSection)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc `ceilDiv`*[T](x, y: T): T = x div y + ord(x mod y != 0)
proc `//=`*(x: var SomeInteger; y: SomeInteger) = x = x div y
proc `%=`*(x: var SomeInteger; y: SomeInteger) = x = x mod y
template `<?=`*[T](x: var T; y: T) = x = min(x, y)
template `>?=`*[T](x: var T; y: T) = x = max(x, y)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc withIndex*[T](s: openArray[T]): seq[tuple[i: int, v: T]] =
  (0..s.high).mapIt((it, s[it]))

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template countIt*[T](a: openArray[T]; pred: untyped): int =
  var result = 0
  for it {.inject.} in items(a):
    if pred: result.inc
  result

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc reversed*(s: string): string =
  result = newString(s.len)
  for i, c in s: result[s.len - i - 1] = c

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import newSeqWith, allIt

template newSeqWithImpl[T](lens: seq[int]; init: T; currentDimension, lensLen: static[int]): untyped =
  when currentDimension == lensLen:
    newSeqWith(lens[currentDimension - 1], init)
  else:
    newSeqWith(lens[currentDimension - 1], newSeqWithImpl(lens, init, currentDimension + 1, lensLen))

template newSeqWith*[T](lens: varargs[int]; init: T): untyped =
  assert(lens.allIt(it > 0))
  newSeqWithImpl(@lens, init, 1, lens.len)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
when not defined(release):
  template stderrEcho*(x: varargs[string, `$`]) =
    for v in x:
      stderr.write(v)
    stderr.writeLine ""

  template stderrEcho*[T](x: seq[seq[T]]) =
    for v in x: stderrEcho v

  template stderrEcho*(x: seq[string]) =
    for v in x: stderrEcho v
else:
  template stderrEcho*(x: untyped) = discard

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from std/os import relativePath, getCurrentDir

proc discardableId[T](x: T): T {.discardable.} = x

template dbg*(exprs: varargs[untyped]): untyped =
  let result = (exprs)
  when not defined(release):
    let info = instantiationInfo(fullPaths = true)
    stderr.write "[", os.relativePath(info.filename, os.getCurrentDir()), ":", $info.line, "] "
    stderr.writeLine (exprs).astToStr, " = ", result
  discardableId(result)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import newSeqWith

type
  UnionFind* = object
    par: seq[int] # x が根のときその木のサイズ, そうじゃないとき par[x] = xの親
  UnionFindRef* = ref UnionFind
  UnionFindTypes = UnionFind | UnionFindRef

proc initUnionFind*(n: int): UnionFind =
  UnionFind(par: newSeqWith(n, -1))

proc newUnionFind*(n: int): UnionFindRef =
  UnionFindRef(par: newSeqWith(n, -1))

proc findRoot*(this: var UnionFindTypes; x: int): int =
  if this.par[x] < 0:
    return x
  else:
    this.par[x] = this.findRoot(this.par[x])
    return this.par[x]

proc find*(this: var UnionFindTypes; x: int): int =
  this.findRoot(x)

proc size*(this: var UnionFindTypes; x: int): int =
  -this.par[this.findRoot(x)]

proc unite*(this: var UnionFindTypes; x, y: int): bool {.discardable.} =
  var
    xr = this.findRoot(x)
    yr = this.findRoot(y)

  if xr == yr:
    return false

  if this.size(xr) < this.size(yr):
    swap(xr, yr)

  this.par[xr] += this.par[yr]
  this.par[yr] = xr
  return true

proc union*(this: var UnionFindTypes; x, y: int): bool {.discardable.} =
  this.unite(x, y)

proc same*(this: var UnionFindTypes; x, y: int): bool =
  this.findRoot(x) == this.findRoot(y)

proc isSame*(this: var UnionFindTypes; x, y: int): bool =
  this.same(x, y)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from algorithm import sort

type
  Edge* = tuple[u, v, cost: int]
  Kruskal* = object
    V: int # 頂点数
    es: seq[Edge] # sort済みの辺集合
    isChanged: bool # 前回と比べて変更されたか?
    resultCost: int # 最小全域木のコスト
    resultEdges: seq[Edge] # 最小全域木に使われるコスト
  KruskalRef* = ref Kruskal
  KruskalTypes = Kruskal | KruskalRef

proc initKruscal*(V: int): Kruskal =
  Kruskal(V: V, es: newSeq[Edge](), isChanged: false)

proc newKruskal*(V: int): KruskalRef =
  KruskalRef(V: V, es: newSeq[Edge](), isChanged: false)

proc toKruskal*(s: seq[(int, int, int)]; V: int): Kruskal =
  Kruskal(V: V, es: s, isChanged: false)

proc addEdge*(this: var KruskalTypes; u, v, cost: int) =
  this.es.add((u, v, cost))
  this.isChanged = true

proc calc(this: var KruskalTypes) =
  this.es.sort(proc (e1, e2: Edge): int = system.cmp(e1.cost, e2.cost))
  this.resultEdges = newSeq[Edge]()
  var uf = initUnionFind(this.V)
  for e in this.es:
    if not uf.isSame(e.u, e.v):
      uf.unite(e.u, e.v)
      this.resultCost += e.cost
      this.resultEdges.add(e)
  this.isChanged = false

proc cost*(this: var KruskalTypes): int =
  if not this.isChanged:
    this.resultCost
  else:
    this.calc()
    this.resultCost

proc edges*(this: var KruskalTypes): seq[Edge] =
  if not this.isChanged:
    this.resultEdges
  else:
    this.calc()
    this.resultEdges

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from std/math import sum
from std/sequtils import filterIt

inputs:
  (N, M): int
  E: seq[M, tuple[A, B, C: int]]; it.mapIt((A: it.A.pred, B: it.B.pred, C: it.C))

var K = initKruscal(N)
for (a, b, c) in E:
  let c = (if c > 0: c else: 0)
  K.addEdge(a, b, c)
  K.addEdge(b, a, c)
K.calc()
echo E.mapIt(it.C).filterIt(it > 0).sum - K.cost
stderrEcho K.edges

Submission Info

Submission Time
Task E - Destruction
User nimon
Language Nim (1.0.6)
Score 500
Code Size 15095 Byte
Status AC
Exec Time 217 ms
Memory 68188 KiB

Compile Error

/imojudge/sandbox/Main.nim(14, 6) Warning: imported and not used: 'terminal' [UnusedImport]

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 23
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 4 ms 3116 KiB
hand_02.txt AC 5 ms 3236 KiB
random_01.txt AC 148 ms 47488 KiB
random_02.txt AC 35 ms 10636 KiB
random_03.txt AC 149 ms 42648 KiB
random_04.txt AC 33 ms 10492 KiB
random_05.txt AC 48 ms 12840 KiB
random_06.txt AC 113 ms 30912 KiB
random_07.txt AC 151 ms 61580 KiB
random_08.txt AC 203 ms 62192 KiB
random_09.txt AC 172 ms 46472 KiB
random_10.txt AC 132 ms 44340 KiB
random_11.txt AC 143 ms 48380 KiB
random_12.txt AC 117 ms 43184 KiB
random_13.txt AC 217 ms 68188 KiB
random_14.txt AC 182 ms 49736 KiB
random_15.txt AC 122 ms 36304 KiB
random_16.txt AC 125 ms 36752 KiB
random_17.txt AC 211 ms 67240 KiB
random_18.txt AC 162 ms 45524 KiB
sample_01.txt AC 2 ms 3244 KiB
sample_02.txt AC 2 ms 3240 KiB
sample_03.txt AC 2 ms 3156 KiB