Heaps from Trees

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.

Medium7TreeDynamic programmingGreedySortingNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a rooted tree with nn nodes. The nodes are labeled 11 to nn, and node 11 is the root. Each node ii has a value viv_i.

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 i,ji, j in the subset, if node ii is an ancestor of node jj in the tree, then vi>vjv_i > v_j.

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 nn (1n2×1051 \le n \le 2 \times 10^5), the number of nodes in the tree. The nodes are numbered 11 to nn.

Each of the next nn lines describes one node, in order of node number. Line ii contains two integers viv_i and pip_i, where viv_i (0vi1090 \le v_i \le 10^9) is the value of the node and pip_i (0pi<i0 \le p_i < i) is the index of its parent. Every node's index is strictly greater than its parent's index. Only node 11, the root, has p1=0p_1 = 0, since it has no parent. For every other node (i=2,,ni = 2, \ldots, n), 1pi<i1 \le p_i < i.

Output

Print a single integer: the number of nodes in the largest subset that satisfies the heap property.