Sort by

recency

|

4 Discussions

|

  • + 0 comments
    import java.util.Scanner
    
    import scala.collection.immutable.TreeMap
    
    object Solution {
      def main(args: Array[String]): Unit = {
        val sc = new Scanner(System.in)
    
        val n = sc.nextInt
        val q = sc.nextInt
    
        val links = (0 until n - 1).map(_ => (sc.nextInt, sc.nextInt))
          .groupBy(_._2)
          .map { case (key, list) => key -> list.map(_._1) }
        val salaries = (0 until n).map(_ => sc.nextInt)
    
        case class Node(index: Int, count: Int, children: Seq[Node], data: TreeMap[Int, Int])
    
        val nodes = Array.ofDim[Node](n + 1)
    
        def create(index: Int): Node = {
          val children = links.get(index) match {
            case None => Nil
            case Some(v) => v.map(create)
          }
    
          val sortedChildren = children.sortBy(-_.count)
          val data = if (children.isEmpty) TreeMap[Int, Int]() else
            sortedChildren.tail.foldLeft(sortedChildren.head.data + (salaries(sortedChildren.head.index - 1) -> sortedChildren.head.index))(
              (acc, c) => acc ++ c.data + (salaries(c.index - 1) -> c.index)
            )
    
          val res = Node(index, children.map(_.count).sum, children, data)
          nodes(index) = res
          res
        }
    
        create(1)
    
        var answer = 0
        (0 until q).foreach(_ => {
          val v = sc.nextInt
          val k = sc.nextInt
    
          val id = answer + v
    
          answer = nodes(id).data.drop(k - 1).values.head
          println(answer)
        })
      }
    }
    
  • + 0 comments

    Yes, the chip of the town is issued for the formation of the classes for the field. Part of the turn and essay reviewer is issued for the transom the field. Mode is approved for humans. The thing is done for humans. The candidate is invited for their of the parts. the candidate is involved for the forms for all joys and tis of the field.

  • + 0 comments

    Hello,

    The problem is interesting but I don't get why my solution is not accepted. It says "Wrong Answer" on test case 14 but I downloaded it and a diff shows me nothing (except for the 'no new line at end of file' but that is not the issue)

    Thanks in advance for the help !

  • + 1 comment

    I wrote down and debugged the whole code in C, just to learn the editor doesn't accept C for this Problem.

    Why ???

No more comments