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)
Total Submission(s): 388 Accepted Submission(s): 125
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.
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = ;
typedef long long LL;
const LL mod = 1e9 + ;
vector<int>g[maxn];
int son[maxn];
LL fac[maxn] = {};
LL dfs(int u,int fa){
son[u] = ;
LL ret = ;
int a = ,b = ;
for(int i = g[u].size()-; i >= ; --i){
if(g[u][i] == fa) continue;
ret = ret*dfs(g[u][i],u)%mod;
if(son[g[u][i]] > ) b++;
else a++;
if(!ret || b > ) return ;
son[u] += son[g[u][i]];
}
if(b) ret = ret*%mod;
ret = ret*fac[a]%mod;
return ret;
}
void init(){
for(int i = ; i < maxn; ++i)
fac[i] = i*fac[i-]%mod;
}
int main(){
int kase,n,u,v,cs = ;
init();
scanf("%d",&kase);
while(kase--){
scanf("%d",&n);
for(int i = ; i < maxn; ++i) {g[i].clear();son[i] = ;}
for(int i = ; i < n; ++i){
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
LL ret = dfs(,-);
if(son[] > ) ret = ret*%mod;
printf("Case #%d: %I64d\n",cs++,ret);
}
return ;
}
2015 Multi-University Training Contest 7 hdu 5379 Mahjong tree的更多相关文章
- 2015多校第7场 HDU 5379 Mahjong tree 构造,DFS
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的 ...
- Hdu 5379 Mahjong tree (dfs + 组合数)
题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点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(树的遍历&组合数学)
本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...
- HDU 5379 Mahjong tree
题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...
- 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 dfs+组合数学
题意:给你一棵树来分配号码,要求是兄弟节点连续并且每一棵子树连续. 思路:因为要求兄弟和子树都是连续的,所以自己打下草稿就可以发现如果一个节点有3个或3个以上的非叶子结点,那么就无论如何也不能达到目的 ...
- 2015 Multi-University Training Contest 8 hdu 5390 tree
tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...
- 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!
Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: ...
随机推荐
- 如何用Vim提高开发效率
即可 ●输入m获取到文章目录 推荐↓↓↓ C/C++编程 更多推荐<18个技术类公众微信> 涵盖:程序人生.算法与数据结构.黑客技术与网络安全.大数据技术.前端开发.Java.Python ...
- 01springMVC入门
1 MVC模式回顾 Spring MVC是一种基于MVC的Web应用框架. MVC是一种设计模式,MVC在b/s系统下的应用: 执行步骤: 发出请求,请求到MVC当中的C,C接收请求后并不能 ...
- 如何打开WCF测试客户端
- 用户体验之如何优化你的APP
用户体验,速度为王,来几个优化APP“速度”的建议. 1.后台执行 毋庸多言,已是通常做法. 一般在执行下载任务时让其在后台运营,让用户有精力去做别的事情. 后端加载 2.提前显示 客户端与WEB的数 ...
- poi判断一行是隐藏的getZeroHeight()
poi判断一行是隐藏的 getZeroHeight() boolean isZeroHeight = row.getZeroHeight(); if(isZeroHeight){ // 如果为隐藏行就 ...
- Ruby按照换行符进行分割
Ruby按照换行符进行分割 string.split(/\n/)
- Effective JavaScript Item 22 使用arguments来创建接受可变參数列表的函数
本系列作为Effective JavaScript的读书笔记. 在Item 21中,介绍了结合apply方法实现的可变參数列表函数average,它实际上仅仅声明了一个数组作为參数,可是利用apply ...
- 插入排序和冒泡排序(Swift版本)
插入排序(只记录代码不作详细分析) 插入排序将输入的元素一个个插入到已排序的队列里,对比是由后往前 代码: func insertSort<Elem: Comparable>(_ arr: ...
- oracle故障处理之删除大表空间hang住
背景 数据库分区表数据越来越大,需要对过期话的数据进行迁移,以及大的分区表需要进行数据的清理和删除,达到释放磁盘空间的目的. 问题说明 环境:linux 6.X 数据库:oracle 11.2.0.4 ...
- SVN——Jenkins自动发布
最近公司项目处于开发阶段,很多功能开发完后就需要发布到测试环境等待测试去验收,这个时候如果手动更新网站的话,是很费时费力的. 于是乎,我们做成了自动发布,这样我们只管提交代码到SVN就行了,发布由软件 ...