【HDU 5370】 Tree Maker(卡特兰数+dp)
Tree Maker
Problem DescriptionTree Lover loves trees crazily.One day he invents an interesting game which is named Tree Maker.
In this game, all trees are binary trees.
Initially, there is a tree with only one vertex and a cursor on it. Tree Lover can control the cursor to apply 5 operations to build a tree, and their formats are following:
0 : Jump to the parent of the current vertex.
1 : Jump to the left child of the current vertex.
2 : Jump to the right child of the current vertex.
3 x : Generate a tree with x vertices arbitrarily and make it the left subtree of the current vertex.
4 x : Generate a tree with x vertices arbitrarily and make it the right subtree of the current vertex.
When applying an operation, the log system will log down a record of it.
Tree Lover played this game for a whole day yesterday. As a forgetful man, although Tree Lover knew the shape of the tree while playing, after a sleep he forgot it.
All he has now is the logs of operations.
Tree Lover wants to know: how many possible shapes of the tree can have yesterday according to the logs?
Can you answer this question?
InputThe input consists of multiple test cases.For each test case:
The first line is an integer n (1 <= n <= 500), denoting the lines of logs.
Then follow n lines of logs. The formats of logs are as described above.
The integer x of operation 3 and 4 is positive.
In each case, the number of vertices of the tree will never exceed 500.
You can assume that the cursor will never jump to a non-existent vertex.
If the left child of a vertex exists, operation 3 will not be applied on this vertex, and operation 4 is similar.
OutputFor each test case, ouput a single line “Case #x: y”, where x is the case number, starting from 1, and y is the answer to Tree Lover’s question.Because the answer can be large, please output the answer mod 1000000007.
Sample Input23 34 323 31Sample OutputCase #1: 25Case #2: 5HintBecause the tree is a binary tree, if left and right subtrees of a vertex are of different shapes, after swapping them, the new tree is considered different from the original one.
有一个造二叉树的程序,初始时只有一个节点并且光标指向它,然后进行了n次操作,每次操作属于以下5种操作之一:
- 光标指向当前节点的父亲节点.
- 光标指向当前节点的左儿子.
- 光标指向当前节点的右儿子.
- 随机造一棵包含x个节点的二叉树,把它作为当前节点的左子树.
- 随机造一棵包含x个节点的二叉树,把它作为当前节点的右子树.
给出这n个操作,求在满足所有操作合法的前提下,共有多少种可能的二叉树被造出来,对109+7取模.
1≤n≤500,∑x<500.
【分析】
这是我做的最难的卡特兰数了,然而难在打的方面,真的要想清楚才行啊!!主要是dp部分,卡特兰数是求二叉树形态的。
当只有操作1~3,我们可以准确知道这棵树,就是说我们可以准确知道这棵树的一部分,
然后对于操作4~5,我们知道要插入i棵小树,一共j的点这样的信息。
当要插入一颗大小为k的树时,方案数=卡特兰(k),
然后dp求出用j个节点构成i棵可以为空的子树的方案数f[i][j],然后在树上走一遍然后统计即可。
重点是求当前对于询问能插子树的位置以及未知点的个数。

上图,箭头边x->y表示在x那里有一个插子树操作。(箭头指向位置可以为空,因为那个位置是未知的,插了一颗子树但是没有走到)。
无向边表示没有进行插树操作但是走到了这个点,就是说这是给我们知道的对于未知子树的特别信息,是我们可以确定部分子树形态。
操作每个点记录一个is[x],sum[x],sum[x]表示从x开始,不走箭头边能一共走到的点的个数。
is[x]表示从x开始不走箭头边走到的点的插位个数(就是如果没有左孩子,就有1个插位位置,右孩子也一样)
为什么要这样呢?如上图,1,2都有一个插左子树操作,那么3节点必定不是执行1插子树操作时产生的,2左子树上其他点也是如此,所以不能计入1操作的答案里。
所以如果统计1插左子树操作时,询问一下2的sum[x]和is[x]即可,最后加上dp就好了。
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define LL long long
#define Mod 1000000007
#define Maxn 510 LL p[*Maxn],c[Maxn];
LL f[Maxn][Maxn]; LL qpow(LL x,LL b)
{
LL ans=;
while(b)
{
if(b&) ans=(ans*x)%Mod;
x=(x*x)%Mod;
b>>=;
}
return ans;
} LL get_c(LL n)
{
LL k=qpow(p[n+],Mod-),ans=;
ans=(p[*n]*k)%Mod;
k=qpow(p[n],Mod-);
ans=(ans*k)%Mod;
return ans;
} void init()
{
p[]=;
for(LL i=;i<=*Maxn-;i++) p[i]=(p[i-]*i)%Mod;
for(LL i=;i<=Maxn-;i++) c[i]=get_c(i);
memset(f,,sizeof(f));
f[][]=;
for(LL i=;i<=Maxn-;i++) f[][i]=c[i]; for(LL i=;i<=Maxn-;i++)
for(LL j=;j<=Maxn-;j++)
{
// f[i][j]=0;
for(LL k=;k<=j;k++)
f[i][j]=(f[i][j]+f[i-][j-k]*c[k])%Mod;
} } struct node
{
int x,lc,rc,f;
int sum,is;
bool al,ar;
}t[Maxn];int cnt; struct hp
{
int x,id;
bool q;
}qy[Maxn];int ql; void upd(int x)
{
t[x].is=; t[x].sum=;
t[x].sum+=t[x].al?:t[t[x].lc].sum;
t[x].sum+=t[x].ar?:t[t[x].rc].sum; int y;
y=t[x].lc?t[t[x].lc].is:;
if(t[x].al) y=;
t[x].is+=y; y=t[x].rc?t[t[x].rc].is:;
if(t[x].ar) y=;
t[x].is+=y;
} int n;
bool ffind()
{
int now=;cnt=;ql=;
t[].lc=t[].rc=;
t[].al=t[].ar=;
upd();
bool ok=;
for(int i=;i<=n;i++)
{
int opt;
scanf("%d",&opt);opt++;
if(opt==)
{
if(now==) ok=;
now=t[now].f;
}
else if(opt==)
{
if(!t[now].lc)
{
t[++cnt].f=now;
t[cnt].lc=t[cnt].rc=;
t[cnt].al=t[cnt].ar=;
upd(cnt);
t[now].lc=cnt;
upd(t[cnt].f);
}
now=t[now].lc;
}
else if(opt==)
{
if(!t[now].rc)
{
t[++cnt].f=now;
t[cnt].lc=t[cnt].rc=;
t[cnt].al=t[cnt].ar=;
t[now].rc=cnt;
}
now=t[now].rc;
}
else if(opt==)
{
if(t[now].al||t[now].lc) ok=;
int x;
scanf("%d",&x);
qy[++ql].id=now;qy[ql].x=x;qy[ql].q=;
t[now].al=;
}
else
{
if(t[now].ar||t[now].rc) ok=;
int x;
scanf("%d",&x);
qy[++ql].id=now;qy[ql].x=x;qy[ql].q=;
t[now].ar=;
}
}
return ok;
} void dfs(int x)
{
if(t[x].lc) dfs(t[x].lc);
if(t[x].rc) dfs(t[x].rc);
upd(x);
} LL get_ans()
{
LL ans=;
for(int i=;i<=ql;i++)
{
int now,y=qy[i].x;
if(qy[i].q==)
{
if(t[qy[i].id].lc) now=t[t[qy[i].id].lc].is,y-=t[t[qy[i].id].lc].sum;
else now=;
}
else
{
if(t[qy[i].id].rc) now=t[t[qy[i].id].rc].is,y-=t[t[qy[i].id].rc].sum;
else now=;
}
if(y<) return ;
ans=(ans*f[now][y])%Mod;
}
return ans;
} int main()
{
int T,kase=;
init();
while(scanf("%d",&n)!=EOF)
{
printf("Case #%d: ",++kase);
if(!ffind()) {printf("0\n");continue;}
dfs();
printf("%lld\n",get_ans());
}
return ;
}
[HDU 5370]
2016-09-21 22:12:01
【HDU 5370】 Tree Maker(卡特兰数+dp)的更多相关文章
- HDU 5370 Tree Maker
一个显然的结论是,一棵n个结点的二叉树的形态数,是Catalan数第n项.
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- HDU 5673 Robot【卡特兰数】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题意: 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如 ...
- HDU 5673 Robot ——(卡特兰数)
先推荐一个关于卡特兰数的博客:http://blog.csdn.net/hackbuteer1/article/details/7450250. 卡特兰数一个应用就是,卡特兰数的第n项表示,现在进栈和 ...
- hdu5816 卡特兰数+dp
题意:共n张无中生有,m张攻击牌.每张攻击牌攻击力已知,敌方有p点血.随机洗牌.游戏开始,己方抽取一张手牌,若是无中生有则可再抽两张牌.求能在第一回合内将敌方杀死的概率. n+m <= 20, ...
- HDU 4828 Grids(卡特兰数+乘法逆元)
首先我按着我的理解说一下它为什么是卡特兰数,首先卡特兰数有一个很典型的应用就是求1~N个自然数出栈情况的种类数.而这里正好就对应了这种情况.我们要满足题目中给的条件,数字应该是从小到大放置的,1肯定在 ...
- hdu 1130How Many Trees?(卡特兰数)
卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列. 以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)的名字来命名,其前几项为(从第零 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- HDU 5293 Tree chain problem 树形DP
题意: 给出一棵\(n\)个节点的树和\(m\)条链,每条链有一个权值. 从中选出若干条链,两两不相交,并且使得权值之和最大. 分析: 题解 #include <cstdio> #incl ...
随机推荐
- 记录一次centos升级gblic的教训
前些天公司项目需要对上传的图片打水印,前端采用angularjs,后端nodejs,网上一搜,找到了一个images的库,在本地Windows环境下试了下还不错,然后就上传测试服务器(centos6. ...
- win8 64位操作系统 Microsoft Visual Studio 2010在IIS上调试 “此任务要求应用程序具有提升的权限”等问题
很少在IIS上调试程序,因系统原因,所以不得不在IIS上预览项目和调试项目(因为只能在IIS上预览项目才能看到项目里的数据). 1.附加到进程(注意附加到进程前必须预览项目) 2.选择调试项 需要注意 ...
- Asp.Net MVC--Controller激活2
在使用MVC项目中,如果激活控制器,则就会向前台返回action执行的结果. 很多时候,根据需求,手动激活控制器来向客户端返回结果. 一.激活实例代码1 这是在Global文件中使用 var rout ...
- 如何注册ActiveX打印控件
一.看系统是32位还是64位的.(以64位为例) 二.先找到你的wfPrint.OCX文件所在路径 三.找到SysWOW64所在的命令控制符 四.最后在该cmd下注册 就可以了.
- Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...
- Python内存管理及引用计数
作为一门动态语言,python很重要的一个概念就是动态类型,即对象的类型和内存占用都是运行时确定的.(Why?)运行时,解释器会根据语法和右操作数来决定新对象的类型.动态类型的实现,是通过引用和对象的 ...
- JS对于字符串的切割截取
对于字符串的切割截取平时所用可能不是特别多,而且分的比较细,所以自备自查.有备无患. 由于之前所有均在一个demo测试,若是哪里打错了,敬请谅解.一些其余属性找时间继续添加. 1.函数:split() ...
- javascript 事件流及应用
当页面元素触发事件的时候,该元素的容器以及整个页面都会按照特定顺序发生该元素的触发 事件,事件传播的顺序叫做事件流 1.事件流的分类: A.冒泡型事件(所有浏览器都支持) 由明确的事件源到最不确定 ...
- The test form is only available for requests from the local machine 解决方法
protocolsdocumentationsoapweb 当您尝试从远程计算机访问 Web 服务时,不会显示“调用”按钮.并且,您会收到以下错误信息: The test form is only ...
- 使用远程链接数据库工具无法链接到 linxu 系统上的数据库配置 1045
1.远程连接上Linux系统,确保Linux系统已经安装上了MySQL数据库.登陆数据库.mysql -uroot -p(密码). 2. 创建用户用来远程连接 GRANT ALL PRIVILEGES ...