Under construction

Still being built. Things may change

Cover the Paths

Time limit1sMemory limit256 MB

Summary
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 nn vertices labeled by integers 1,2,…,n1, 2, \ldots, n. A total of mm simple paths are chosen in this tree. Each path is described as a pair of its endpoints (ai,bi)(a_i, b_i).

Let VV be the set of all vertices of the tree. We say that subset SS of VV is good if for every ii such that 1≤i≤m1 \le i \le m, the simple path from aia_i to bib_i contains at least one vertex from SS. We say that subset TT is the best subset if TT is a good subset and there is no good subset XX such that ∣X∣<∣T∣|X| < |T|.

You have to find the best subset of VV.

Input

The first line contains an integer nn, the number of vertices in the tree (1≤n≤1051 \le n \le 10^5).

Each of the next n−1n - 1 lines describes an edge of the tree. Edge ii is denoted by two integers uiu_i and viv_i, the labels of vertices it connects (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i). It is guaranteed that the given edges form a tree.

The next line contains an integer mm, the number of paths (1≤m≤1051 \le m \le 10^5).

Each of the next mm lines describes a path in the tree. Path ii is denoted by two integers aia_i and bib_i, the labels of the endpoints (1≤ai,bi≤n1 \le a_i, b_i \le n). For some paths, it may be that ai=bia_i = b_i. It is not guaranteed that all paths are pairwise distinct.

Output

On the first line, print the size of the best subset of VV. On the second line, print the labels of vertices belonging to the best subset of VV in any order.

If there are several possible solutions, print any one of them.

Examples2

  1. Example 1

    Input
    4
    1 2
    2 3
    2 4
    2
    1 2
    4 2
    
    Expected output
    1
    2
    
  2. Example 2

    Input
    6
    1 2
    2 3
    3 4
    5 6
    5 2
    5
    2 1
    6 6
    1 4
    3 4
    4 1
    
    Expected output
    3
    6 3 1