树形dp--hdu5379

C - Mahjong tree
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

 

/**
题意:给定一棵树

把1-n填到树的节点上,使得:

1:儿子节点上填的数字是连续的。

2:子树节点上填的数字是连续的。

把儿子节点分成两种,一种是叶子节点,一种是非叶子节点。

显然非叶子节点个数不能超过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 tot=1,nt=0;///nt 包含根节点的所有节点,直接孩子节点||子树个数
    int n=g[u].size();
    int ns=n-1;///孩子个数,减一是减的他的父亲
    if(u==1) ns++;///1是树根,没有父亲,孩子就是n个
    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;///当有多个(非子树的)节点可自由排序,ns-nt=叶子个数,叶子全排列;

    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);///根节点左右两种切法,把1放根上和把n放根上两种方法
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值