結果

問題 No.1023 Cyclic Tour
ユーザー yakamotoyakamoto
提出日時 2020-04-11 23:27:36
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 959 ms / 2,000 ms
コード長 4,046 bytes
コンパイル時間 20,573 ms
コンパイル使用メモリ 455,128 KB
実行使用メモリ 75,784 KB
最終ジャッジ日時 2023-10-19 20:36:58
合計ジャッジ時間 64,625 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 293 ms
51,872 KB
testcase_01 AC 299 ms
51,868 KB
testcase_02 AC 301 ms
51,876 KB
testcase_03 AC 294 ms
51,872 KB
testcase_04 AC 591 ms
61,828 KB
testcase_05 AC 578 ms
61,848 KB
testcase_06 AC 589 ms
61,880 KB
testcase_07 AC 579 ms
61,836 KB
testcase_08 AC 744 ms
68,868 KB
testcase_09 AC 705 ms
68,584 KB
testcase_10 AC 674 ms
68,124 KB
testcase_11 AC 802 ms
70,440 KB
testcase_12 AC 749 ms
70,328 KB
testcase_13 AC 729 ms
70,304 KB
testcase_14 AC 714 ms
70,820 KB
testcase_15 AC 738 ms
70,756 KB
testcase_16 AC 836 ms
74,504 KB
testcase_17 AC 874 ms
75,312 KB
testcase_18 AC 806 ms
75,784 KB
testcase_19 AC 826 ms
69,580 KB
testcase_20 AC 783 ms
70,356 KB
testcase_21 AC 799 ms
70,296 KB
testcase_22 AC 907 ms
71,184 KB
testcase_23 AC 846 ms
72,528 KB
testcase_24 AC 947 ms
73,016 KB
testcase_25 AC 882 ms
70,456 KB
testcase_26 AC 790 ms
72,564 KB
testcase_27 AC 774 ms
70,440 KB
testcase_28 AC 938 ms
72,964 KB
testcase_29 AC 863 ms
74,420 KB
testcase_30 AC 936 ms
73,240 KB
testcase_31 AC 916 ms
72,544 KB
testcase_32 AC 874 ms
72,464 KB
testcase_33 AC 959 ms
73,072 KB
testcase_34 AC 650 ms
64,032 KB
testcase_35 AC 725 ms
64,832 KB
testcase_36 AC 812 ms
70,448 KB
testcase_37 AC 806 ms
73,156 KB
testcase_38 AC 829 ms
74,484 KB
testcase_39 AC 917 ms
72,488 KB
testcase_40 AC 875 ms
72,660 KB
testcase_41 AC 917 ms
72,564 KB
testcase_42 AC 839 ms
72,512 KB
testcase_43 AC 892 ms
74,948 KB
testcase_44 AC 626 ms
64,520 KB
testcase_45 AC 745 ms
70,328 KB
testcase_46 AC 622 ms
70,580 KB
testcase_47 AC 618 ms
66,504 KB
testcase_48 AC 790 ms
70,412 KB
testcase_49 AC 835 ms
70,380 KB
testcase_50 AC 758 ms
70,452 KB
testcase_51 AC 822 ms
71,128 KB
testcase_52 AC 865 ms
71,152 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): Boolean {
    val x1 = find(x)
    val y1 = find(y)
    return if (x1 == y1) false
    else {
      if (rank[x1] < rank[y1])
        merge(x1, y1)
      else
        merge(y1, x1)

      true
    }
  }

  /**
   * 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) {
        if (!uf.unite(e.a, e.b)) {
          out.println("Yes")
          return
        }
      }
    }

    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