博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5379 Mahjong tree(dfs)
阅读量:6857 次
发布时间:2019-06-26

本文共 3579 字,大约阅读时间需要 11 分钟。

题目链接:

pid=5379

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
 

Source

题意:

一颗树共同拥有 n 个节点,现要把他们从 1 - n 编号。

条件:

1、每一个节点的子节点之间号是连续的(如:4 5 6 或者6 5 4均可)!

2、每棵子树的编号自身是连续的!

求一共同拥有多少种可能的编号方式。

PS:

假设一个节点的子树中规模大于1的子树多于2个。那么肯定是不可能有满足要求的安排编号的!

在一个区间中。规模大于1的子树必定选择的是最左边或者最右边的连续的编号。

剩下规模为1的子树,有N!种安排方法。

手动扩栈,须要用C++提交!

代码例如以下:

#pragma comment(linker, "/STACK:102400000,102400000")#include 
#include
#include
#include
using namespace std;#define LL long long#define maxn 100017const LL mod = 1e9+7;vector
v[maxn];LL f[maxn];LL son[maxn];void init(){ f[0] = 1; for(int i = 1; i <= maxn; i++) { f[i] = (i*f[i-1])%mod; }}LL dfs(int u, int father){ son[u] = 1; int x = 0, y = 0; LL ans = 1; int num = v[u].size(); for(int i = 0; i < num; i++) { if(v[u][i] == father) { continue; } int t1 = v[u][i]; LL tt = dfs(t1, u); ans = (ans*tt)%mod; if(son[v[u][i]] >= 2)//规模大于1的子树 { x++; } else { y++; } if(ans==0 || x > 2)//ans==0无解,规模大于1的子树多于2个肯定不可能 { return 0; } son[u]+=son[v[u][i]]; } if(x) { ans*=2; ans%=mod; } ans = (ans*f[y])%mod; return ans;}int main(){ int t; int n; int cas = 0; init(); scanf("%d",&t); while(t--) { memset(son,0,sizeof(son)); scanf("%d",&n); for(int i = 0; i < maxn; i++) { v[i].clear(); } int x, y; for(int i = 1; i < n; i++) { scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } LL ans = dfs(1, -1); if(son[1] > 1) { ans*=2; ans%=mod; } printf("Case #%d: %I64d\n",++cas,ans); } return 0;}

转载于:https://www.cnblogs.com/clnchanpin/p/6790348.html

你可能感兴趣的文章
Linux 命令详解(三)./configure、make、make install 命令
查看>>
分享Kali Linux 2017年第31周镜像文件
查看>>
03-老马jQuery教程-DOM操作
查看>>
mongodb09----replicattion set--健壮性
查看>>
sql中的笛卡尔积
查看>>
C Array length function problem - C / C++
查看>>
ASP.NET中26个常用性能优化方法
查看>>
Objective-C利用协议实现回调函数
查看>>
angularjs的$window功能小练习
查看>>
Bayesian generalized linear model (GLM) | 贝叶斯广义线性回归实例
查看>>
Pulsar-Producer实现简介
查看>>
<转载>构造函数与拷贝构造函数
查看>>
[转]K近邻算法
查看>>
在Fedora8上的Tomcat上deploy一个war
查看>>
Swing的GUI组件得到焦点
查看>>
Google开源机器学习工作流Kubeflow Pipelines,推出AI Hub
查看>>
构建一个运行在Azure虚拟机上的MySQL Spring Boot应用程序
查看>>
网易数据基础平台建设经验谈
查看>>
从起步到爆发,UPYUN云CDN架构演进之路
查看>>
NGINX应用性能优化指南(第五部分):吞吐量
查看>>