hdu 5379 Mahjong tree dfs
2015-08-11 18:06
183 查看
Mahjong tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 455 Accepted Submission(s): 143
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
题意:给定一棵树,排序号,使得每个节点的儿子们是连续的,每颗子树是连续的,求方案数
从根节点开始,,每个节点如果有儿子有节点,就会有两种选择,一个是当前最大的数,一个是当前存留最小的数,其他没有儿子的节点由于是按顺序排列的,所以会对结果造成n!的影响,dfs一遍,另外如果有一个节点有多于两个节点有儿子,那是不合法的,只有一个节点是1
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <stdio.h> #include <algorithm> #include <stdlib.h> #include <string.h> #include <math.h> #include <vector> #include <queue> #include <stack> #include <map> #include <set> using namespace std; #define Max 100010 const int MOD = 1e9+7; vector <int > q[Max]; int vis[Max]; long long res=1; int flag=1; int n; void dfs(int a) { int k=q[a].size(); int cnt=0; vis[a]=1; for(int i=0;i<k;i++) { if(vis[q[a][i]]) continue; if(q[q[a][i]].size()==1&&vis[q[a][i]]==0) cnt++; else dfs(q[a][i]); } if(a!=1)k--; if(k-cnt>2){flag=0;return ;} if(cnt!=k) { res*=2; res%=MOD; } //cout<<k<<" "<<cnt<<" "; while(cnt>0) { res*=cnt;res%=MOD;cnt--; } //cout<<a<<" "<<res<<endl; //cout<<res<<endl; } int main() { int T; cin>>T; int _case=0; while(T--) { cin>>n; flag=1; for(int i=1;i<=n;i++) q[i].clear(); memset(vis,0,sizeof(vis)); int a,b; for(int i=1;i<n;i++) { scanf("%d %d",&a,&b); q[b].push_back(a); q[a].push_back(b); } res=2; if(n!=1) dfs(1); else res=1; if(!flag) res=0; cout<<"Case #"<<++_case<<": "<<res%MOD<<endl; } return 0; }
相关文章推荐
- 【读书笔记】iOS-开发技巧-三种收起键盘的方法
- Cool Android Apis 整理(一)
- 计算从A地出发到各个地方的路径及距离
- 关于mac book 的启动选项
- kernel_task high cpu usage
- Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数
- How Android Draws Views(Android 视图绘制机制)
- Android开发中遇到的exception
- 最简单的android annotation demo
- BZOJ 4216 Pig 分块乱搞
- HTTP协议学习,post于get;用Fiddler测试请求
- W3C标准
- 【读书笔记】iOS-开发技巧-三种收起键盘的方法
- Corel VideoStudio Pro X7(会声会影)
- 使用Chef自动化部署框架的好处
- iOS 打包静态库
- Hdu 1875 畅通工程再续【最小生成树】
- java 让程序定时运行
- 总结eclipse中安装maven插件
- 真机与模拟器有区别(录音)