#abc152f. [abc152_f]Tree and Constraints
[abc152_f]Tree and Constraints
Problem Statement
We have a tree with vertices numbered to . The -th edge in this tree connects Vertex and Vertex .
Consider painting each of these edges white or black. There are such ways to paint the edges. Among them, how many satisfy all of the following restrictions?
- The -th restriction is represented by two integers and , which mean that the path connecting Vertex and Vertex must contain at least one edge painted black.
Constraints
- The graph given in input is a tree.
- If , either or
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of ways to paint the edges that satisfy all of the conditions.
Sample Input 1
3
1 2
2 3
1
1 3
Sample Output 1
3
The tree in this input is shown below:
All of the restrictions will be satisfied if Edge and are respectively painted (white, black), (black, white), or (black, black), so the answer is .
Sample Input 2
2
1 2
1
1 2
Sample Output 2
1
The tree in this input is shown below:
All of the restrictions will be satisfied only if Edge is painted black, so the answer is .
Sample Input 3
5
1 2
3 2
3 4
5 3
3
1 3
2 4
2 5
Sample Output 3
9
The tree in this input is shown below:
Sample Input 4
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
Sample Output 4
62
The tree in this input is shown below: