Embedding Enumeration
Time limit4sMemory limit512 MB
Count the ways to place a labeled tree's nodes into a 2 by n grid so node 1 sits at the top-left, edges touch, and no cell repeats, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
A tree is a graph with nodes and undirected edges in which every two nodes are joined by exactly one path. In a labeled tree every node carries a different integer between 1 and . Drawing a tree neatly is hard in general, but some trees fit into a rectangular grid.
Let be a labeled tree with nodes. A embedding of maps the nodes of to the cells of a grid with 2 rows and columns so that all three conditions hold.
- Node 1 maps to the cell in the upper left corner.
- Two nodes joined by an edge map to two cells that touch up, down, left or right.
- No two nodes map to the same cell.
Count the embeddings of the given tree, modulo .
Input
The first line contains the number of nodes of ().
The -th of the next lines contains the two endpoints and of the -th edge (, ).
The given graph is always a tree.
Output
Print the number of embeddings of the given tree, modulo .
Note

The figure above draws all 4 embeddings of the tree in the first example.