<html>

Source: Internet
Author: User
Tags cas

pid=5370 "> topics link: hdu 5370 Tree Maker


The two-fork tree of n nodes is the nth item of Catalan number

For a subtrees tree, the moved node is a definite Location. So just to know the number of K nodes that have been determined where there is an empty child pointer m, and the node in the N indeterminate position under that subtree equals to the number of species of the m-nullable subtree constructed with n nodes. The number of shapes for the whole tree is the product of several individual subtree morphology numbers.

Define dp[i][j] to construct the number of forms of J tree with I Nodes. dp[i][j] = sum{dp[i-1][j-k] * catalan[k] | 0≤k≤j}, The DP array is preprocessed with the complexity of O (n^3). Then the simulation operation calculates the M and N of each Subtree.


#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;typedef long ll;  const int MAXN = 505;const int mod = 1e9 + 7;int dp[maxn][maxn], catalan[maxn];void Preserve () {catalan[0] = catalan[1] = 1;for (int i = 2; i <=; i++) {for (int j = 0; J < i; j + +) catalan[i] = (catalan[i] + (ll) catalan[j] * catalan[i -j-1])% mod;}  dp[0][0] = 1;for (int i = 1; i <=; i++) {for (int j = 0; J <=; J + +) {for (int k = 0; k <= j; k++) dp[i][j] = (dp[i][j] + (ll) dp[i-1][j-k] * catalan[k])% mod;}}} int N, M, far[maxn], son[maxn][2], idx[maxn], cnt[maxn], sum[maxn];inline int newNode (int F) {m++;cnt[m] = son[m][0] = So n[m][1] = 0;far[m] = F;return M;} void init () {M = 0;int u = newNode (1), t, k;idx[u] = m;for (int i = 1; i <= N; i++) {scanf ("%d", &t); if (t = = 0) u = Far[u];else if (t <= 2) {if (son[u][t-1] = = 0) {son[u][t-1] = newNode (u); idx[son[u][t-1]] = idx[u];cnt[idx[u]]--;} U = son[u][t-1];} else {scanf ("%d", &k); son[u][t-3] = NewNode (u); cnt[son[u][t-3]] = k-1;idx[son[u][t-3]] = son[u][t-3];}} memset (sum, 0, sizeof (sum)), for (int i = 1; i <= M; i++) {if (son[i][0] = 0) sum[idx[i]]++;if (son[i][1] = = 0) Sum[idx[i ]]++;}} int solve () {int ret = 1;for (int i = 1; i <= M; i++) {if (idx[i]! = i) Continue;ret = (ll) ret * dp[sum[i]][cnt[i]]% mod;} Return ret;} int main () {preserve (); int cas = 1;while (scanf ("%d", &n) = = 1) {init ();p rintf ("case #%d:%d\n", cas++, solve ());} Return 0;}


Read the full text of the copyright notice: this article for Bo Master original article, without Bo Master agreed not to Reprint. Report
    • Label:
    • This article has been included in the following column:
0Article comments
Related articles recommended
hdu-1267 Sand has a few grains Dp/catalan topic link: hdu 5370 Tree makern nodes of the Two-tree species of Catalan number of the nth item for a subtrees Trees. A node that has been moved is a definite location. So just know how many K nodes have been identified.
  • hss871838309
  • 2012-03-01 15:06
  • 501
HDU 4359 Easy Tree DP? Best Solutions Forproblem 4359<div align= "right" style= "font-family:arial; font-size:12px; F
  • Java-mans
  • 2012-08-10 14:09
  • 370
HDU-4714 tree2cycle (tree dp) topic link: HDU 5370 Tree makern nodes of the two-tree species of Catalan number of the nth item for a subtrees Trees. The moved node is a definite location, so just know how many K nodes have been identified.
  • qq_31759205
  • Yesterday at 00:20
  • 11
HDU 1023 Train problem II Catalan series large number multiplication Train problem iitime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total submission (s): 2714Accepted submission (s): 1517</str
  • Hulunberbus
  • 2012-02-28 17:44
  • 385
HDU5370 Tree Maker topic link: hdu 5370 Tree makern nodes of the two-ary species for the Catalan number of the nth item for a subtrees tree, the moved node is a definite position, So just know how many K nodes have been identified.
  • Kyleyoung_ymj
  • 2016-06-28 15:52
  • 184
POJ 2378 Tree Cutting (dp) This article is from <a target= "_blank" href= "http://blog.csdn.net/shuangde800" style= "color:rgb" ( 202,0,0);
  • King_tt
  • 2013-08-31 14:31
  • 386
2017 School 3rd HDU 6065 RXD, tree and Sequence lca,dp topic links: HDU 5370 Tree makern nodes of the two-tree species of Catalan number of the nth item for a subtrees tree, the moved A node is a definite position, so just know how many K nodes have been identified.
  • Just_sort
  • 5 days ago 09:08
  • 35
Ural 1018 Binary Apple Tree DP | Classic) this article
  • King_tt
  • 2013-09-02 00:49
  • 325
hdu-1267 Sandy Sand There are a few dp/catalan tangled problem ah Acmstep 2.3.8 in 2.3 a look at the Catalan number on the thought of a very happy derivation of the Formula: c[m][n]= (m-n+1)/n*c[m][ n-1]; c[m][0]=1, among Them. M is the number of positions that D can place.

finally, a large number of processing methods to calculate the results after the submission WA crunch Leng thought for a long while do not know why crunch finally found the last HIMDD blog post, found to be able to use DP Ideas. Solution one: DP Code and parsing such as the following: #include &lt;iost

  • Java-mans
  • 2012-03-01 15:06
  • 423
HDU 6035 Colorful Tree dp) topic link: HDU 5370 Tree makern nodes of the two-tree species of the Catalan number of the nth item for a subtrees, the moved node is a definite position, So just know how many K nodes have been identified.
  • Addkai
  • 2017-07-29 18:11
  • 20
no
hurry, No rush, no fuss + attention
Original
304
Fans
a
Like
0
    • hdu 5876 Sparse Graph (bfs+ and Collection)
    • hdu 5875 Function (high Efficiency)
    • hdu 5593 ZYB ' s tree (dp)
    • hdu 5592 ZYB ' s premutation (segment tree)
many other articles Online Courses
"live" Machine Learning & Data Mining 7 weeks Training--Wei Chi
"package" System Integration Projects management project manager Smooth Clearance--Xu Bong
  • folder
  • like to cancel like
  • Collection
  • share Weibo QQ
Collection Assistant Bad news report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.