http://www.cnblogs.com/keam37/p/3639294.html keam所有 转载请注明出处

Problem Description

Give you two definitions tree and rooted tree. An undirected connected graph without cycles is called a tree. A tree is called rooted if it has a distinguished vertex r called the root. Your task is to make a program to calculate the number of rooted trees with n vertices denoted as Tn. The case n=5 is shown in Fig. 1.

Input

There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer means n(1<=n<=40) .

Output

For each test case, there is only one integer means Tn.

Sample Input

1 2 5

Sample Output

1 1 9

分析:

题目比较容易读懂,就是求n个节点的树有多少种。

按照递推的思路,f[n]=f[a1]*f[a2]*….f[ai];          其中 a1+a2+…+ai=n 且a1<=a2<=….<=ai

很自然想到这是整数拆分的样式

进一步考虑,对于n=a*sum[a]+b*sum[b]+c*sum[c]; sum[k]为一个拆分中k的个数

仅对sum[a]个 a计算

一个数a有f[a]种不同的拆分方式

就相当于从(f[a]个不同a)中选取sum[a]个(可以重复) 的排列

即 多重排列 C(f[a]+sum[a]-1,sum[a]);

再依次计算b,c累加即可.

参考代码

#include<iostream>
#define int64 __int64
using namespace std; int64 f[41];
int cnt[41] , n;
//计算组合数
int64 C(int64 n,int64 m){
m=m<(n-m)?m:(n-m);
int64 ans=1;
for(int i=1;i<=m;i++)
ans=ans*(n-i+1)/i;
return ans;
}
//拆分数并计算f[n]
int dfs(int temp, int left){
if( left == 0 ){
int64 ans = 1;
for(int i = 1; i<=n ;i ++){
if(cnt[i] == 0) continue;
//计算并累加多重排列
ans = ans * C(f[i] + cnt[i] - 1 , cnt[i]);
}
f[n] += ans;
return 0;
}
for(int i=temp;i<=left;i++){
cnt[i]++; dfs(i,left-i);
cnt[i]--;
}
return 0;
}
int main() {
f[1] = f[2] = 1;
for(n = 3; n <= 40 ; n++) dfs(1,n-1);
while(cin>>n) cout<<f[n]<<endl;
return 0;
}

HDU p1294 Rooted Trees Problem 解题报告的更多相关文章

  1. HDU 1294 Rooted Trees Problem

    题目大意:求有n个节点的树有几种? 题解:http://www.cnblogs.com/keam37/p/3639294.html #include <iostream> typedef ...

  2. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  3. hdu 1556.Color the ball 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目意思:有 n 个气球从左到右排成一排,编号依次为1,2,3,...,n.给出 n 对 a, ...

  4. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. hdu 1160 FatMouse's Speed 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weigh ...

  6. hdu 1879 继续畅通工程 解题报告

    题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1879 这条题目我的做法与解决Constructing Roads的解法是相同的. 0 表示没有连通: ...

  7. hdu 1233 还是畅通工程 解题报告

    题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1233 并查集的运用, 实质就是求最小生成树.先对所有的村庄距离从小到大排序,然后判断村庄之间是否属于 ...

  8. hdu 1213 How Many Tables 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 有关系(直接或间接均可)的人就坐在一张桌子,我们要统计的是最少需要的桌子数. 并查集的入门题,什 ...

  9. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

随机推荐

  1. c 查找A字符串在B字符串中是否存在,计算出现的次数

    主要是应用了头文件<string.h>中的strstr函数 char * strstr(const char *s1, const char *s2); 查找是否存在: #include& ...

  2. sublime test3 使用技巧

    sublimeText3使用技巧 常用快捷键 ctrl+d :选中光标处的文本单元,继续按ctrl+d选中相同文本单元 alt+F3 :功能和ctrl+d类似,用于批量修改相同文本 shift+↑ ↓ ...

  3. JavaScript 深入学习及常用工具方法整理 ---- 01.浮点数

    在JavaScript中是不区分整数值和浮点数值的,其中所有的数字均用浮点数值表示.JavaScript采用IEEE 754标准(有兴趣可以浏览网络规范分类下的IEEE 754标准,需要原文件请在留言 ...

  4. zoj 1366 Cash Machine

    01背包加变形 动态规划的时候就犯浑了,每个状态都要记录的,我却只记录了当前状态的!! #include<stdio.h> #include<string.h> int max ...

  5. BEGIN_SINK_MAP(CMainDlg) SINK_ENTRY(IDC_EXPLORER1, ..。响应不到的

    </pre><pre name="code" class="cpp"> class CMainDlg : public CAxDialo ...

  6. CV_EXPORTS定义及作用

    CV_EXPORTS 常用如下: class CV_EXPORTS MatExpr; template<typename _Tp> class CV_EXPORTS Size_; CV_E ...

  7. BZOJ 1624: [Usaco2008 Open] Clear And Present Danger 寻宝之路( 最短路 )

    直接floyd.. ----------------------------------------------------------------------- #include<cstdio ...

  8. wireshark 包分析命令

    1.查看原地址过滤包命令: ip.src ==192.168.1.1 2.查看目的地址过滤包:ip.dst == 192.168.1.1 3.关键字 eq 等于 "==" ,and ...

  9. 用户管理_组管理_设置主机名_UGO_文件高级权限_ACL权限

    用户管理: 添加用户:useradd tom 设置密码:passwd tom 切换账户: su - tom (不加-也能切换,但是 -会有两点不同 1.有-会切换到该用户的主目录  2.会切换到该用户 ...

  10. MapList 自己封装的

    //// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)/ ...