atcoder#ABC152F. [ABC152F] Tree and Constraints
[ABC152F] Tree and Constraints
Score : points
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.
3
1 2
2 3
1
1 3
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 .
2
1 2
1
1 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 .
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:
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: