Problem Statement You are given a tree with N N vertices numbered 1 1 to N N. The i i-th…
- User
Problem Statement You are given a tree with N N vertices numbered 1 1 to N N. The i i-th edge connects vertices A i A i and B i B i .
Consider a tree that can be obtained by removing some (possibly zero) edges and vertices from this graph. Find the minimum number of vertices in such a tree that includes all of K K specified vertices V 1 , … , V K V 1 ,…,V K .
Constraints 1 ≤ K ≤ N ≤ 2 × 1 0 5 1≤K≤N≤2×10 5
1 ≤ A i , B i ≤ N 1≤A i ,B i ≤N 1 ≤ V 1 < V 2 < … < V K ≤ N 1≤V 1 <V 2 <…<V K ≤N The given graph is a tree. All input values are integers. Input The input is given from Standard Input in the following format:
N N K K A 1 A 1
B 1 B 1
⋮ ⋮ A N − 1 A N−1
B N − 1 B N−1
V 1 V 1
… … V K V K
Output Print the answer.
Sample 1 Inputcopy Outputcopy 7 3 1 2 1 3 2 4 2 5 3 6 3 7 1 3 5 4 The given tree is shown on the left in the figure below. The tree with the minimum number of vertices that includes all of vertices 1 , 3 , 5 1,3,5 is shown on the right.
Figure
Sample 2 Inputcopy Outputcopy 4 4 3 1 1 4 2 1 1 2 3 4 4 Sample 3 Inputcopy Outputcopy 5 1 1 4 2 3 5 2 1 2 1 1 Sualda nə deyilir?Zəhmət olmasa idea və hint ver
- Assistant
To solve this problem, we need to find the smallest subtree that includes all K specified vertices in a given tree with N vertices. The goal is to determine the minimum number of vertices that this subtree will contain.
Idea:
-
Understanding the Minimal Subtree:
- The minimal subtree that includes all K specified vertices is essentially the union of all the paths between these vertices.
-