atcoder#ABC152F. [ABC152F] Tree and Constraints

[ABC152F] Tree and Constraints

Score : 600600 points

Problem Statement

We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aia_i and Vertex bib_i. Consider painting each of these edges white or black. There are 2N12^{N-1} such ways to paint the edges. Among them, how many satisfy all of the following MM restrictions?

  • The ii-th (1iM)(1 \leq i \leq M) restriction is represented by two integers uiu_i and viv_i, which mean that the path connecting Vertex uiu_i and Vertex viv_i must contain at least one edge painted black.

Constraints

  • 2N502 \leq N \leq 50
  • 1ai,biN1 \leq a_i,b_i \leq N
  • The graph given in input is a tree.
  • 1Mmin(20,N(N1)2)1 \leq M \leq \min(20,\frac{N(N-1)}{2})
  • 1ui<viN1 \leq u_i < v_i \leq N
  • If iji \not= j, either uiuju_i \not=u_j or vivjv_i\not=v_j
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 b1b_1

::

aN1a_{N-1} bN1b_{N-1}

MM

u1u_1 v1v_1

::

uMu_M vMv_M

Output

Print the number of ways to paint the edges that satisfy all of the MM conditions.

3
1 2
2 3
1
1 3
3

The tree in this input is shown below:

Figure

All of the MM restrictions will be satisfied if Edge 11 and 22 are respectively painted (white, black), (black, white), or (black, black), so the answer is 33.

2
1 2
1
1 2
1

The tree in this input is shown below:

Figure

All of the MM restrictions will be satisfied only if Edge 11 is painted black, so the answer is 11.

5
1 2
3 2
3 4
5 3
3
1 3
2 4
2 5
9

The tree in this input is shown below:

Figure

8
1 2
2 3
4 3
2 5
6 3
6 7
8 6
5
2 7
3 5
1 6
2 8
7 8
62

The tree in this input is shown below:

Figure