Investigation
Time limit2sMemory limit1024 MB
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.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
A 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 houses and 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 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 , and let be the children of . Then for some () the thief is hiding in one of the houses of the subtree rooted at .
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.
Input
The first line contains the number of houses in the city, . Houses are numbered from to .
The second line contains space separated integers . Integer () means that a road joins the house numbered and the house numbered , and holds.
The limit is .
Output
Print one integer, the number of houses you have to search in the worst case when you follow an optimal strategy.