Mahjong tree

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

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
 
 
题目大意:给出t表示t组测试数据。给出n,表示一棵树有n个顶点,下面有n-1条边的关系(没有明确给出谁到谁,错了好多次,以为u->v就是v为双亲u为儿子,然后建图错了)。让给树的各个结点编上号,从1->n。并且要求各个结点的所有儿子的编号是连续的,且子树的所有结点的编号也是连续的一段。问一共有多少种编号方法。
 
解题思路:题解中给出带有儿子的子节点的个数最多只能是两个。原因就是如果子节点带有儿子,那么就需要一段连续的数字,如果这段数字处在目前剩余的一段数字中间的话,那么必然不能让所有的子节点是连续的,所以如果有两个子节点是带有儿子的,那么这两个子节点应该分得目前所剩一段数字的两侧。如第二组样例:根的编号为1。则剩下数字2 3 4 5 6 7 8。现在子节点带有儿子的有两个,那么就应该让有三个子孙的结点3分得2,3,4,5。有一个子孙的结点5分得7,8。或者是让结点3分得5,6,7,8。让结点5分得2,3。可以看出,有两个结点带有儿子时,就只能让他们分在剩下数字的两端,如果超过两个结点带有儿子,就不能满足题意了。
 
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int maxn=1e6+200;
vector<int>G[maxn];
const int MOD=1e9+7;
typedef long long INT;
#pragma comment(linker,"/STACK:1024000000,1024000000")  //加上扩栈,同时语言选C++,不然爆栈
INT fact(int x){
if(x==0)
return 1;
INT ret=1;
for(int i=2;i<=x;i++){
ret=((ret%MOD)*(i%MOD))%MOD;
}
return ret;
}
INT dfs(int u,int fa){ int lef=0,ulef=0; //子节点是叶子的个数,子节点不是叶子的个数
int v;
INT tmp=0 , ret=1; //ret保存方案数
for(int i=0;i<G[u].size();i++){
v=G[u][i];
if(v==fa)
continue;
if(G[v].size()==1){
lef++;
}else{
ulef++;
tmp=dfs(v,u);
if(tmp==-1)
return -1;
ret=((ret%MOD)*(tmp%MOD))%MOD;
}
}
if(ulef<2){ //如果非叶子子节点个数小于2,叶子节点全排列,树根可以为最大值或最小值(之所以乘2)
return ((2*ret%MOD)*(fact(lef)%MOD))%MOD;
}else if(ulef==2){ //如果非叶子子节点等于2,只能让叶子节点全排列
return ((ret%MOD)*(fact(lef)%MOD))%MOD;
}else return -1; //不合法
}
int main(){
int t,n,u,v,cnt=0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
if(n==1){
printf("Case #%d: 1\n",++cnt);
continue;
}
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
G[v].push_back(u);
G[u].push_back(v);
}
INT ans=dfs(1,-1);
if(ans==-1)
ans=0;
printf("Case #%d: %lld\n",++cnt,ans);
for(int i=0;i<=n;i++)
G[i].clear();
}
return 0;
}

  

 
 
 

HDU 5379——Mahjong tree——————【搜索】的更多相关文章

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

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

  2. HDU 5379 Mahjong tree(dfs)

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

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

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

  4. 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 ...

  5. 2015多校第7场 HDU 5379 Mahjong tree 构造,DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5379 题意:一颗n个节点n-1条边的树,现在要给每个节点标号(1~n),要求:(1)每一层的兄弟节点的 ...

  6. HDU 5379 Mahjong tree

    题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...

  7. HDU 5379 Mahjong tree dfs+组合数学

    题意:给你一棵树来分配号码,要求是兄弟节点连续并且每一棵子树连续. 思路:因为要求兄弟和子树都是连续的,所以自己打下草稿就可以发现如果一个节点有3个或3个以上的非叶子结点,那么就无论如何也不能达到目的 ...

  8. HDU - 4431 Mahjong (模拟+搜索+哈希+中途相遇)

    题目链接 基本思路:最理想的方法是预处理处所有胡牌的状态的哈希值,然后对于每组输入,枚举每种新加入的牌,然后用哈希检验是否满足胡牌的条件.然而不幸的是,由于胡牌的状态数过多(4个眼+一对将),预处理的 ...

  9. Mahjong tree (hdu 5379 dfs)

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

随机推荐

  1. Transfer data to SQL Server from SPC-Light with Excel macros

    公司的QA检测软件SPC-Light,需要从其中读取一些信息至SQL Server数据库,储存或是做其它分析. 先是在Excel的VBE的工具中,引入一个组件Microsoft ActiveX Dat ...

  2. framwork maven的配置及使用

    maven的配置及使用 一.什么是maven: 我们在开发项目的过程中,会使用一些开源框架.第三方的工具等等,这些都是以jar包的方式被项目所引用,并且有些jar包还会依赖其他的jar包,我们同样需要 ...

  3. python3中模块初识

    python的模块使用方法 1.用于显示python的环境变量 import sys print(sys.path) 运行路径执行结果如下: ['F:\\codes', 'F:\\codes', 'C ...

  4. 【OD深入学习】Java多线程面试题

    一.参考文章 1. Java线程面试题 Top 50 2. Java面试——多线程面试题 3. JAVA多线程和并发基础面试问答 4. 15个顶级Java多线程面试题及回答 二.逐个解答 三.一语中的 ...

  5. 【bzoj1030】: [JSOI2007]文本生成器 字符串-AC自动机-DP

    [bzoj1030]: [JSOI2007]文本生成器 首先把匹配任意一个的个数的问题转化为总个数-没有一个匹配的个数 先构造AC自动机,然后枚举每一位的字母以及在自动机上的位置 f[i][j]为第i ...

  6. 51nod1244 莫比乌斯函数之和(杜教筛)

    题面 传送门 题解 我--我忘记把预处理的块的大小调成\(n^{\frac{2}{3}}\)了--(仰天) 首先\(\mu*1=e\) 然后杜教筛就行了 //minamoto #include< ...

  7. P2723 丑数 Humble Numbers

    题意:给你k个质数,定义丑数集合为k个质数随机(1--k)个相乘得到的数 求第n小的丑数 暴力...貌似不太可行,(把所有大量丑数求出来,sort   QAQ) 可以想到,对于第i个丑数f[i],它一 ...

  8. Color Length UVA - 1625

    题目大意: 给你两个字符串p,q,字符串中每个字符代表一个颜色,现在按顺序合并两个字符串,得到一个新字符串.新字符串的价值为,每个颜色价值的和,单个颜色价值的和等于该颜色在新字符中最后一次出现的位置减 ...

  9. Nginx 基本 安装..

    ubuntu 下 Nginx是高度自由化的Web服务器,它的功能是由许多模块来支持.如果使用了某个模块,这个模块使用了一些类似zlib或OpenSSL等的第三方库,那么就必须先安装这些软件.Ubunt ...

  10. spring 配置 junit

    package cn.hefen.mall.app; import cn.hefen.mall.app.model.ResultMap; import cn.hefen.mall.app.model. ...