//ans(n)求共n个叶子节点,且每个非叶子节点至少有两个子节点的树的数目
//最后答案*2 ,分为rot为并联或者串联
//f(i,j) 表示每棵子树最多包含i叶子,一共有j个叶子的方案数 i可以>=j
//ans(i)=f(i-1,i)
//f(i,j)=sum{c(ans(i)+p-1,p)*f(i-1,j-p*i)};
//f(i,0)=1 (i>=0)
//f(i,1)=1 f(0,i)=0 (i>=1)
Problem H
Series-Parallel Networks
Input: standard input
Output: standard output
Time Limit: 5 seconds
Memory Limit: 32 MB
In this problem you are expected to count two-terminal series-parallel networks. These are electric networks considered topologically or geometrically, that is, without the electrical properties of the elements connected. One of the two terminals can be considered as thesource and the other as the sink.
A two-terminal network will be considered series-parallel if it can be obtained iteratively in the following way:
q A single edge is two-terminal series-parallel.
q If G1 and G2 are two-terminal series-parallel, so is the network obtained by identifying the sources and sinks, respectively (parallel composition).
q If G1 and G2 are two-terminal series-parallel, so is the network obtained by identifying the sink of G1 with the source ofG2 (series composition).
Note here that in a series-parallel network two nodes can be connected by multiple edges. Moreover, networks are regarded as equivalent, not only topologically, but also when interchange of elements in series brings them into congruence; otherwise stated, series interchange is an equivalence operation. For example, the following three networks are equivalent:
Similarly, parallel interchange is also an equivalence operation. For example, the following three networks are also equivalent:
Now, given a number N, you are expected to count the number of two-terminal series parallel networks containing exactly N edges. For example, for N = 4, there are exactly 10 series-parallel networks as shown below:
Input
Each line of the input file contains an integer N (1 £ N £ 30) specifying the number of edges in the network.
A line containing a zero for N terminates the input and this input need not be considered.
Output
For each N in the input file print a line containing the number of two-terminal series-parallel networks that can be obtained using exactlyN edges.
Sample Input
1
4
15
0
Sample Output
1
10
1399068
(World Final Warm-up Contest, Problem Setter: Rezaul Alam Chowdhury)
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define MAXN 35
typedef long long LL;
LL f[MAXN][MAXN];
LL ans[MAXN];
LL c(LL n,LL m)
{
double ans=1;
for(int i=0;i<m;i++)
ans*=(n-i);
for(int i=0;i<m;i++)
ans/=(i+1);
return (LL)(ans+0.5);
}
int main()
{
f[0][1]=0;ans[1]=1;
for(int i=0;i<=30;i++)
f[i][0]=1;
for(int i=1;i<=30;i++)
f[i][1]=1,f[0][i]=0;
for(int i=1;i<=30;i++)
{
for(int j=2;j<=30;j++)
{
f[i][j]=0;
for(int p=0;p*i<=j;p++)
{
f[i][j]+=(c(ans[i]+p-1,p)*f[i-1][j-p*i]);
}
}
ans[i+1]=f[i][i+1];
}
int n;
while(cin>>n&&n)
cout<<(n==1?1:ans[n]*2)<<endl;
return 0;
}