Given a tree and a thief hidden at one node, find the minimum worst-case number of queries to locate him in an optimal search strategy.
Hard8TreeDynamic programmingGreedyNo attempts yetTime limit2sMemory limit1024 MBA robbery happened in a city of Bytelandia. The thief got away and hid somewhere in the city. You are the investigator on the case, and your goal is to find the thief and arrest him.
The city has N houses and N−1 roads. Each road joins two houses, and between any two houses there is exactly one path, so the city forms a tree. The thief hides in one of the houses.
To narrow down his location you pick a house h and search it. If the thief was hiding there, you arrest him on the spot. Otherwise you question the inhabitants of that house and they give you the following information. Picture the city as a tree rooted at house h, and let c1,c2,…,cm be the children of h. Then for some i (1≤i≤m) the thief is hiding in one of the houses of the subtree rooted at ci.
You keep searching houses until you find and arrest the thief. Assume the thief stays in the house he first chose for the whole investigation. The final search, the one that catches him, also counts toward the number of searched houses.
The order of the searches matters. Even a search that misses returns the information above, which sharply cuts down the set of houses where the thief can still be. So you need a strategy that makes the number of searched houses in the worst case as small as possible.
You are given the description of the city. Compute the number of houses you have to search in the worst case when you follow an optimal strategy.
The first line contains the number of houses in the city, N. Houses are numbered from 0 to N−1.
The second line contains N−1 space separated integers v1 v2 … vN−1. Integer vi (1≤i≤N−1) means that a road joins the house numbered vi and the house numbered i, and vi<i holds.
The limit is 2≤N≤105.
Print one integer, the number of houses you have to search in the worst case when you follow an optimal strategy.