DP---Mahjong tree

HDU  5379

 

Problem Description
Little sun is an artist. Today he is playing mahjong alone. He suddenly feels that the tree in the yard doesn't look good. So he wants to decorate the tree.(The tree has n vertexs, indexed from 1 to n.)
Thought for a long time, finally he decides to use the mahjong to decorate the tree.
His mahjong is strange because all of the mahjong tiles had a distinct index.(Little sun has only n mahjong tiles, and the mahjong tiles indexed from 1 to n.)
He put the mahjong tiles on the vertexs of the tree.
As is known to all, little sun is an artist. So he want to decorate the tree as beautiful as possible.
His decoration rules are as follows:

(1)Place exact one mahjong tile on each vertex.
(2)The mahjong tiles' index must be continues which are placed on the son vertexs of a vertex.
(3)The mahjong tiles' index must be continues which are placed on the vertexs of any subtrees.

Now he want to know that he can obtain how many different beautiful mahjong tree using these rules, because of the answer can be very large, you need output the answer modulo 1e9 + 7.
 
Input
The first line of the input is a single integer T, indicates the number of test cases. 
For each test case, the first line contains an integers n. (1 <= n <= 100000)
And the next n - 1 lines, each line contains two integers ui and vi, which describes an edge of the tree, and vertex 1 is the root of the tree.
 
Output
For each test case, output one line. The output format is "Case #x: ans"(without quotes), x is the case number, starting from 1.
 
Sample Input
2 9 2 1 3 1 4 3 5 3 6 2 7 4 8 7 9 3 8 2 1 3 1 4 3 5 1 6 4 7 5 8 4
 
Sample Output
Case #1: 32 Case #2: 16
 
Author
UESTC
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5659 5658 5657 5656 5655 
 
题意:有n个点,有n-1条边连接这些点成为树,节点分别为从1到n号,1号节点为树根。在这n个节点中填充1~n的数字,三点限制:1、每个节点只填一个数;2、每一个节点的所有子节点的数字连续(排序后);3、子树中的所有点连续(排序后);求有多少种填充的方法。
 
思路:观察规律,一个节点下,有多余两棵子树,则无法填出相应的树,有一个或两个子树,结果乘以2,然后结果还要乘以子叶个数的阶乘,因为子叶可以随意填数,故是全排列。
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int N=100005;
const long long MOD=1e9+7;
vector<int>g[N];
long long A[N],res;

int dfs(int u, int f) 
{
    int nt=0;///包含根节点的所有节点,直接孩子节点,子树个数
    int n=g[u].size();
    int ns=n-1;
    if(u==1) ns++;
    for(int i=0;i<n;i++) 
    {
        int v=g[u][i];
        if(v==f)  continue;///防止相邻的两个节点上下反复递归;
        int num=dfs(v,u);
        if(num>1)   nt++;
    }
    if(nt>2)///有大于两个子树不能使切割后连续
        return res =0;
    if(nt)///有一个或两个子树切割方法数都只有两个
        res=res*2%MOD;
    res=res*A[ns-nt]%MOD;///当有多个(非子树的)节点可自由排序;
    return ns+1;
}

int main() 
{
    A[0]=1;
    for (int i=1;i<N;i++)
    A[i]=A[i-1]*i%MOD;
    
    int t,cas=1,n;
    scanf("%d",&t);
    while(t--) 
    {
        scanf("%d",&n);
        for(int i=0;i<=n;i++)
        g[i].clear();
        for(int i=0;i<n-1;i++) 
        {  
            int a,b;
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
            g[b].push_back(a);
        }
        if(n==1) 
        {
            printf("Case #%d: 1\n",cas++);
            continue;
        }
        res=1;
        dfs(1,0);
        printf("Case #%d: %lld\n",cas++,res*2%MOD);//根节点左右两种切法
    }
    return 0;
}

 

posted @ 2016-04-03 17:07  茶飘香~  阅读(145)  评论(0编辑  收藏  举报