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 题意:给你一棵树 ...
随机推荐
- C++14 make code cleaner
在C++11中我们如果要写一个通过tuple实现函数调用的函数要这样写: template<int...> struct IndexTuple{}; template<int N, ...
- [原]quick2.25精灵变灰
由于quick2.25没有导出shader相应的接口,所以2.25无法直接使用shader. 本文简单介绍如何导出相应接口,同时教大家使用shader 实现精灵变灰 一.编写静态函数,以供导出使用(直 ...
- Castle.ActiveRecord (V3.0.0.130)
为项目添加 Castle.ActiveRecord 的引用: 安装成功后,查看项目的引用如图: 配置文件 App.Config (MySQL) <?xml version="1.0&q ...
- Workday为何迟迟不进入中国
全球知名HRM SaaS厂商Workday在世界各地攻城拔寨,俨然是HR SaaS的代名词,更是HRM市场的领导品牌.但是却单单在中国市场悄无声息,除了为数不多的海尔海外.联想海外等规模客户和部分ro ...
- Asus ubuntu Fn恢复
sudo sed 's/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"/GRUB_CMDLINE_LINUX_DEFAULT="qui ...
- Android开源代码分享
一.AppMsg实现自定义Toast. github下载地址 二.CircleImageView实现带边框圆形头像. github下载地址 ...
- 你还在问android横竖屏切换的生命周期?
本文原创,转载请注明来自xiaoQLuhttp://www.cnblogs.com/xiaoQLu/p/3324503.html 开源帮助android获得了飞速的发展,开源也导致了数不清的碎片问题. ...
- SQL Server 问题 1 - SQL Server encountered error 0x80070422/0x8007042d
今天执行SQL Server 2014的full-text search 查询操作:select * from table where contains(summary, 'smith') 报出如下错 ...
- Centos用yum升级mysql到(5.5.37)
原文:http://www.if-not-true-then-false.com/2010/install-mysql-on-fedora-centos-red-hat-rhel/ 1. Change ...
- new/delete和malloc/free的区别
通俗易懂版本:http://zhidao.baidu.com/question/86185100 1 new/delete和malloc/free最大区别是对对象的理解. 如果你使用 Foo* foo ...