Connected Graph
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3156   Accepted: 1533

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.

Sample Input

1
2
3
4
0

Sample Output

1
1
4
38

Source

 
 
 
n个点之间任取两点连边,按照组合数公式,共有$ C(n,2)=n*(n-1)/2 $条边可连
每条边可连可不练,所以总情况有 P=2^C(n,2) 种。
我们要求的是所有点都连通的情况数,可以用总数P减去不连通的情况数
设F[i]为i个点构成连通图的情况数,任取一点为基准,当与其构成连通图的点有j-1个时,共有F[j]种连通情况。则若在总图中有j个点一定连通,共有$C(i-1,j-1)*F[j] $种情况,而剩下的点可以随意连边,共有$2^C(i-j,2)$种情况。
若总点数为i,则答案为:$F[i]=P[i]-sum$;   sum=sum+(C(i-1,j-1)*F[j]*2^C(i-j,2))    {1<=j<i 累加求和}
 
然而高精度各种写不对,我选择死亡。
 
先放一张表
 INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:
INPUT:
OUTPUT:

打表

然后是我一直改不对的代码

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct bgnum{
int l;
int a[];
bgnum operator + (const bgnum &x) const{
bgnum ans;
memset(ans.a,,sizeof(ans.a));
int len=max(l,x.l);
ans.l=;
for(int i=;i<=len;i++){
ans.a[i]+=a[i]+x.a[i];
ans.a[i+]+=ans.a[i]/;
ans.a[i]%=; }
len++;
while(!ans.a[len]&&len)len--;
ans.l=len;
return ans;
}
bgnum operator - (const bgnum &x) const{
bgnum ans;
memset(ans.a,,sizeof(ans.a));
for(int i=;i<=l;i++){
ans.a[i]+=a[i]-x.a[i];
if(ans.a[i]<){
ans.a[i]+=;
ans.a[i-]--;
}
}
ans.l=l;
while(!ans.a[ans.l] && ans.l) ans.l--;
return ans;
}
bgnum operator * (const bgnum &x) const{
bgnum ans;
memset(ans.a,,sizeof(ans.a));
for(int i=;i<=l;i++)
for(int j=;j<=x.l;j++){
ans.a[i+j-]+=a[i]*x.a[j];
ans.a[i+j]+=ans.a[i+j-]/;
ans.a[i+j-]%=;
}
int len=l+x.l;
while(!ans.a[len] && len)len--;
ans.l=len;
return ans;
}
}f[],//[i]个点构不同图的方案数
c[][],//[i]个点中选[j]个任意连边的方案数
mi[],//2的[i]次方
sum; void Print(bgnum p){
for(int i=p.l;i>=;i--){
printf("%d",p.a[i]);
}
printf("\n");
return;
}
bgnum p1,p2;
int main(){
p1.l=;p1.a[]=;//高精度数1
p2.l=;p2.a[]=;//高精度数2
int i,j;
mi[]=p1;
for(i=;i<=;i++)
mi[i]=mi[i-]*p2;
for(i=;i<=;i++)
c[i][]=p1;
for(i=;i<=;i++)
for(j=;j<=i;j++){
c[i][j]=c[i-][j]+c[i-][j-];//组合数递推公式
}
for(i=;i<=;i++){
sum.l=;
memset(sum.a,,sizeof(sum.a));
for(j=;j<i;j++){
sum=sum+(c[i-][j-]*f[j]*mi[(i-j)*(i-j-)/]);
}
// Print(sum);
f[i]=mi[i*(i-)/]-sum;
}
int n;
scanf("%d",&n);
Print(f[n]);
return ;
}

再放隔壁某dalao的AC题解

http://blog.csdn.net/orion_rigel/article/details/51812864

POJ1737 Connected Graph的更多相关文章

  1. 【Java】【高精度】【组合数】【递推】poj1737 Connected Graph

    http://blog.csdn.net/sdj222555/article/details/12453629 这个递推可以说是非常巧妙了. import java.util.*; import ja ...

  2. [poj1737]Connected Graph(连通图计数)

    题意:输出题中带有$n$个标号的图中连通图的个数. 解题关键: 令$f(n)$为连通图的个数,$g(n)$为非联通图的个数,$h(n)$为总的个数. 则$f(n) + g(n) = h(n)$ 考虑标 ...

  3. $Poj1737\ Connected\ Graph$ 计数类$DP$

    AcWing Description 求$N$个节点的无向连通图有多少个,节点有标号,编号为$1~N$. $1<=N<=50$ Sol 在计数类$DP$中,通常要把一个问题划分成若干个子问 ...

  4. poj 1737 Connected Graph

    // poj 1737 Connected Graph // // 题目大意: // // 带标号的连通分量计数 // // 解题思路: // // 设f(n)为连通图的数量,g(n)为非连通图的数量 ...

  5. POJ 1737 Connected Graph 题解(未完成)

    Connected Graph Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3156   Accepted: 1533 D ...

  6. Connected Graph

    Connected Graph 求n个点的无向联通图数量,\(n\leq 50\). 解 直接无向联通图做状态等于是以边点做考虑,难以去重,考虑联通对立面即不联通. 不难求出n个点的总方案数为\(2^ ...

  7. 【poj1737】 Connected Graph

    http://poj.org/problem?id=1737 (题目链接) 题意 求n个节点的无向连通图的方案数,不取模w(゚Д゚)w Solution 刚开始想了个第二类斯特林数,然而并不知道怎么求 ...

  8. POJ 1737 Connected Graph(高精度+DP递推)

    题面 \(solution:\) 首先做个推销:带负数的压位高精度(加减乘+读写) 然后:由 \(N\) 个节点组成的无向图的总数为: \(2^{N*(N-1)/2}\) (也就是说这个图总共有 \( ...

  9. POJ 1737 Connected Graph (大数+递推)

    题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单(无重边无自环)连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS ...

随机推荐

  1. android stuio eclipse映射下的快捷键

    转:关于 android stuio eclipse映射下的快捷键 http://www.cnblogs.com/0616--ataozhijia/p/3870064.html 会持续更新)这边讲的常 ...

  2. View (二) 自定义属性 自定义属性的格式详解

    自定义属性格式一共有十种: 1. reference:参考某一资源ID. 2. color:颜色值. 3. boolean:布尔值. 4. dimension:尺寸值. 5. float:浮点值. 6 ...

  3. AndroidStudio权威教程 AS添加第三方库的6种方式(Jar module so等)

    点击项目设置按钮 依次选择 App > Dependencies 1. 直接搜索法 依次选择 + > Library dependency 这里的搜索一定要是全名的,不然搜不到哦 下图所表 ...

  4. NSDictionary(key与value)

    1.key与value关系,用一个key的值控制整个模型 NSDictionary *dic = @{@"channelKey":channelModel,@"chann ...

  5. 线程操作案例--生产者与消费者,Object类对线程的支持

    本章目标 1)加深对线程同步的理解 2)了解Object类中对线程的支持方法. 实例 生产者不断生产,消费者不断消费产品. 生产者生产信息后将其放到一个区域中,之后消费者从区域中取出数据. 既然生产的 ...

  6. window7 右键菜单显示-》在此处打开命令窗口

    window7 右键菜单显示->在此处打开命令窗口: 注册表中: HKEY_CLASSES_ROOT\Directory\Background\shell\cmd下将[Extended]重命名或 ...

  7. Gerrit日常操作命令收集

    Gerrit代码审核工具是个好东西,尤其是在和Gitlab和Jenkins对接后,在代码控制方面有着无与伦比的优势. 在公司线上部署了一套Gerrit系统,在日常运维中,使用了很多gerrit命令,在 ...

  8. 使用CSS3画出一个叮当猫

    刚学习了这个案例,然后觉得比较好玩,就练习了一下.然后发现其实也不难,如果你经常使用PS或者Flash的话,应该就会知道画个叮当猫是很容易的事,至少我是这么觉得.但是,用CSS3画出来确实是第一次接触 ...

  9. 呵呵!手把手带你在 IIS 上运行 Python(转)

    原文:http://blog.csdn.net/yangzhencheng_001/article/details/40342449 公司的网站让我头痛死了.在众多前辈高手的带领下,一大堆的 CMD ...

  10. WPF学习笔记:MVVM模式下,ViewModel如何关闭View?

    原文:http://blog.csdn.net/leftfist/article/details/32349731 矫枉过正,从一个极端走向另一个极端.MVVM模式,View只负责呈现,虽然也有后台代 ...