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=;
const long long MOD=1e9+;
vector<int>g[N];
long long A[N],res; int dfs(int u, int f)
{
int nt=;///包含根节点的所有节点,直接孩子节点,子树个数
int n=g[u].size();
int ns=n-;
if(u==) ns++;
for(int i=;i<n;i++)
{
int v=g[u][i];
if(v==f) continue;///防止相邻的两个节点上下反复递归;
int num=dfs(v,u);
if(num>) nt++;
}
if(nt>)///有大于两个子树不能使切割后连续
return res =;
if(nt)///有一个或两个子树切割方法数都只有两个
res=res*%MOD;
res=res*A[ns-nt]%MOD;///当有多个(非子树的)节点可自由排序;
return ns+;
} int main()
{
A[]=;
for (int i=;i<N;i++)
A[i]=A[i-]*i%MOD; int t,cas=,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
g[i].clear();
for(int i=;i<n-;i++)
{
int a,b;
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
if(n==)
{
printf("Case #%d: 1\n",cas++);
continue;
}
res=;
dfs(,);
printf("Case #%d: %lld\n",cas++,res*%MOD);//根节点左右两种切法
}
return ;
}

DP---Mahjong tree的更多相关文章

  1. HDU 5379 树形DP Mahjong tree

    任意一棵子树上节点的编号连续,每个节点的所有二字节点连续,求编号方案的总数. 稍微分析一下可知 每个节点的非叶子节点个数不能多于两个,否则这个子树无解,从而整棵树都无解. 每棵子树将所有节点按照编号从 ...

  2. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  3. 【HDOJ 5379】 Mahjong tree

    [HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共同拥有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根 ...

  4. HDU 5379 Mahjong tree(dfs)

    题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...

  5. HDU 5379——Mahjong tree——————【搜索】

    Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  6. HDU 5379 Mahjong tree(树的遍历&amp;组合数学)

    本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...

  7. Hdu 5379 Mahjong tree (dfs + 组合数)

    题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...

  8. 2015 Multi-University Training Contest 7 hdu 5379 Mahjong tree

    Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  9. Mahjong tree (hdu 5379 dfs)

    Mahjong tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  10. DP Intro - Tree DP Examples

    因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...

随机推荐

  1. Sql中的Merge和output

    先看merge, 不用merge时: --更新 update TA Value ) --插入没有的数据 insert into TA ,,Value from TB ) and TypeName=@v ...

  2. Spring源码追踪4——SpringMVC View解析

    这次的议题是返回json和返回普通view经过的路线差异. ---------------------------------------------------------------------- ...

  3. 10个你必须掌握的超酷VI命令技巧

    摘要:大部分Linux开发者对vi命令相当熟悉,可是遗憾的是,大部分开发者都只能掌握一些最常用的Linux vi命令,下面介绍的10个vi命令虽然很多不为人知,但是在实际应用中又能让你大大提高效率. ...

  4. SQL调优 - Hints指定索引 解决慢查询案例

    背景 每当交易高峰时期,可能会暴露一些平时无法发现的问题,机遇和挑战并存.下面聊聊最近解决的一个案例,因为执行计划走错导致慢查询,进而引发应用线程阻塞.线程池爆满,最后应用功能瘫痪.如何标本兼治的解决 ...

  5. IE11兼容性设定

    增加browser文件,如下:  <browsers>   <browser refID="Default">     <capabilities&g ...

  6. CentOS 6 安装 python 2.7 和 mod_wsgi 运行pyocr[tesseract]

    最新版本的tesseract-ocr 3.0.4 需要运行于python2.7版本以上,因此需要升级系统中默认的2.6版本python及Mod_WSGI 1,下载安装Python-2.7.10 > ...

  7. android html 与webview属性从冲突

    在最近的项目开发中,使用webview加载html页面,这样可以节省大量页面开发的时间,同时也可加快项目进度. 我们需求是需要显示商品评论,页面设计如下: 调用android代码,对于webview的 ...

  8. UITableViewCell分割线左边部分缺少一些的解决方法

    -(void)viewDidLayoutSubviews {        if ([self.mytableview respondsToSelector:@selector(setSeparato ...

  9. struts2 hello world

    注意:仅需要如下这些jar包,否则启动时会出错 commons-lang3-3.2.jar commons-logging-1.1.3.jarcommons-logging-api-1.1.jarfr ...

  10. 超实用的JavaScript技巧及最佳实践(下)

    1.使用逻辑符号&&或者||进行条件判断 1 2 3 var foo = 10;  foo == 10 && doSomething(); // is the same ...