Uva 10253 - Series-Parallel Networks 解题报告(递推)
2014-03-27 18:51
323 查看
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 the source 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 ofG1 with the source of G2 (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 exactly N edges.
Sample Input
1
4
15
0
Sample Output
1
10
1399068
解题报告:比较难搞的递推。主要是要能想到递推方程。不多说了,不懂就看书吧。(训练指南,p117)。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
LL dp[100][100];
LL C(LL x,int y)
{
double tmp=0;
for(int i=1;i<=y;i++)
tmp+=log10(x-i+1)-log10(i);
return (LL)(pow(10,tmp)+0.5);
}
LL dfs(int x,int y)
{
if(y==0) return 0;
if(x==1||y==1||x==0) return 1;
LL &ans=dp[x][y];
if(~ans) return ans;
ans=0;
for(int i=0;i*y<=x;i++)
ans+=dfs(x-i*y,y-1)*C(dfs(y,y-1)+i-1,i);
return ans;
}
int main()
{
memset(dp,-1,sizeof(dp));
int n;
while(~scanf("%d",&n) && n)
printf("%lld\n", n==1?1:dfs(n,n-1)*2);
}
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 the source 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 ofG1 with the source of G2 (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 exactly N edges.
Sample Input
1
4
15
0
Sample Output
1
10
1399068
解题报告:比较难搞的递推。主要是要能想到递推方程。不多说了,不懂就看书吧。(训练指南,p117)。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
LL dp[100][100];
LL C(LL x,int y)
{
double tmp=0;
for(int i=1;i<=y;i++)
tmp+=log10(x-i+1)-log10(i);
return (LL)(pow(10,tmp)+0.5);
}
LL dfs(int x,int y)
{
if(y==0) return 0;
if(x==1||y==1||x==0) return 1;
LL &ans=dp[x][y];
if(~ans) return ans;
ans=0;
for(int i=0;i*y<=x;i++)
ans+=dfs(x-i*y,y-1)*C(dfs(y,y-1)+i-1,i);
return ans;
}
int main()
{
memset(dp,-1,sizeof(dp));
int n;
while(~scanf("%d",&n) && n)
printf("%lld\n", n==1?1:dfs(n,n-1)*2);
}
相关文章推荐
- Uva 10312 - Expression Bracketing 解题报告(卡特兰数+递推)
- Uva 11361 Investigating Div-Sum Property 解题报告(递推)
- Uva 1393 - Highways 解题报告(递推)
- Uva 580 - Critical Mass 解题报告(递推)
- UVA 10253 - Series-Parallel Networks(数论+计数问题+递推)
- Uva 10157 - Expressions 解题报告(递推)
- UVa 10253 (组合数 递推) Series-Parallel Networks
- Uva 11375 Matches 解题报告(递推+高精度)
- Uva 11137 Ingenuous Cubrency 解题报告(递推)
- Uva 10079 - Pizza Cutting 解题报告(递推)
- Uva 10081 - Tight Words 解题报告(递推)
- Uva 11210 Chinese Mahjong 解题报告(枚举+码力)
- Uva 10795 A Different Task 解题报告(递归+思维)
- UVa 11627 Slalom 解题报告(二分)
- UVa 1398 Meteor 解题报告(扫描线)
- Uva12100Printer Queue NWERC2008 解题报告
- 训练指南UVALive5088解题报告
- UVA 539 - The Settlers of Catan 解题报告
- UVA 12538 Version Controlled IDE 解题报告
- Uva 1350 - Pinary 解题报告(斐波那契+二分)