UVA 10253 Series-Parallel Networks (树形dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
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 of G1with 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
(World Final Warm-up Contest, Problem Setter: Rezaul Alam Chowdhury)
这道题目想了好久,最终还是参考了题解。
大致意思就是给你n条边,问你恰好用n条边,能构成几种串并联网络。(串联的各个部分可以任意调换,并联在一起的各个部分也可以任意调换,若通过调换可得,则二者视为等效)
分析:将每个网络都看成一棵树,为每次串联或者并联创建一个结点,并且把串联/并联部分看作该结点的子树,则可以转化为树形dp。
dp[i][j]表示每棵子树叶子数目不超过i,一共有j片叶子的方案数。
f[i]=dp[i-1][i],则根据可重复组合的公式,在有k个恰好包含i片叶子的子树时,其方案数等于C(f[i]+k-1,k);
dp[i][j]=∑(C(f[i]+k-1,k)*d[i-1][j-p*i]) k≥0,k*i<=j
另外注意处理好边界。
对于求这个组合数,想不出较好的方法,最终还是采用了刘汝佳在大白书上写的用double来做的方法(虽然我一度担心会因为double的精度问题会使得有所误差)。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll dp[][];
ll f[];
ll C(ll n,int m)
{
double ret=;
for(ll i=n+-m;i<=n;i++)
{
ret*=i;
}
for(int i=;i<=m;i++)ret/=i;
return (ll)(ret+0.5);
}
int main()
{
ios::sync_with_stdio(false);
int n=;
f[]=;
for(int i=;i<=n;i++){dp[][i]=;dp[i][]=;}
for(int i=;i<=n;i++)dp[i][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
dp[i][j]=;
for(int k=;i*k<=j;k++)
{
dp[i][j]+=C(f[i]+k-,k)*dp[i-][j-i*k];
}
}
f[i+]=dp[i][i+];
}
for(int i=;i<=n;i++)f[i]*=2LL;
while(cin>>n&&n)
{
cout<<f[n]<<endl;
}
}
UVA 10253 Series-Parallel Networks (树形dp)的更多相关文章
- UVa 12186 - Another Crisis(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 12186 Another Crisis (树形DP)
思路:dp[i]表示让上司i签字至少需要多少工人签字. 转移方程:将i的所有节点根据所需工人数量升序排序,设i需要k个下属签字,dp[i] = sum{dp[v]| 0 <= v & ...
- UVA - 1218 Perfect Service (树形DP)
思路:dp[i][0]表示i是服务器:dp[i][1]表示i不是服务器,但它的父节点是服务器:dp[i][2]表示i和他的父亲都不是服务器. 转移方程: d[u][0] += min(d[ ...
- UVa 1218 - Perfect Service(树形DP)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1220 Party at Hali-Bula (树形DP)
求一棵数的最大独立集结点个数并判断方案是否唯一. dp[i][j]表示以i为根的子树的最大独立集,j的取值为选和不选. 决策: 当选择i时,就不能选择它的子结点. 当不选i时,它的子结点可选可不选. ...
- UVa 10859 - Placing Lampposts 树形DP 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA - 1218 Perfect Service(树形dp)
题目链接:id=36043">UVA - 1218 Perfect Service 题意 有n台电脑.互相以无根树的方式连接,现要将当中一部分电脑作为server,且要求每台电脑必须连 ...
- 树形DP UVA 1292 Strategic game
题目传送门 /* 题解:选择一个点,它相邻的点都当做被选择,问最少选择多少点将所有点都被选择 树形DP:dp[i][0/1]表示当前点选或不选,如果选,相邻的点可选可不选,取最小值 */ /***** ...
- uva 1292 树形dp
UVA 1292 - Strategic game 守卫城市,城市由n个点和n-1条边组成的树,要求在点上安排士兵,守卫与点相连的边.问最少要安排多少士兵. 典型的树形dp.每一个点有两个状态: dp ...
随机推荐
- 系统报错 hppatusg01
下载DLL 放在C:\Windows\SysWOW64(64位系统)或C:\Windows\System32(32位系统) 下载地址 https://yunpan.cn/cBB4Q6czDKyqt ...
- PHP学习路上的一点心得
继学些了java后,接触php的项目后发现 php真的也是很强大的一门语言,这只是一篇回想,想到什么就写什么把,大家随便看看. 1.php其实无需等待,一般的改完代码后直接刷新页面即可,不需要像jav ...
- 利用JavaScript 的formdata 进行无刷新上传文件
<html> <head> <title></title> <script type=&quo ...
- JS 没有块级作用域
在函数(方法)中声明的所有变量,他们在整个函数中都有定义 var scope="abc"; function f() { alert(scope); //显示undefine v ...
- php----浅谈一下empty isset is_null的用处
} } { } { } } } { } { } is_null():判断变量是否为null if ($a){} 那这个未声明变量会报noti ...
- Win7/Win8/Win8.1众多版本,我该选择哪个?
当你要下载Win7或者Win8/8.1镜像时,是不是被Windows版本种类给吓着了?到底该选择哪种版本的?其实,大多数人用的就那一两个版本,这也是推荐选择的版本,请看快速通道.如果你想了解的更多一点 ...
- CRC校验源码分析
这两天做项目,需要用到 CRC 校验.以前没搞过这东东,以为挺简单的.结果看看别人提供的汇编源程序,居然看不懂.花了两天时间研究了一下 CRC 校验,希望我写的这点东西能够帮助和我有同样困惑的朋友节省 ...
- keil 中用函数指针调用函数的参数限制
NSIC中,通过函数指针调用的函数的参数的个数没有限制,但是KeilC对此有限制,至多3个参数.因为,KeilC编译时,无法通过函数指针找到该函数的局部数据段,也就无法通过局部数据段传递参数,只能通过 ...
- AnimateWindow
WINDOWS提供了一个很有意思的函数:AnimateWindow.之前我想实现像MSN,QQ这些收到邮件的时候动画方式,从地下升上来的显示一个窗口,感觉很麻烦,自己去写代码,效果很不理想,今天无意中 ...
- Apache本地服务无法自启动问题解决
问题描述: Windows不能在本地计算机启动Apache2.有关更多信息,查阅系统事件日志.如果这是非Microsoft服务,请与服务厂商联系,并参考特定服务错误代码1. 解决方案: 1.检查Apa ...