Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,389 views
in Online Assessments by Expert (110,880 points) | 1,389 views

1 Answer

0 like 0 dislike

There were 4 questions. I managed to solve 3, but couldn't solve this one. Any hints/solutions will be appreciated.

 

Robber on Tree!

 

You are given a rooted tree of N nodes, numbered 1 to N, with root as 1. Each node in the tree has some number of coins denoted by Ci representing the number of coins the ith node has.

 

At time t = 0, you are at the root node and there's a robber at one of the leaf nodes R. At each step, both you and robber take one step each and move to an adjacent node.

 

To prevent chaos, there's some restriction on both your and robber's movement. You have to choose a leaf node and take the shortest path to that leaf node and stop once you reach that leaf node. The robber has to take the shortest path to the root node and stop once the robber reaches the root node.

 

A person reaching a node first gets to take all the coins in the node leaving it empty for anyone who visits it after that time. If 2 people reach the node at the same time, they each get half of the coins in that node.

 

What's the maximum number of coins that you can aim to get in this process?

 

2<= N <= 100,000
-10000 <= Ci <= 10000

 

The number of coins in each node is even.

 

Input format:

 

First line contains two integers N, R representing the number of nodes in the tree and the position of the robber at t = 0.

 

Next N-1 lines contain two integers A, B denoting that there's an Undirected edge between nodes A and B.

 

The next line contains N space separated integers which represent the coins in each node.

 

Output Format:

 

The maximum no. of coins you can get.

by Expert (110,880 points)
What's the solution?

Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .