Minimum Depth Tree
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
You are given a rooted tree with \(n\) vertices, rooted at vertex 1.
There are \(m\) allowed operations, given as pairs of numbers \((a_i, b_i)\). The operation \((u, v)\) means rerooting the subtree of vertex \(v\) (including the vertex \(v\) itself) to vertex \(u\). It is forbidden to use the operation \((u, v)\) if vertex \(u\) is in the subtree of vertex \(v\).
What is the minimum possible depth of the tree that can be obtained after applying any number of operations in any order?
Input
The first line contains an integer \(n\) — the number of vertices in the tree.
The next \(n - 1\) lines contain two integers \(u_i\), \(v_i\) — the endpoints of the tree’s edges.
The next line contains an integer \(m\) — the number of allowed operations.
The next \(m\) lines contain pairs of numbers \(a_i\), \(b_i\) — the allowed operations.
Output
In a single line, print an integer — the minimum depth of the tree.
Constraints
\(1 \le n, m \le 3 \cdot 10^5\),
\(1 \le u_i, v_i, a_i, b_i \le n\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 8 1 5 1 2 2 3 3 4 5 6 6 8 4 7 2 1 4 8 7 | 3 |
Notes
Depth of a tree is the length of the longest path from the root to any other vertex.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|