【HDU】5370 Tree Maker 【树dp】
传送门:【HDU】5370 Tree Maker
my code:my~~code:
#include <bits/stdc++.h>
using namespace std ;typedef long long LL ;#define clr( a , x ) memset ( a , x , sizeof a )const int MAXN = 505 ;
const int mod = 1e9 + 7 ;int G[MAXN][2] , cnt ;
int f[MAXN] ;
int root[MAXN] , idx ;
int is[MAXN] ;
int num[MAXN] ;
int dp[MAXN] , dp2[MAXN][MAXN] ;
int n , cur ;void upd ( int& x , int y ) {x += y ;if ( x >= mod ) x -= mod ;
}void preprocess () {dp[0] = dp[1] = 1 ;for ( int i = 2 ; i < MAXN ; ++ i ) {for ( int j = 0 ; j < i ; ++ j ) {upd ( dp[i] , ( LL ) dp[j] * dp[i - 1 - j] % mod ) ;}}
}void dfs ( int u , int x ) {int l = G[u][0] , r = G[u][1] ;if ( is[u] == is[l] ) dfs ( l , x ) ;if ( is[u] == is[r] ) dfs ( r , x ) ;if ( is[l] != is[u] && is[r] != is[u] ) {if ( !l && !r ) {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = dp[i + 1] ;}} else if ( !l ) {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = dp[i] ;}} else if ( !r ) {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = dp[i] ;}} else {dp2[u][0] = 1 ;for ( int i = 1 ; i <= x ; ++ i ) {dp2[u][i] = 0 ;}}} else if ( is[u] != is[l] ) {if ( !l ) {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = 0 ;for ( int j = 0 ; j <= i ; ++ j ) {upd ( dp2[u][i] , ( LL ) dp[j] * dp2[r][i - j] % mod ) ;}}} else {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = dp2[r][i] ;}}} else if ( is[u] != is[r] ) {if ( !r ) {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = 0 ;for ( int j = 0 ; j <= i ; ++ j ) {upd ( dp2[u][i] , ( LL ) dp[j] * dp2[l][i - j] % mod ) ;}}} else {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = dp2[l][i] ;}}} else {for ( int i = 0 ; i <= x ; ++ i ) {dp2[u][i] = 0 ;for ( int j = 0 ; j <= i ; ++ j ) {upd ( dp2[u][i] , ( LL ) dp2[l][j] * dp2[r][i - j] % mod ) ;}}}
}int newnode ( int x ) {++ cnt ;G[cnt][0] = G[cnt][1] = 0 ;f[cnt] = cur ;is[cnt] = x ;return cnt ;
}void solve () {int op , x ;idx = cnt = 0 ;cur = 1 ;newnode ( 0 ) ;for ( int i = 1 ; i <= n ; ++ i ) {scanf ( "%d" , &op ) ;if ( op == 0 ) cur = f[cur] ;else if ( op <= 2 ) {if ( !G[cur][op - 1] ) {G[cur][op - 1] = newnode ( is[cur] ) ;num[is[cur]] -- ;}cur = G[cur][op - 1] ;} else {scanf ( "%d" , &x ) ;G[cur][op - 3] = newnode ( ++ idx ) ;root[idx] = cnt ;num[idx] = x - 1 ;}//printf ( "%d: %d %d\n" , i , cur , num[is[cur]] ) ;}int ans = 1 ;for ( int i = 1 ; i <= idx ; ++ i ) if ( num[i] ) {//printf ( "%d %d\n" , root[i] , num[i] ) ;dfs ( root[i] , num[i] ) ;ans = ( LL ) ans * dp2[root[i]][num[i]] % mod ;}printf ( "%d\n" , ans ) ;
}int main () {int cas = 0 ;preprocess () ;while ( ~scanf ( "%d" , &n ) ) {printf ( "Case #%d: " , ++ cas ) ;solve () ;}return 0 ;
}
【HDU】5370 Tree Maker 【树dp】相关推荐
- hdu 5370 Tree Maker(catalan+dp)
题目链接:hdu 5370 Tree Maker n个节点的二叉树种类为Catalan数的第n项 对于一棵子树而言,被移动过的节点就是确定的位置,所以只要知道已经确定位置的K个节点有多少个空孩子指针M ...
- HDU - 5370 Tree Maker dp
HDU - 5370 f[ i ] 表示顶点数为 i 的二叉树的种数, 其实就是卡特兰数. dp[ i ][ j ] 表示 i 个顶点分给 j 个二叉树的方案数, 这个是个 n ^ 3 的dp. 然 ...
- HDU 3534 Tree (经典树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534 题意: 给你一棵树,问你有多少对点的距离等于树的直径. 思路: dp[i][0]表示在i的子树中 ...
- HDU.5909.Tree Cutting(树形DP FWT/点分治)
题目链接 \(Description\) 给定一棵树,每个点有权值,在\([0,m-1]\)之间.求异或和为\(0,1,...,m-1\)的非空连通块各有多少个. \(n\leq 1000,m\leq ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- CodeForces - 1557D Ezzat and Grid(线段树+dp)
题目链接:点击查看 题目大意:给出 nnn 个 010101 串,现在问最少需要删掉多少个串,才能使得剩下的串拼起来是连通的 规定两个 010101 串是连通的,当且仅当存在至少一列,在两个串中都为 ...
- Codeforces1153D-Serval and Rooted Tree(树形dp)
原题链接:http://codeforces.com/problemset/problem/1153/D 题目原文: D. Serval and Rooted Tree time limit per ...
- HDU 2795 Billboard (线段树+贪心)
HDU 2795 Billboard (线段树+贪心) 手动博客搬家:本文发表于20170822 21:30:17, 原地址https://blog.csdn.net/suncongbo/articl ...
- C++SEG TREE线段树的实现算法(附完整源码)
C++SEG TREE线段树的实现算法( C++SEG TREE线段树的实现算法(完整源码(定义,实现,main函数测试) C++SEG TREE线段树的实现算法(完整源码(定义,实现,main函数测 ...
最新文章
- Python使用pyserial进行串口通信
- 大物实验计算弹性模量_普渡大学amp;橡树岭国家实验室IPJ:强度高达2.4GPa,双相纳米复合结构助力铝合金性能大幅提高!...
- 云原生中间件与开源自建TCO对比
- Linux就业技术指导(一):简历撰写及面试筹备要领
- emacs python ide_Emacs Python IDE win7 x64
- 让Windows7 x64 IIS7支持32位应用程序
- 如何用excle制作黑人拉馆_小编教你如何用装机吧制作u盘启动盘
- window下Ionic环境安装
- java泛型范围_Java泛型类型中的通配符参数在其范围内的正式条件是什么?
- 使用Volley+OkHttp+Gson加速Android网络开发
- 网络安全--通过握手包破解WiFi(详细教程)
- 2021年中国DevOps现状调查报告发布!
- MAGENTO for XAMPP install config -搬家配置与安装配置
- mysql initialize 什么意思_mysql initialize
- 一个选手和小丫姐的搞笑对话
- 系统函数,频率响应定义
- Paddle-NEAT——飞桨进化神经网络组件
- 《PTA——拼题A》之第1016题
- isset php 二维数组_php 一维数组按条件转换成二维数组
- 如何让自己做任何事情都很有条理?适合规划工作计划的清单便签