hdu5379(树形dp)

来源:互联网 发布:小米淘宝旗舰店优惠券 编辑:程序博客网 时间:2024/05/17 04:36

Mahjong tree

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


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
292 13 14 35 36 27 48 79 382 13 14 35 16 47 58 4
 

Sample Output
Case #1: 32Case #2: 16

题意:有一棵有n个节点的树,有编号1~n的n个数字要放到树上,且要满足三个要求: 
1.树的每个节点只放一个数字 
2.树的任意一个节点的所有直接孩子节点上面放的数字排序后是连续的 
3.一棵子树的所有节点上面放的数字排序后是连续的 
问有多少种不同的放法,结果取模1e9+7。 

思路:由于所有子树是连续的,所以可以用区间来表示子树,设要给当前子树编号为[1,n],如果当前子树是原树,那么根有两种选择,分别是放头和尾(如果n等于1,那么头和尾重合了,也就是只有1种选择),如果不是原树,那么根的选择是唯一的,因为在考虑它的父亲的时候,它的位置就确定了。如果它的非叶子节点的儿子数目超过两个,显然是无解的,否则就有解,设叶子节点的儿子个数为cnt,答案就是cnt!,如果有非叶子节点的儿子,那么这个儿子可以放头也可以放尾,答案还要乘上2。

#pragma comment (linker,"/STACK:102400000,102400000")#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;typedef long long ll;const int N=100010;const ll mod=1e9+7;vector<int>tu[N];ll jc[N];int n;ll ans;int dfs(int now,int pre){    int l=tu[now].size(),son=0,tree=0,sum=1;    for(int i=0; i<l; i++)    {        int to=tu[now][i];        if(to==pre)continue;        son++;        int tem=dfs(to,now);        if(tem>1)            tree++;        sum+=tem;    }    if(tree>2)        return ans=0;    else if(tree)        ans=ans*2%mod;    ans=ans*jc[son-tree]%mod;    return sum;}int main(){    jc[0]=1;    for(ll i=1; i<N; i++)        jc[i]=jc[i-1]*i%mod;    int t,o=1;    cin>>t;    while(t--)    {        for(int i=0; i<=n; i++)tu[i].clear();        scanf("%d",&n);        if(n==1)        {            printf("Case #%d: 1\n",o++);            continue;        }        for(int i=0; i<n-1; i++)        {            int a,b;            scanf("%d%d",&a,&b);            tu[a].push_back(b);            tu[b].push_back(a);        }        ans=2;///直接考虑根要乘以2        dfs(1,0);        printf("Case #%d: %I64d\n",o++,ans);    }    return 0;}


0 0
原创粉丝点击