#AT1498. F - Tree and Constraints

F - Tree and Constraints

F - 树和约束

得分:600分

题目描述

我们有一棵有NN个顶点的树,顶点编号为1到NN

ii条边连接顶点aia_i和顶点bib_i

考虑对每一条边涂上黑色或者白色。总共有2N12^{N-1}种方式来涂色。在这些方式中,有多少种满足以下的MM个限制条件?

  • ii个限制条件(1iM1 \leq i \leq M)由两个整数uiu_iviv_i表示,表示顶点uiu_i和顶点viv_i之间的路径必须至少包含一条黑色的边。

约束条件

  • 2N502 \leq N \leq 50
  • 1ai,biN1 \leq a_i,b_i \leq N
  • 给出的图是一棵树
  • 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
  • 如果iji \neq j,要么uiuju_i \neq u_j,要么vivjv_i \neq v_j
  • 输入中的所有值都是整数

输入

输入从标准输入中得到,格式如下:

NN

a1a_1 b1b_1

...

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

MM

u1u_1 v1v_1

...

uMu_M vMv_M

输出

输出满足所有MM个条件的涂色方式的数量。

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 $M$ restrictions will be satisfied only if Edge $1$ is painted black, so the answer is $1$.


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