We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*importjava.util.*importjava.util.Scanner.*funmain(args:Array<String>){/* Enter your code here. Read input from STDIN. Print output to STDOUT. */valquery=readln().toInt()for(iin0untilquery){valc=HashMap<Int,HashSet<Int>>()valq:Queue<Int>=LinkedList()valnm=readln().split(" ")valn=nm[0].toInt()valm=nm[1].toInt()for(iin0untilm){valuv=readln().split(" ")valu=uv[0].toInt()valv=uv[1].toInt()if(c[u]==null){c[u]=HashSet()}if(c[v]==null){c[v]=HashSet()}c[u]!!.add(v)c[v]!!.add(u)}vals=readln().trim().toInt()valvisited=Array(n){0}q.offer(s)while(q.isNotEmpty()){valnode=q.peek()c[node]?.forEach{if(visited[it-1]==0){visited[it-1]=visited[node-1]+6q.offer(it)}}q.poll()}valsb=StringBuilder()for(iinvisited.indices){valv=visited[i]if(i+1!=s){if(v>0){sb.append(v)}else{sb.append("-1")}if(i!=visited.lastIndex){sb.append(" ")}}}println(sb.toString())}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
BFS: Shortest Reach in a Graph
You are viewing a single comment's thread. Return to all comments →
Kotlin code