2015 Multi-University Training Contest 7 1011(DP)

Mahjong tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 828    Accepted Submission(s): 258


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
 

 

Source
 

 

题意:有一棵有n个节点的树,有编号1~n的n个数字要放到树上,且要满足三个要求: 

1.树的每个节点只放一个数字 

2.树的任意一个节点的所有直接孩子节点上面放的数字排序后是连续的 

3.一棵子树的所有节点上面放的数字排序后是连续的 

问有多少种不同的放法,结果取模1e9+7。

分析:画图分析发现一棵树的非叶子节点的子树最多有两个,(若存在两个以上,那么非叶子节点子树根节点那一层将区间将不会是连续的) 再分类讨论

1、如果不存在非叶子节点子树,那么答案就是叶子节点个数的阶乘;

2、如果存在一个非叶子节点子树,那么这个非叶子节点子树的根一定是在区间最左或者区间最右,所以答案就是非叶子节点子树的排列方法数*其他叶子节点的排列个数(阶乘)*2,

乘2是因为非叶子节点子树可以在左边也可以在右边

3、如果存在两个非叶子节点子树,那么这两个非叶子节点子树的根一定是在区间最左和区间最右,所以答案就是非叶子节点子树的排列方法数*中间叶子节点的排列个数(阶乘)*2,

乘2是因为非叶子节点子树可以互换位置

第一次写树形DP,也是照着大神的写的,http://blog.csdn.net/hyczms/article/details/47426035

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<stdlib.h>
#include<algorithm>
#define LL __int64
#define FIN freopen("in.txt","r",stdin)
using namespace std;
const int MAXN=100000+5;
const int MOD=1e9+7;
LL jie[MAXN],res;
int n;

vector<int> G[MAXN];

void init()
{
    jie[0]=1;
    for(int i=1;i<=MAXN;i++) jie[i]=jie[i-1]*i%MOD;
}

int DFS(int u,int rt)
{
    int tot=1,ns=0,nt=0,num;//tot表示当前节点为根节点的总节点个数,ns表示直接孩子个数,nt表示子树个数
    int len=G[u].size();
    for(int i=0;i<len;i++)
    {
        int v=G[u][i];
        if(v==rt) continue;
        ns++;
        num=DFS(v,u);       //统计边的另一个节点的为根的总节点个数
        if(num>1) nt++;     //如果非叶子节点说明存在非叶子节点子树
        tot+=num;           //更新当以前节点为根的总节点个数
    }
    if(nt>2) return res=0;  
    if(nt) res=res*2%MOD;
    res=res*jie[ns-nt]%MOD;
    return tot;
}
int main()
{
    //FIN;

    init();

    int kase,Case=0;
    scanf("%d",&kase);
    while(kase--)
    {
        scanf("%d",&n);
        for(int i=0;i<=n;i++) G[i].clear();
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            G[u].push_back(v);
            G[v].push_back(u);
        }

        printf("Case #%d: ",++Case);
        if(n==1) {printf("1\n");continue;}

        res=1;
        DFS(1,0);
        printf("%I64d\n",res*2%MOD);
    }
    return 0;
}
View Code

 

posted @ 2015-08-12 14:14  Cliff Chen  阅读(177)  评论(0编辑  收藏  举报