uva10253 Series-Parallel Networks

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]);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值