I made this topic because i need help with a problem: You are given a rooted tree with n nodes (and n - 1 edges, and the root is node 1). All edges are represented as pipes filled with cold water in the beggining, and all nodes are represented as faucets, except the root(node 1) which is a water heater.
Next, you are given m numbers, each representing the number of the faucet(node) which a person wants to turn on. The time(in minutes) they will wait for the hot water to come to the chosen faucet is defined as number of pipes containing cold water on the unique path from the water heater to the faucet. Also, after a person turns on certain faucet, pipes that are on the unique path from that faucet to the water heater, no longer contain cold water. So, you are given m requests, and for every one you need to output the time one would wait for the hot water to come to that faucet(considering all mentioned above).
The time limit is 2 seconds, and memory limit is 256MB.
Thanks in advance!
PS: Here is one test case:
Input:
4
1 4
4 3
2 4
2
3
2
Output:
2
1
Explanation: After we turn on the faucet with number 3(we will obviously wait 2 minutes), pipes (1,4) and (4,3) will no longer contain cold water. Then, we turn on the faucet number 2. The path from heater to node 2 is (1,4) -> (4,2). Since the pipe (1,4) doesn't contain cold water, we will wait only 1 minute.
Aucun commentaire:
Enregistrer un commentaire