DP---Mahjong tree
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.
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.
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
Case #2: 16
#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的更多相关文章
- HDU 5379 树形DP Mahjong tree
任意一棵子树上节点的编号连续,每个节点的所有二字节点连续,求编号方案的总数. 稍微分析一下可知 每个节点的非叶子节点个数不能多于两个,否则这个子树无解,从而整棵树都无解. 每棵子树将所有节点按照编号从 ...
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- 【HDOJ 5379】 Mahjong tree
[HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共同拥有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根 ...
- HDU 5379 Mahjong tree(dfs)
题目链接:pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little su ...
- HDU 5379——Mahjong tree——————【搜索】
Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5379 Mahjong tree(树的遍历&组合数学)
本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...
- Hdu 5379 Mahjong tree (dfs + 组合数)
题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...
- 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 ...
- Mahjong tree (hdu 5379 dfs)
Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- DP Intro - Tree DP Examples
因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...
随机推荐
- Sql中的Merge和output
先看merge, 不用merge时: --更新 update TA Value ) --插入没有的数据 insert into TA ,,Value from TB ) and TypeName=@v ...
- Spring源码追踪4——SpringMVC View解析
这次的议题是返回json和返回普通view经过的路线差异. ---------------------------------------------------------------------- ...
- 10个你必须掌握的超酷VI命令技巧
摘要:大部分Linux开发者对vi命令相当熟悉,可是遗憾的是,大部分开发者都只能掌握一些最常用的Linux vi命令,下面介绍的10个vi命令虽然很多不为人知,但是在实际应用中又能让你大大提高效率. ...
- SQL调优 - Hints指定索引 解决慢查询案例
背景 每当交易高峰时期,可能会暴露一些平时无法发现的问题,机遇和挑战并存.下面聊聊最近解决的一个案例,因为执行计划走错导致慢查询,进而引发应用线程阻塞.线程池爆满,最后应用功能瘫痪.如何标本兼治的解决 ...
- IE11兼容性设定
增加browser文件,如下: <browsers> <browser refID="Default"> <capabilities&g ...
- 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 > ...
- android html 与webview属性从冲突
在最近的项目开发中,使用webview加载html页面,这样可以节省大量页面开发的时间,同时也可加快项目进度. 我们需求是需要显示商品评论,页面设计如下: 调用android代码,对于webview的 ...
- UITableViewCell分割线左边部分缺少一些的解决方法
-(void)viewDidLayoutSubviews { if ([self.mytableview respondsToSelector:@selector(setSeparato ...
- struts2 hello world
注意:仅需要如下这些jar包,否则启动时会出错 commons-lang3-3.2.jar commons-logging-1.1.3.jarcommons-logging-api-1.1.jarfr ...
- 超实用的JavaScript技巧及最佳实践(下)
1.使用逻辑符号&&或者||进行条件判断 1 2 3 var foo = 10; foo == 10 && doSomething(); // is the same ...