Heaps from Trees
Time limit2sMemory limit512 MB
Given a rooted tree with a value at each node, find the largest subset in which every ancestor-descendant pair has the ancestor's value strictly larger.
- Level
Medium7 of 10
- Topics
- Tree, Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
You are given a rooted tree with nodes. The nodes are labeled to , and node is the root. Each node has a value .
You would like to turn this tree into a heap. That is, you would like to choose the largest possible subset of nodes that satisfies the following heap property: for every pair of nodes in the subset, if node is an ancestor of node in the tree, then .
Equality is not allowed. Find the maximum number of nodes such a subset can contain. The subset does not have to form a subtree.
Input
The input consists of a single test case.
The first line contains a single integer (), the number of nodes in the tree. The nodes are numbered to .
Each of the next lines describes one node, in order of node number. Line contains two integers and , where () is the value of the node and () is the index of its parent. Every node's index is strictly greater than its parent's index. Only node , the root, has , since it has no parent. For every other node (), .
Output
Print a single integer: the number of nodes in the largest subset that satisfies the heap property.