设f(n)为所求答案

g(n)为n个顶点的非联通图

则f(n) + g(n) = h(n) = 2^(n * (n - 1) / 2)

其中h(n)是n个顶点的联图的个数

这样计算

先考虑1所在的连通分量包含哪些顶点

假设该连通分量有k个顶点

就有C(n - 1, k - 1)种集合

确定点集后,所在的连通分量有f(k)种情况。其他连通分量有 h(n - k)种情况

因此有递推公式。g(n) = sum{ C(n - 1, k - 1) * f(k) * h(n - k)} 其中k = 1,2...n-1

注意每次计算出g(n)后立刻算出f(n)

import java.math.BigInteger;
import java.util.Scanner; public class Main { public static void main(String[] args) {
BigInteger two[] = new BigInteger [55];
two[0] = BigInteger.ONE;
for(int i = 1; i <= 50; i++)
two[i] = two[i - 1].multiply(BigInteger.valueOf(2));
BigInteger h[] = new BigInteger [55];
for(int i = 1; i <= 50; i++){
int a = i;
int b = i - 1;
if(a % 2 == 0) a/= 2;
else b /= 2;
h[i] = BigInteger.ONE;
for(int j = 0; j < a; j++) {
h[i] = h[i].multiply(two[b]);
}
}
BigInteger C[][] = new BigInteger[55][55];
C[0][0] = BigInteger.ONE;
for(int i = 0; i <= 50; i++){
C[i][0] = C[i][i] = BigInteger.ONE;
for(int j = 1; j < i; j++){
C[i][j] = C[i - 1][j].add(C[i - 1][j - 1]);
}
}
BigInteger f[] = new BigInteger[55];
BigInteger g[] = new BigInteger[55];
f[1] = BigInteger.ONE;
for(int i = 2; i <= 50; i++) {
g[i] = BigInteger.ZERO;
for(int j = 1; j < i; j++) {
g[i] = g[i].add(C[i - 1][j - 1].multiply(f[j]).multiply(h[i - j]));
}
f[i] = h[i].subtract(g[i]);
}
int n;
Scanner cin = new Scanner(System.in);
while(cin.hasNext()){
n = cin.nextInt();
if(n == 0) break;
System.out.println(f[n]);
}
} }

POJ 1737 统计有n个顶点的连通图有多少个 (带标号)的更多相关文章

  1. poj 1737男人八题之一 orz ltc

    这是楼教主的男人八题之一.很高兴我能做八分之一的男人了. 题目大意:求有n个顶点的连通图有多少个. 解法: 1.  用总数减去不联通的图(网上说可以,我觉得时间悬) 2.    用动态规划(数学递推) ...

  2. poj 1737 Connected Graph

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

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

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

  4. POJ 2002 统计正方形 HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...

  5. POJ 1971 统计平行四边形 HASH

    题目链接:http://poj.org/problem?id=1971 题意:给定n个坐标.问有多少种方法可以组成平行四边形.题目保证不会有4个点共线的情况. 思路:可以发现平行四边形的一个特点,就是 ...

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

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

  7. POJ 1966 Cable TV Network(顶点连通度的求解)

                               Cable TV Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  8. poj 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 AC代码: #include<iostream> #include<algor ...

  9. poj 3286 统计0的个数

    #include <iostream> using namespace std; long long p; ]; long long solve(long long n){ ; ;i< ...

随机推荐

  1. spin.js插件的转圈加载效果

    先上插件链接地址:http://fgnass.github.io/spin.js/ 以下是使用spin.js插件的完整版测试例子: <!doctype html> <html> ...

  2. WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1) 简单数据绑定 简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性.采用如下形式进行绑定 ...

  3. 在python 中有时候我们用数组

    在python 中有时候我们用数组操作数据可以极大的提升数据的处理效率, 类似于R的向量化操作,是的数据的操作趋于简单化,在python 中是使用numpy模块可以进行数组和矢量计算. 下面来看下简单 ...

  4. php5.4安装ecshopphp5.4问题及解决

    includes/cls_template.php line422 将 $tag_sel = array_shift(explode(" ", $tag)); 这句话拆开为两句. $tag_exp = ...

  5. dotnet core 开发体验之Routing

    开始 回顾上一篇文章:dotnet core开发体验之开始MVC 里面体验了一把mvc,然后我们知道了aspnet mvc是靠Routing来驱动起来的,所以感觉需要研究一下Routing是什么鬼. ...

  6. 写个自动安装JDK的shell脚本

    #!/bin/bash ################################################# # # INSTALL JDK AUTOMATICALLY # # auth ...

  7. 自己写的carousel

    可以 function appendRight() { //alert("right"); lastItem = itemsRight[urls.length - ]; first ...

  8. 【HDOJ】2888 Check Corners

    二维RMQ. /* 2888 */ #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  9. wcf资料

    WCF服务安全控制之netTcpBinding的用户名密码验证http://www.cnblogs.com/wengyuli/archive/2011/05/14/wcf-nettcpbinding- ...

  10. Service Trace Viewer Tool (SvcTraceViewer.exe)

    Service Trace Viewer Tool <?xml version="1.0" encoding="utf-8" ?> <conf ...