转载请注明出处: 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 ...
随机推荐
-
杨光福IT讲师微博
杨光福IT讲师微博: http://weibo.com/321chinavideo 微博现在里面有很多干货,以后会越来越多,主要用于分享和交流技术.关注一下对你有帮助.
-
PHP内核探索之变量(3)- hash table
在PHP中,除了zval, 另一个比较重要的数据结构非hash table莫属,例如我们最常见的数组,在底层便是hash table.除了数组,在线程安全(TSRM).GC.资源管理.Global变量 ...
-
JAX-WS:背后的技术JAXB及传递Map
转载:http://www.programgo.com/article/98912703200/ 1.什么是JAX-WS JAX-WS (JavaTM API for XML-Based Web Se ...
-
小圣求职记A:腾讯篇
本人普通985高校计算机专业研究生一枚,从9月12号开始正式找工作,一个月过去了,参加了能参加的各个互联网公司的宣讲.笔试.面试,现用两篇随笔分享所见所闻.随笔A将以腾讯为例详细展示整个过程,随笔B将 ...
-
从一个简单的Java单例示例谈谈并发
一个简单的单例示例 单例模式可能是大家经常接触和使用的一个设计模式,你可能会这么写 public class UnsafeLazyInitiallization { private static Un ...
-
Android Studio中JNI -- 2 -- 编写c文件
继上一篇,我们在native接口中编写了2个方法 生成的相应.h文件 这时,需要我们自己去完善.c文件 /* DO NOT EDIT THIS FILE - it is machine generat ...
-
poj 2411 新写法
别以为我在刷水题.... 今天做了场srm,500pt想到了是dp但是无从下手,但是看了rng_58的神代码后顿觉海阔天空啊(盯着看了一个下午),相比于一年前的写法,真的是不忍直视啊, TC真是个好地 ...
-
Unity3D研究院之在把代码混淆过的游戏返混淆回来(七十七)
最近一直在找如何在MAC上混淆Android的DLL,至今没能找到合适的,有大神知道记得告诉我喔.今天群里有人说了一个混淆代码和返混淆代码的工具de4dot ,不查不知道一查吓一跳.这玩意可以把别人混 ...
-
hdu_5876_Sparse Graph(补图BFS)
题目链接:hdu_5876_Sparse Graph 附上叉姐的题解: 1009 Sparse Graph [by ftiasch] 题意:n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最 ...
-
基于服务的SOA架构_后续篇
今天是元宵节,首先祝各位广大博友在接下来的光阴中技术更上一层,事事如意! 昨天简单介绍了一下本人在近期开发过的一个电商购物平台的架构流程和一些技术说明:今天将详细总结一下在项目中用到的各个架构技术的环 ...