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 ...
随机推荐
- BZOJ 2330 SCOI 2011 糖果
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MB Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友 ...
- python 杂记
class TestA(object): def __init__(self): print("A is initing"); def foo(self): print(" ...
- javaweb分页思想
web上的分页分析 在web编写中的经常会遇到,数据需要分页的情况.当数据量不是很大的时候. 可以直接使用js来分页.可以很好的提高性能.简化代码.数据量大的时候.还是需要使用java的分页类 ...
- poj 1037 A decorative fence
题目链接:http://poj.org/problem?id=1037 Description Richard just finished building his new house. Now th ...
- Http GET、Post方式的请求总结
读取http响应信息,并返回响应体 /// <summary> /// 读取http响应信息,并返回响应体 /// </summary> /// <param name= ...
- 解决PopupWindow遮住输入法
1: PopupWindow中的设置 pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); pop.setSoftInputMode(Win ...
- ActiveX in QT
http://doc.qt.io/qt-4.8/activeqt.htmlhttp://doc.qt.io/qt-5/activeqt-index.html
- Linux——oracle数据库实例启动关闭(转)
-->Oracle 数据库实例启动关闭过程 --================================ [root@robinson ~]# su - oracle --查看未启动实例 ...
- C语言中的声明解析规则——数组,指针与函数
摘要:C语言的申明存在的最大问题是:你无法以一种人们所习惯的自然方式和从左向右阅读一个声明,在引入voliatile和const关键字以后,情况更加糟糕了.由于这些关键字只能出现在声明中,是的声明形式 ...
- hdu 1829 A Bug's Life(并查集)
A Bu ...