uva10253 Series-Parallel Networks

原创 2017年01月02日 21:48:12

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:  A single edge is two-terminal series-parallel.  If G
1 and G 2 are two-terminal series-parallel, so is the network obtained
by identifying the sources and sinks, respectively (parallel
composition).  If G 1 and G 2 are two-terminal series-parallel, so is
the network obtained by identifying the sink of G 1 with the source of
G 2 (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 le 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 le print a line containing the number
of two-terminal series-parallel networks that can be obtained using
exactly N edges.

不断的串并联其实就是不断的合并边的过程,而且从最后一次回溯,每一层的串并联情况都相同,相邻层之间的串并联情况相反【否则就不分层了】。
这样其实就是一个正整数拆分,可以用动态规划解决。
dp[i][j]表示i条边,最大的联通块不超过j条边的方案数,那么答案ans[i]=dp[i][i1]【因为每一次至少连接两个联通块】,最后需要乘2【串并联互换】。枚举恰好有j条边的联通块数,

dp[i][j]=k=1ijdp[ikj][j1]f(ans[j],k)

其中f(n,k)表示从n个元素中可重复地选取k个的组合数,可以证明
f(n,k)=Ckn+k1

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
const int maxn=30;
LL dp[35][35],ans[35];
LL c(LL n,LL k)
{
    double ret=1;
    LL i;
    for (i=n-k+1;i<=n;i++)
      ret*=i;
    for (i=1;i<=k;i++)
      ret/=i;
    return ret+0.5;
}
int main()
{
    int i,j,k,n;
    dp[0][0]=ans[0]=ans[1]=1;
    for (i=1;i<=maxn;i++)
      dp[0][i]=dp[1][i]=1;
    for (j=1;j<=maxn;j++)
    {
        for (i=2;i<=maxn;i++)
          for (k=0;k*j<=i;k++)
            dp[i][j]+=dp[i-k*j][j-1]*c(ans[j]+k-1,k);
        ans[j+1]=dp[j+1][j];
    }
    while (scanf("%d",&n)&&n) printf("%lld\n",n==1?1:2*ans[n]);
}
版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。

相关文章推荐

UVa 10253 Series-Parallel Networks

《训练指南》中的第二种算法,其实本质上就是个背包。d[i][j]表示,在子树的节点数最大为i的情况下,j个节点的解。当之前的i-1,i-2,....0的结果都已知的时候,d[i][j]自然可根据下式求...

Uva 10253 - Series-Parallel Networks 解题报告(递推)

Problem H Series-Parallel Networks Input: standard input Output:  standard output Time Limit: 5 ...
  • kbdwo
  • kbdwo
  • 2014-03-27 18:51
  • 570

控制异步回调利器 - async 串行series,并行parallel,智能控制auto简介

async 作为大名鼎鼎的异步控制流程包,在npmjs.org 排名稳居前五,目前已经逐渐形成node.js下控制异步流程的一个规范.async成为异步编码流程控制的老大哥绝非偶然,它不仅使用方便,文...

算法竞赛入门经典(第2版)习题4-5 IP网络 IP Networks UVa1590

这道题的逻辑很简单,读入所有的ip地址,确认第一个不同出现的数据位置
  • archya
  • archya
  • 2014-08-11 13:30
  • 1677

UVa 1590 IP Networks (IP网络)

题意: 子网掩码是子网划分的依据,它跟IP地址一样,长度也是32位,点分十进制表示,每部分0~255,但是跟IP地址不同的是,子网掩码只能由连续的1和0组成,也就是说,把这32位从任意位置分开,...

UVA 1590 IP Networks(IP 网络)

题目链接:UVA - 1590 IP Networks题目大意: 一个网络地址A和一个子网掩码可以描述一个子网,子网是一个数包含32个二进制位,前n个位为1,后32-n个位为0,如:255.255....

UVA 1590 IP Networks 简单模拟

WA了许多次,也不知道什么原因。。。。最后拿到官方数据对比发现原来是下溢的问题。。。。-1在无符号数下发生下溢,我仅仅考虑了n的下溢,但是没有考虑到其他的问题。。。确实不应该。         感觉...

UVA 1590 IP Networks 位操作解题

用位操作来解uvaoj 1590 ip networks
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)