import kotlin.math.PI fun main(arr:Array) { val(cityCount, roadCount, logCount) = readLine()!!.split(" ").map { it.toInt() } val roads = mutableMapOf>>() (0 until roadCount).forEach { val(city1, city2, cost) = readLine()!!.split(" ").map { it.toInt() } if(!roads.containsKey(city1)) { roads[city1] = mutableMapOf() } if(!roads.containsKey(city2)) { roads[city2] = mutableMapOf() } if(!roads[city1]!!.containsKey(city2)) { roads[city1]!![city2] = mutableMapOf() } if(!roads[city2]!!.containsKey(city1)) { roads[city2]!![city1] = mutableMapOf() } roads[city1]!![city2]!![cost.toLong()] = true roads[city2]!![city1]!![cost.toLong()] = true } val log = readLine()!!.split(" ").map { it.toLong() } val ans = Proc(roads, log).getAns() println(ans.size) println(ans.sorted().joinToString(" ")) } class Proc(val roads:Map>>, val log:List) { public fun getAns():List { var currentList = roads.map { a->a.value.filter { b-> log[0] in b.value.keys }.map { it.key }.toMutableList() }.reduce { acc, list -> acc.union(list).toMutableList() }.toList() for(i in log.drop(1)) { currentList = currentList.map { a->roads[a]?.let { b-> b.filter { i in it.value.keys }.keys.toMutableList() }?: mutableListOf() }.reduce { acc, mutableList -> acc.union(mutableList).toMutableList() }.toList() } return currentList } }