设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. javascrip 分享到

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. WPF内嵌代码和后台代码简单混合使用

    下面实例展示了WPF内嵌代码和后台代码混合使用,一个简单基础的实例: xaml文件: <Window x:Class="WPF内嵌代码和后台代码混合使用.MainWindow" ...

  3. MongoDB在windows服务器安装部署及远程连接MongoDB

    (.\是表示在服务器的windows powershell下需要 表示信任此命令才会执行不然会报错,自己电脑上使用时可去掉.\) 在本地使用都不需要开启权限而在服务器上需要开启安全模式所以需要在原本的 ...

  4. Windows命令行下pip安装python whl包

    因为做网页爬虫,需要用到一个爬新闻的BeautifulSoup 的包,然后再关网上下的是whl包,第一次装,虽然花了点时间,最后还是装上去了,记录一下,方便下次. 先发一下官方文档地址.http:// ...

  5. jackson学习----解析豆瓣的图书信息

      异常一. org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple ...

  6. 如何分析matlab程序的主要效率问题

    利用profile on 在需要分析效率的程序段前后加入 profile on profile off 然后,在common line中输入profile viewer即可观察到这段程序的效率

  7. c# 函数注释 显示换行 ,

    格式:<para>………..</para> /// <summary> /// <para>把html中的随机汉字转换为图片 调用如下:</par ...

  8. sqlserver 学习

    http://www.cnblogs.com/CareySon/category/411344.html SQL Server查找的最小单位实际上是页.也就是说即使你只查找一行很小的数据,SQL Se ...

  9. CPU再烂,俺也支持虚拟化呀,再附送64位WINDOWS的IIS上配置支持PHP的注意事项

    原来要对IIS进行降权,让他可以支持32位程式     cscript.exe %SYSTEMDRIVE%\inetpub\adminscripts\adsutil.vbs SET W3SVC/App ...

  10. AlarmManager.setRepeating将不再准确

    背景: 当我们想让Android应用程序定时为做一件工作时,我们往往会在一个BroadcastReceiver中使用AlarmManager.setRepeating()方法来实现.在API 19(即 ...