Rooted Trees Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 443 Accepted Submission(s): 147
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
Author
SmallBeer(CML)
Source
Recommend
lcy
枚举子树的组成,然后计算方法数。
#include<cstdio>
#include<iostream>
using namespace std;
long long t[50];
int cnt[50] , n;
long long gcd(long long a, long long b)
{
return b?gcd(b,a%b):a;
}
long long C(long long n, long long m)
{
if(m > n - m ) m = n - m;
long long mul = 1 , div = 1;
for(int i = 0; i < m; i++)
{
mul *= (n-i);
div *= (i+1);
long long g = gcd(mul,div);
mul/=g , div /= g;
}
return mul / div;
}
void dfs(int start, int left)
{
if( left == 0 )
{
long long ans = 1;
for(int i = 1; i<=n ; i ++)
{
if(cnt[i] == 0)continue;
ans = ans * C(t[i] - 1 + cnt[i] , cnt[i]);
}
t[n] += ans;
return ;
}
if(start > left )return;
cnt[start]++;
dfs(start , left - start);
cnt[start]--;
dfs(start+1 , left);
}
int main()
{
t[1] = t[2] = 1;
for(n = 3; n <= 40 ; n++)
{
dfs(1,n-1);
}
while( ~scanf("%d",&n) )
printf("%I64d\n",t[n]);
return 0;
}