Christmas tree

0

0 votes
Algorithms, Approved, Depth First Search, Graphs, Medium
Problem

Little Santa have to give gifts to N children. There are N1 roads connecting the homes of N children. Each road has some special Santa tax value which only Santa have to pay. Little Santa is wondering what is the realKth minimum tax value in the path between the home of child realA and child realB.

Note: kth minimum value in a list A is kth value in the list A after sorting A.

enter image description here

Input format:
First line contains one integer, N (2N7.5105), denoting the number of children. Next N1 lines contains three space separated integers each, x,y,w (1x,yN),(0w109), denoting that home of child x is connected to home of child y and the tax value of the road is w. Next line will contain an integer, q (1q7.5105), denoting the number of queries. Next q lines contains three space separated integers each, a,b (1a,bN) and k (1kN1).

To generate realA and realB you have to use following formulas:

  • realAi=((ai1+max(0,lastAns))modn)+1
  • realBi=((bi1+2max(0,lastAns))modn)+1
  • realKi=((ki1+3max(0,lastAns))modn)+1

Where lastAns is answer for previous query, or 0 if this is the first query.

Output format:
For each query, print the number the realKth minimum tax value in the path between the home of child realA and child realB. Print 1 if there is no such value.

Time Limit: 3
Memory Limit: 512
Source Limit:
Explanation

Here are the real values for queries from sample:

realA1=4,realB1=5,realK1=1

realA2=3,realB4=4,realK2=1

realA3=5,realB3=2,realK3=1

Editor Image

?