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. [学习笔记]HTML5之canvas

    虐了一下午的canvas 先撸了一个七巧板 <!doctype html> <html> <head> <meta charset="utf-8&q ...

  2. UVa1583 Digit Generator

    #include <stdio.h> int main(){    int T, N, i, k, digitsum, generator;    scanf("%d" ...

  3. MySqlQueryList

    //辅助查询列表,或实例 public class MySqlQueryList { #region List<T> ToList<T>(string sql, params ...

  4. Media Queries for Standard Devices

    /* Smartphones (portrait and landscape) ----------- */ @media only screen and (min-device-width : 32 ...

  5. C#读取网页

    public bool getweb(string strURL,out string buf) { buf=""; try { //Uri url=new Uri(strURL, ...

  6. R语言学习之主成分分析法的R实践

    主成分分析R软件实现程序(一): >d=read.table("clipboard",header=T) #从剪贴板读取数据 >sd=scale(d)  #对数据进行标 ...

  7. CI 模板解析器类

    模板解析器类可以解析你的视图文件中的伪变量.它可以解析简单的变量或者以变量作为标签的结构.如果你以前没有用过模板引擎,那么伪变量如下所示: <html><head><ti ...

  8. HTML的标题样式

    标题样式1 <p> <span style=" text-align: center; padding-bottom: 6px; padding-left: 20px; p ...

  9. MySql学习笔记(转载)

    . 数值类型 -- a. 整型 ----------     类型            字节        范围(有符号位)     tinyint        1字节    - ~        ...

  10. JAVA FILE or I/O学习 - File学习

    public class FileKnow { public static void main(String[] args) { //构建file对象 ,参数表示文件所在的路径 File file = ...