Submission #3503052


Source Code Expand

import strutils
import sequtils
import algorithm
import math
import queues
import tables
import sets
import logging
import future

const INF* = int(1e18 + 373)

proc readLine*(): string = stdin.readLine()
proc readSeq*(): seq[string] = readLine().strip().split()
proc readSeq*(n: Natural): seq[string] =
  result = newSeq[string](n)
  for i in 0..<n:
    result[i] = readLine().strip()
proc readInt1*(): int = readSeq().map(parseInt)[0]
proc readInt2*(): (int, int) =
  let a = readSeq().map(parseInt)
  return (a[0], a[1])
proc readInt3*(): (int, int, int) =
  let a = readSeq().map(parseInt)
  return (a[0], a[1], a[2])
proc readInt4*(): (int, int, int, int) =
  let a = readSeq().map(parseInt)
  return (a[0], a[1], a[2], a[3])
type seq2*[T] = seq[seq[T]]
proc newSeq2*[T](n1, n2: Natural): seq2[T] = newSeqWith(n1, newSeq[T](n2))
type seq3*[T] = seq[seq[seq[T]]]
proc newSeq3*[T](n1, n2, n3: Natural): seq3[T] = newSeqWith(n1, newSeqWith(n2, newSeq[T](n3)))

#------------------------------------------------------------------------------#
type UnionFindTree = object
  p: seq[int]
  h: seq[int]

proc initUnionFindTree(n: int): UnionFindTree =
  result.p = newSeq[int](n)
  result.p.fill(-1)
  result.h = newSeq[int](n)
  result.h.fill(0)

proc find(this: var UnionFindTree, v: int): int =
  if this.p[v] == -1:
    return v
  this.p[v] = find(this, this.p[v])
  return this.p[v]

proc same(this: var UnionFindTree, u, v: int): bool =
  this.find(u) == this.find(v)

proc unite(this: var UnionFindTree, u, v: int) =
  var uRoot = this.find(u)
  var vRoot = this.find(v)
  if uRoot == vRoot:
    return

  if this.h[uRoot] < this.h[vRoot]:
    swap(uRoot, vRoot)

  this.p[vRoot] = uRoot
  if this.h[uRoot] == this.h[vRoot]:
    this.h[uRoot] += 1

#------------------------------------------------------------------------------#

type Edge =
  tuple [ w, u, v: int ]

proc main() =
  let (n, m, k) = readInt3()
  let c = readSeq().map(parseInt).map(it => it - 1)

  var es = newSeq[Edge]()
  for i in 0..<m:
    let (a, b, w) = readInt3()
    es.add((w, a - 1, b - 1))
  es.sort(cmp[Edge])

  var uft = initUnionFindTree(n + k)
  for i in 0..<n:
    if c[i] == -1:
      continue
    uft.unite(i, n + c[i])

  var ans = 0
  var cnt = 0
  for e in es:
    if cnt >= k - 1:
      break

    if uft.same(e.u, e.v):
      continue
    uft.unite(e.u, e.v)
    ans += e.w
    cnt += 1

  if cnt == k - 1:
    echo ans
  else:
    echo -1

main()

Submission Info

Submission Time
Task A - Colorful MST
User somq14
Language Nim (0.13.0)
Score 700
Code Size 2552 Byte
Status AC
Exec Time 130 ms
Memory 12160 KB

Compile Error

Hint: system [Processing]
Hint: Main [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: sequtils [Processing]
Hint: algorithm [Processing]
Hint: math [Processing]
Hint: times [Processing]
Hint: queues [Processing]
Hint: tables [Processing]
Hint: hashes [Processing]
Hint: etcpriv [Processing]
Hint: sets [Processing]
Hint: os [Processing]
Hint: posix [Processing]
Hint: logging [Processing]
lib/pure/logging.nim(128, 22) Hint: 'Exception' is declared but not used [XDeclaredButNotUsed]
Hint: future [Processing]
Hint: macros [Processing]
Hint:  [Link]
Hint: operation successful (24893 lines compiled; 2.728 sec total; 25.256MB; Release Build) [SuccessX]

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 100 / 100 100 / 100 200 / 200 300 / 300
Status
AC × 4
AC × 7
AC × 12
AC × 7
AC × 28
Set Name Test Cases
Sample 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt
Subtask1 00_example_03.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt
Subtask2 s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt
Subtask3 00_example_02.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt
Subtask4 00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 00_example_04.txt, s1_01.txt, s1_02.txt, s1_03.txt, s1_04.txt, s1_05.txt, s1_06.txt, s2_07.txt, s2_08.txt, s2_09.txt, s2_10.txt, s2_11.txt, s2_12.txt, s3_13.txt, s3_14.txt, s3_15.txt, s3_16.txt, s3_17.txt, s3_18.txt, s4_19.txt, s4_20.txt, s4_21.txt, s4_22.txt, s4_23.txt, s4_24.txt
Case Name Status Exec Time Memory
00_example_01.txt AC 1 ms 256 KB
00_example_02.txt AC 1 ms 256 KB
00_example_03.txt AC 1 ms 256 KB
00_example_04.txt AC 1 ms 256 KB
s1_01.txt AC 1 ms 256 KB
s1_02.txt AC 114 ms 11452 KB
s1_03.txt AC 1 ms 256 KB
s1_04.txt AC 1 ms 256 KB
s1_05.txt AC 59 ms 9340 KB
s1_06.txt AC 117 ms 12076 KB
s2_07.txt AC 16 ms 2428 KB
s2_08.txt AC 118 ms 12052 KB
s2_09.txt AC 90 ms 7016 KB
s2_10.txt AC 85 ms 5872 KB
s2_11.txt AC 118 ms 9212 KB
s2_12.txt AC 113 ms 9212 KB
s3_13.txt AC 15 ms 2044 KB
s3_14.txt AC 112 ms 12012 KB
s3_15.txt AC 89 ms 6888 KB
s3_16.txt AC 86 ms 6656 KB
s3_17.txt AC 111 ms 12012 KB
s3_18.txt AC 108 ms 10988 KB
s4_19.txt AC 16 ms 2008 KB
s4_20.txt AC 115 ms 9456 KB
s4_21.txt AC 95 ms 7016 KB
s4_22.txt AC 89 ms 6092 KB
s4_23.txt AC 130 ms 12160 KB
s4_24.txt AC 113 ms 9584 KB