poj1737 Connected Graph

175 篇文章 0 订阅
125 篇文章 0 订阅

Description An undirected graph is a set V of vertices and a set of
E∈{V*V} edges.An undirected graph is connected if and only if for
every pair (u,v) of vertices,u is reachable from v. You are to write a
program that tries to calculate the number of different connected
undirected graph with n vertices. For example,there are 4 different
connected undirected graphs with 3 vertices.

Input The input contains several test cases. Each test case contains
an integer n, denoting the number of vertices. You may assume that
1<=n<=50. The last test case is followed by one zero.

Output For each test case output the answer on a single line.

计数类dp。
用总数减去不连通的图的个数,得到的就是连通图的个数。
设有i个点,不连通图数为g[i],连通图数为f[i],图的总数为cnt[i]。
易得cnt[i]=2^(i * (i-1)/2)【总共有i * (i-1)/2条可能的边,考虑每条边是否存在】
考虑1号点所在连通块大小,设之为j。
那么g[i]=C(i-1,j-1)【在剩下的i-1个点中选择j-1个点与1号点连接】 * f[j]【这个连通块里的点必须连通】 * cnt[i-j]【剩下的点随便连】
f[i]=cnt[i]-g[i]
其中组合数可以用C(i,j)=C(i-1,j)+C(i-1,j-1),C(i,0)=1计算出。
注意用高精度。
可以先预处理出所有需要的值和答案,直接回答问题。

#include<cstdio>
#include<cstring>
struct num
{
    int l,a[100];
    num operator + (const num &x) const
    {
        num ans;
        int len;
        memset(ans.a,0,sizeof(ans.a));
        for (int i=1;i<=l||i<=x.l;i++)
        {
            ans.a[i]+=a[i]+x.a[i];
            ans.a[i+1]+=ans.a[i]/10000;
            ans.a[i]%=10000;
        }
        if (l<x.l) len=x.l+1;
        else len=l+1;
        while (!ans.a[len]&&len) len--;
        ans.l=len;
        return ans;
    }
    num operator - (const num &x) const
    {
        num ans;
        memset(ans.a,0,sizeof(ans.a));
        for (int i=1;i<=l;i++)
        {
            ans.a[i]+=a[i]-x.a[i];
            if (ans.a[i]<0)
            {
                ans.a[i]+=10000;
                ans.a[i+1]--;
            }
        }
        ans.l=l;
        while (!ans.a[ans.l]&&ans.l) ans.l--;
        return ans;
    }
    num operator * (const num &x) const
    {
        num ans;
        memset(ans.a,0,sizeof(ans.a));
        int len;
        for (int i=1;i<=l;i++)
          for (int j=1;j<=x.l;j++)
          {
            ans.a[i+j-1]+=a[i]*x.a[j];
            ans.a[i+j]+=ans.a[i+j-1]/10000;
            ans.a[i+j-1]%=10000;
          }
        len=l+x.l;
        while (!ans.a[len]&&len) len--;
        ans.l=len;
        return ans;
    }
}f[55],g[55],pow[1300],c[55][55],n1,n2,n3;
num cv(int x)
{
    num ans;
    memset(ans.a,0,sizeof(ans.a));
    ans.l=0;
    while (x)
    {
        ans.l++;
        ans.a[ans.l]=x%10000;
        x/=10000;
    }
    if (!ans.l) ans.l=1;
    return ans;
}
void prt(num x)
{
    printf("%d",x.a[x.l]);
    for (int i=x.l-1;i>=1;i--)
    {
        int y=x.a[i];
        if (y<1000) printf("0");
        if (y<100) printf("0");
        if (y<10) printf("0");
        printf("%d",y);
    }
}
int main()
{
    int i,j,k,m,n,p,q,x,y,z;
    pow[0]=cv(1);
    for (i=1;i<=1250;i++)
      pow[i]=pow[i-1]*cv(2);
    for (i=0;i<=50;i++)
      c[i][0]=cv(1);
    for (i=1;i<=50;i++)
      for (j=1;j<=i;j++)
        c[i][j]=c[i-1][j]+c[i-1][j-1];
    for (i=1;i<=50;i++)
    {
        for (j=1;j<i;j++)
          g[i]=g[i]+(c[i-1][j-1]*f[j]*pow[(i-j)*(i-j-1)/2]);
        f[i]=pow[i*(i-1)/2]-g[i];
    }
    while (scanf("%d",&n)&&n)
    {
        prt(f[n]);
        printf("\n");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值