Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sprint6.fin.B
- import java.io.BufferedReader
- import java.io.InputStreamReader
- import java.util.*
- fun main() {
- val buffer = BufferedReader(InputStreamReader(System.`in`))
- val cityCount = buffer.readLine().toInt()
- val graph = buildMap<Int, Visited> {
- (1..cityCount).forEach { put(it, Visited(Color.WHITE, mutableSetOf())) }
- repeat(cityCount - 1) {
- val cityId = it + 1
- buffer.readLine().forEachIndexed { index, way ->
- val toCity = index + cityId + 1
- when (way) {
- 'R' -> this[cityId]!!.edges.add(toCity)
- 'B' -> this[toCity]!!.edges.add(cityId)
- }
- }
- }
- }
- for (next in graph.entries) {
- if (next.value.color != Color.WHITE) continue
- val stack = Stack<Int>()
- stack.add(next.key)
- while (!stack.empty()) {
- val cur = stack.pop()
- val curVisited = graph[cur]!!
- if (curVisited.color == Color.WHITE) {
- curVisited.color = Color.GREY
- stack.push(cur)
- curVisited.edges.forEach {
- val color = graph[it]!!.color
- if (color == Color.WHITE)
- stack.push(it)
- if (color == Color.GREY) {
- println("NO")
- return
- }
- }
- } else if (curVisited.color == Color.GREY) {
- curVisited.color = Color.BLACK
- }
- }
- }
- if (graph[cityCount]!!.color == Color.BLACK)
- println("YES")
- else println("NO")
- }
- data class Visited(var color: Color, val edges: MutableSet<Int>)
- enum class Color {
- WHITE,
- GREY,
- BLACK
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement