Cover the Paths
Time limit1sMemory limit256 MB
Given a tree and m simple paths, find a minimum-size vertex set that intersects every path, and output its size and members.
- Level
Hard8 of 10
- Topics
- Tree, Greedy, DFS, Implementation
- Solved
- No attempts yet
Problem
You are given an undirected unweighted tree consisting of vertices labeled by integers . A total of simple paths are chosen in this tree. Each path is described as a pair of its endpoints .
Let be the set of all vertices of the tree. We say that subset of is good if for every such that , the simple path from to contains at least one vertex from . We say that subset is the best subset if is a good subset and there is no good subset such that .
You have to find the best subset of .
Input
The first line contains an integer , the number of vertices in the tree ().
Each of the next lines describes an edge of the tree. Edge is denoted by two integers and , the labels of vertices it connects (, ). It is guaranteed that the given edges form a tree.
The next line contains an integer , the number of paths ().
Each of the next lines describes a path in the tree. Path is denoted by two integers and , the labels of the endpoints (). For some paths, it may be that . It is not guaranteed that all paths are pairwise distinct.
Output
On the first line, print the size of the best subset of . On the second line, print the labels of vertices belonging to the best subset of in any order.
If there are several possible solutions, print any one of them.