HDU 5379 Mahjong tree(dfs)
题目链接: pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379
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.
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
Case #1: 32
Case #2: 16
题意:
一颗树共同拥有 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 <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define LL long long
#define maxn 100017
const LL mod = 1e9+7;
vector<int> 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;
}
HDU 5379 Mahjong tree(dfs)的更多相关文章
- Hdu 5379 Mahjong tree (dfs + 组合数)
题目链接: Hdu 5379 Mahjong tree 题目描述: 给出一个有n个节点的树,以节点1为根节点.问在满足兄弟节点连续 以及 子树包含节点连续 的条件下,有多少种编号方案给树上的n个点编号 ...
- HDU 5379 Mahjong tree dfs+组合数学
题意:给你一棵树来分配号码,要求是兄弟节点连续并且每一棵子树连续. 思路:因为要求兄弟和子树都是连续的,所以自己打下草稿就可以发现如果一个节点有3个或3个以上的非叶子结点,那么就无论如何也不能达到目的 ...
- HDU 5379 Mahjong tree(树的遍历&组合数学)
本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...
- HDU 5379——Mahjong tree——————【搜索】
Mahjong tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 ...
- 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
题意:在一棵有n个节点的树上放编号从1到n的麻将,要求每个点的儿子节点之间的编号连续,每棵子树内的编号连续. 解法:手推一组样例之后就可以得到如下结论然后从根节点一边讨论一边搜就好了. 当一个节点只有 ...
- HDU 6035 Colorful Tree(dfs)
题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...
- [hdu5379 Mahjong tree]dfs计数
题意:给n个节点的树编号1-n,一个节点唯一对应一种编号,要求编完号的树满足如下性质:所有节点的儿子的编号是连续的,对一棵子树,它包含的所有节点的编号也是连续的.连续的意思是把所有数排序后是一段连续的 ...
随机推荐
- [ 总结 ] nginx 编译参数中文详解
贴出来,方便查找和学习. nginx 编译参数: --prefix=PATH 指向安装目录--sbin-path=PATH 指向(执行)程序文件--conf-path=PATH 指向 ...
- background-clip,origin属性
background-clip是新增属性之一,其作用是确定背景的裁剪区域. background-clip语法: background-clip:margin-box | padding-box | ...
- java设计模式之实现对象池模式示例分享
http://www.jb51.net/article/46941.htm 对象池模式经常用在频繁创建.销毁对象,且对象创建.销毁开销很大的场景,比如数据库连接池.线程池.任务队列池等.本代码简单,没 ...
- 【转】C#获取当前程序运行路径的方法集合
//获取当前进程的完整路径,包含文件名(进程名).string str = this.GetType().Assembly.Location;result: X:\xxx\xxx\xxx.exe (. ...
- 跳转的两种实现方法setInterval和setTimeout
setInterval方法: <html> <head> <meta http-equiv="Content-Type" content=" ...
- flutter ui
快速生成无状态模板 void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget ...
- hdu5558
hdu5558 题意 给出一个字符串,按照特殊规则进行加密. 假设已经加密了前 \(i\) 个字符,从第 \(i+1\) 个字符开始找到 \(S[i..N]\) 的长度为 \(K\) 的最长前缀等于 ...
- 简单工厂模式(Factory)
设计模式序言:这是开始学习设计模式的第一步,也是相对简单的一个设计模式,简单工厂设计模式. 简单工厂就是将业务进行封装,减少代码之间的耦合度,使代码更易于扩展和维护. 以下是简单工厂设计模式的图解: ...
- 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster
很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...
- 【MySQL笔记】MySql5安装图解教程
MySql5.6Window超详细安装教程 2015-06-23 0个评论 来源:林炳文Evankaka的专栏 收藏 我要投稿 一.安装包准备 1.下载MySql5.6 ht ...