Back To School '17: Hot and Cold

View as PDF

Submit solution


Points: 20 (partial)
Time limit: 2.5s
Memory limit: 256M

Author:
Problem type

You are overseeing a game of Hot and Cold on an unweighted bidirectional tree with N nodes (numbered 1 to N). In this game, you are the leader and there are S seekers, each playing their own individual game. This means that the game of one seeker does not impact the game of any other.

Each seeker and their game is characterized by three integers, as, bs and ts. The target node, ts, is the node that they are trying to find. The seeker will then traverse each node on the straight path from as to bs and ask Hot or Cold? at each one. You will respond with a number, the distance from that node to the target.

After answering all these questions, you want to know the sum of answers to each node. If no question has been asked about the node, then the answer is 0.

Input Specification

The first line contains two integers, N S.

The next N1 lines contain two integers, x y, denoting an edge exists between x and y.

The next S lines contain three integers, as bs ts.

Constraints

1N,S105

1as,bs,tsN

There are 4 subtasks worth {15%,15%,35%,35%}.

Subtask 1 & 2 [15% + 15% = 30%]

1N,S100

Subtask 1 & 3 [15% + 35% = 50%]

The target tn is always 1 (tn=1,nN)

Output Specification

Print N integers. The nth integer being the sum of answers to queries about node n. Note that answers may not fit in a 32-bit signed int.

Sample Input 1

Copy
5 3
1 2
2 3
2 4
1 5
3 5 1
5 4 1
2 4 1

Sample Output 1

Copy
0 3 2 4 2

Explanation for Sample Output 1

Sample Input 2 (not in pretests)

Copy
6 2
3 4
2 5
6 4
5 4
1 2
6 2 6
5 1 2

Sample Output 2

Copy
1 3 0 1 3 0

Comments

There are no comments at the moment.