3781.Tree and Table

Time Limit: 1s Memory Limit: 256MB

Little Petya likes trees a lot. Recently his mother has presented him a tree with 2n nodes. Petya immediately decided to place this tree on a rectangular table consisting of 2 rows and n columns so as to fulfill the following conditions:
Each cell of the table corresponds to exactly one tree node and vice versa, each tree node corresponds to exactly one table cell. If two tree nodes are connected by an edge, then the corresponding cells have a common side.
Now Petya wonders how many ways are there to place his tree on the table. He calls two placements distinct if there is a tree node which corresponds to distinct table cells in these two placements. Since large numbers can scare Petya, print the answer modulo 1000000007 (109+7).

Input Format(From the terminal/stdin)

The first line contains a single integer n (1 \le n \le 105). Next (2n-1) lines contain two integers each ai and bi (1 \le ai,bi \le 2n;ai \neq bi) that determine the numbers of the vertices connected by the corresponding edge.

Consider the tree vertexes numbered by integers from 1 to 2n. It is guaranteed that the graph given in the input is a tree, that is, a connected acyclic undirected graph.

Output Format(To the terminal/stdout)

Print a single integer - the required number of ways to place the tree on the table modulo 1000000007 (109+7).

Sample Input 1

Copy
3
1 3
2 3
4 3
5 1
6 2
 \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 1

Copy
12
  \n

Sample Input 2

Copy
4
1 2
2 3
3 4
4 5
5 6
6 7
7 8
 \n
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 2

Copy
28
  \n

Sample Input 3

Copy
2
1 2
3 2
4 2
 \n
 · \n
 · \n
 · \n

Sample Output 3

Copy
0
 \n

Hints

Note to the first sample (all 12 variants to place the tree on the table are given below):

1-3-2 2-3-1 5 4 6 6 4 5
| | | | | | | | | | | |
5 4 6 6 4 5 1-3-2 2-3-1
4-3-2 2-3-4 5-1 6 6 1-5
 | | | | | | | |
5-1 6 6 1-5 4-3-2 2-3-4
1-3-4 4-3-1 5 2-6 6-2 5
| | | | | | | |
5 2-6 6-2 5 1-3-4 4-3-1

Submit

请先 登录

© 2025 FAQs