結果
問題 | No.1023 Cyclic Tour |
ユーザー | yakamoto |
提出日時 | 2020-04-11 23:27:36 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 863 ms / 2,000 ms |
コード長 | 4,046 bytes |
コンパイル時間 | 16,404 ms |
コンパイル使用メモリ | 449,584 KB |
実行使用メモリ | 103,484 KB |
最終ジャッジ日時 | 2024-09-19 16:41:45 |
合計ジャッジ時間 | 52,528 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 255 ms
54,504 KB |
testcase_01 | AC | 249 ms
54,240 KB |
testcase_02 | AC | 252 ms
54,352 KB |
testcase_03 | AC | 251 ms
54,300 KB |
testcase_04 | AC | 540 ms
85,100 KB |
testcase_05 | AC | 546 ms
84,764 KB |
testcase_06 | AC | 525 ms
87,260 KB |
testcase_07 | AC | 542 ms
85,332 KB |
testcase_08 | AC | 619 ms
87,756 KB |
testcase_09 | AC | 602 ms
88,516 KB |
testcase_10 | AC | 612 ms
88,436 KB |
testcase_11 | AC | 628 ms
87,124 KB |
testcase_12 | AC | 609 ms
88,368 KB |
testcase_13 | AC | 621 ms
88,336 KB |
testcase_14 | AC | 629 ms
88,148 KB |
testcase_15 | AC | 581 ms
86,464 KB |
testcase_16 | AC | 725 ms
103,476 KB |
testcase_17 | AC | 744 ms
103,484 KB |
testcase_18 | AC | 698 ms
101,792 KB |
testcase_19 | AC | 719 ms
103,476 KB |
testcase_20 | AC | 706 ms
94,488 KB |
testcase_21 | AC | 724 ms
94,928 KB |
testcase_22 | AC | 726 ms
97,384 KB |
testcase_23 | AC | 725 ms
95,544 KB |
testcase_24 | AC | 764 ms
100,404 KB |
testcase_25 | AC | 720 ms
94,916 KB |
testcase_26 | AC | 713 ms
94,592 KB |
testcase_27 | AC | 681 ms
94,520 KB |
testcase_28 | AC | 784 ms
101,728 KB |
testcase_29 | AC | 863 ms
103,148 KB |
testcase_30 | AC | 734 ms
100,452 KB |
testcase_31 | AC | 762 ms
97,496 KB |
testcase_32 | AC | 728 ms
98,212 KB |
testcase_33 | AC | 781 ms
101,088 KB |
testcase_34 | AC | 571 ms
86,612 KB |
testcase_35 | AC | 625 ms
87,776 KB |
testcase_36 | AC | 752 ms
95,748 KB |
testcase_37 | AC | 709 ms
96,052 KB |
testcase_38 | AC | 755 ms
97,760 KB |
testcase_39 | AC | 750 ms
96,992 KB |
testcase_40 | AC | 712 ms
96,268 KB |
testcase_41 | AC | 759 ms
95,628 KB |
testcase_42 | AC | 720 ms
95,860 KB |
testcase_43 | AC | 791 ms
99,736 KB |
testcase_44 | AC | 562 ms
91,408 KB |
testcase_45 | AC | 610 ms
89,076 KB |
testcase_46 | AC | 540 ms
88,612 KB |
testcase_47 | AC | 530 ms
88,204 KB |
testcase_48 | AC | 687 ms
93,436 KB |
testcase_49 | AC | 699 ms
93,160 KB |
testcase_50 | AC | 674 ms
95,120 KB |
testcase_51 | AC | 750 ms
95,316 KB |
testcase_52 | AC | 729 ms
93,848 KB |
ソースコード
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() }