2437: CF599 - E. Sandy and Nuts
[Creator : ]
Description
Rooted treeis a connected graph without any simple cycles with one vertex selected as a root. In this problem the vertex number $1$ will always serve as a root.
Lowest common ancestorof two vertices $u$ and $v$ is the farthest from the root vertex that lies on both the path from $u$ to the root and on path from $v$ to the root. We will denote it as LCA(u,v).
Sandy had a rooted tree consisting of $n$ vertices that she used to store her nuts. Unfortunately, the underwater storm broke her tree and she doesn't remember all it's edges. She only managed to restore $m$ edges of the initial tree and $q$ triples $a_i,b_i,c_i$, for which she supposes LCA$(a_i,b_i)=c_i$.
Help Sandy count the number of trees of size $n$ with vertex $1$ as a root, that match all the information she remembered. If she made a mess and there are no such trees then print $0$. Two rooted trees are considered to be distinct if there exists an edge that occur in one of them and doesn't occur in the other one.
Sandy had a rooted tree consisting of $n$ vertices that she used to store her nuts. Unfortunately, the underwater storm broke her tree and she doesn't remember all it's edges. She only managed to restore $m$ edges of the initial tree and $q$ triples $a_i,b_i,c_i$, for which she supposes LCA$(a_i,b_i)=c_i$.
Help Sandy count the number of trees of size $n$ with vertex $1$ as a root, that match all the information she remembered. If she made a mess and there are no such trees then print $0$. Two rooted trees are considered to be distinct if there exists an edge that occur in one of them and doesn't occur in the other one.
Input
The first line of the input contains three integers $n, m, q\ (1≤n≤13,0≤m<n,0≤q≤100)$ − the number of vertices, the number of edges and LCA triples remembered by Sandy respectively.
Each of the next $m$ lines contains two integers $u_i, v_i\ (1≤u_i,v_i≤n,u_i≠v_i)$− the numbers of vertices connected by the $i$-th edge. It's guaranteed that this set of edges is a subset of edges of some tree.
The last $q$ lines contain the triplets of numbers $a_i, b_i, c_i\ (1≤a_i,b_i,c_i≤n)$. Each of these triples define $\text{LCA}(a_i,b_i)=c_i$. It's not guaranteed that there exists a tree that satisfy all the given LCA conditions.
Each of the next $m$ lines contains two integers $u_i, v_i\ (1≤u_i,v_i≤n,u_i≠v_i)$− the numbers of vertices connected by the $i$-th edge. It's guaranteed that this set of edges is a subset of edges of some tree.
The last $q$ lines contain the triplets of numbers $a_i, b_i, c_i\ (1≤a_i,b_i,c_i≤n)$. Each of these triples define $\text{LCA}(a_i,b_i)=c_i$. It's not guaranteed that there exists a tree that satisfy all the given LCA conditions.
Output
Print a single integer− the number of trees of size n that satisfy all the conditions.
Sample 1 Input
4 0 0
Sample 1 Output
16
Sample 2 Input
4 0 1
3 4 2
Sample 2 Output
1
Sample correct answer looks like this:
Sample 3 Input
3 1 0
1 2
Sample 3 Output
2
There are two possible trees:
3 0 2
2 3 2
2 3 1
0
The answer is 0 because the information about LCA is inconsistent.
Sample 4 Input
4 1 2
1 2
2 2 2
3 4 2
Sample 4 Output
1