Life in Wartime
Time limit2sMemory limit512 MB
Given N distinct cities (all below 250) in an infinite binary heap tree, count cities that either host a unit or lie on the unique path between two units.
- Level
Medium6 of 10
- Topics
- Tree, Hash map, Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
War has broken out in Seokhwan. Seokhwan is a nation shaped like an enormous binary tree, made up of cities numbered . It has roads, and for , the -th road connects city to city . A picture of this is shown below.

Prime Minister Winston Agiseokhwan has taken on the grave task of saving Seokhwan from its crisis. The hostile nations are bent on disrupting Seokhwan's important military facilities, so to protect the people of Seokhwan it is effective to defend first the cities that armies travel through often. In Seokhwan there are military units stationed in distinct cities, and the units move between cities to exchange supplies and information.
A city is dangerous if a military unit is stationed there, or if there exist two distinct military units whose path passes through that city. Note that Seokhwan is a tree and a path is defined never to visit the same city twice, so the path between any two military units is always unique.
For Prime Minister Agiseokhwan, compute the number of dangerous cities in Seokhwan.
Input
The first line gives the number of military units . ()
The next lines give the sequence of the numbers of the cities containing military units. The given cities are all distinct. ()
Output
Print the number of dangerous cities in Seokhwan.