結果

問題 No.1023 Cyclic Tour
ユーザー yakamotoyakamoto
提出日時 2020-04-11 23:15:13
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 3,945 bytes
コンパイル時間 19,467 ms
コンパイル使用メモリ 461,200 KB
実行使用メモリ 74,588 KB
最終ジャッジ日時 2023-10-19 20:11:49
合計ジャッジ時間 61,924 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 292 ms
51,832 KB
testcase_01 AC 290 ms
51,824 KB
testcase_02 AC 291 ms
51,836 KB
testcase_03 AC 293 ms
51,812 KB
testcase_04 AC 616 ms
64,004 KB
testcase_05 AC 620 ms
64,540 KB
testcase_06 AC 638 ms
66,036 KB
testcase_07 AC 634 ms
66,108 KB
testcase_08 AC 696 ms
68,136 KB
testcase_09 AC 697 ms
68,152 KB
testcase_10 AC 687 ms
68,032 KB
testcase_11 AC 699 ms
70,100 KB
testcase_12 AC 746 ms
70,204 KB
testcase_13 AC 728 ms
70,104 KB
testcase_14 AC 714 ms
70,184 KB
testcase_15 AC 785 ms
70,968 KB
testcase_16 AC 814 ms
74,412 KB
testcase_17 AC 818 ms
74,588 KB
testcase_18 AC 813 ms
74,392 KB
testcase_19 AC 767 ms
68,348 KB
testcase_20 AC 776 ms
70,528 KB
testcase_21 AC 804 ms
70,368 KB
testcase_22 AC 890 ms
71,104 KB
testcase_23 AC 852 ms
72,536 KB
testcase_24 AC 959 ms
72,600 KB
testcase_25 AC 791 ms
70,372 KB
testcase_26 AC 765 ms
72,380 KB
testcase_27 AC 810 ms
70,964 KB
testcase_28 AC 945 ms
73,180 KB
testcase_29 AC 889 ms
74,552 KB
testcase_30 AC 865 ms
72,236 KB
testcase_31 AC 858 ms
72,516 KB
testcase_32 AC 823 ms
72,364 KB
testcase_33 AC 859 ms
72,992 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 818 ms
70,348 KB
testcase_37 AC 815 ms
72,928 KB
testcase_38 AC 845 ms
74,440 KB
testcase_39 AC 827 ms
72,472 KB
testcase_40 AC 863 ms
72,364 KB
testcase_41 AC 876 ms
72,416 KB
testcase_42 AC 755 ms
73,072 KB
testcase_43 AC 867 ms
74,424 KB
testcase_44 AC 621 ms
64,084 KB
testcase_45 AC 751 ms
70,316 KB
testcase_46 AC 632 ms
70,284 KB
testcase_47 AC 632 ms
66,532 KB
testcase_48 AC 803 ms
70,316 KB
testcase_49 AC 791 ms
70,300 KB
testcase_50 AC 756 ms
70,756 KB
testcase_51 AC 790 ms
70,292 KB
testcase_52 AC 772 ms
70,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007L

class UnionFind(n: Int) {
  private val par = IntArray(n){it}
  val rank = IntArray(n){1} // 集合の要素数
  private val visits = IntArray(n) // 訪れた場所をfind毎に用意するのがもったいないのでつかいまわす

  fun find(x: Int): Int {
    var ptr = 0
    var i = x
    while(par[i] != i) {
      visits[ptr++] = i
      i = par[i]
    }

    repeat(ptr){ par[visits[it]] = i }
    return i
  }

  private fun merge(node: Int, rt: Int): Int {
    par[node] = rt
    rank[rt] += rank[node]
    return rt
  }

  fun unite(x: Int, y: Int): Int {
    val x1 = find(x)
    val y1 = find(y)
    return if (x1 == y1) x1
    else {
      if (rank[x1] < rank[y1])
        merge(x1, y1)
      else
        merge(y1, x1)
    }
  }

  /**
   * xを解決する必要がないときは直にrankをみる
   */
  fun cntNodes(x: Int): Int = rank[find(x)]
}

data class Edge(val a: Int, val b: Int, val tpe: Int)
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    val N = ni()
    val M = ni()
    val E = Array(M){Edge(ni() - 1, ni() - 1, ni())}
    val uf = UnionFind(N)
    for (e in E) {
      if (e.tpe == 1) uf.unite(e.a, e.b)
    }

    val id = IntArray(N){it}
    for (v in 0 until N) {
      id[v] = uf.find(v)
    }

    val outs = Array(N){ mutableListOf<Int>()}
    val ins = IntArray(N)
    val que = java.util.ArrayDeque<Int>()
    for (e in E) {
      if (e.tpe != 2) continue
      outs[id[e.a]].add(id[e.b])
      ins[id[e.b]]++
    }

    for (v in 0 until N) {
      if (ins[v] == 0) que.add(v)
    }

    while(que.isNotEmpty()) {
      val v = que.poll()
      for (u in outs[v]) {
        if (--ins[u] == 0) que.add(u)
      }
    }

    val ok = ins.any { it > 0 }
    out.println(if (ok) "Yes" else "No")
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return map(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun map(n: Int, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i)
    }
    return res
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  private fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: BooleanArray) {
    debug { a.map { if (it) 1 else 0 }.joinToString("") }
  }

  private fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0