POJ 1737 统计有n个顶点的连通图有多少个 (带标号)
2013-10-08 22:13
197 查看
设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)
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 ); } } }
相关文章推荐
- POJ 1737 统计有n个顶点的连通图有多少个 (带标号)
- Zoj 1119 POJ 1523 SPF 求关节点及删除关节点会出现多少个连通分量 Tarjan算法
- poj 1966 Cable TV Network 顶点连通度
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
- (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)
- (Relax DFS1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)
- poj 2186 Popular Cows 给定一个有向图,求有多少个顶点是由任何顶点出发都可达的 Kosaraju算法+缩点
- POJ 1611 The Suspects 并查集 (统计不同一个树的其他树有多少个)
- poj 1236 Network of Schools 1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 2)至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点
- POJ 1611 The Suspects 并查集 (统计不同一个树的其他树有多少个)
- POJ 1611 The Suspects 并查集 (统计不同一个树的其他树有多少个)
- 网络流之顶点连通度 poj 1966 zoj 2182
- POJ 1611 The Suspects 并查集 (统计不同一个树的其他树有多少个)
- (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)
- POJ 1611 The Suspects 并查集 (统计不同一个树的其他树有多少个)
- POJ 1611 The Suspects 并查集 (统计不同一个树的其他树有多少个)
- hrbustOJ 欧拉路径(判断无向图是否连通+统计顶点的度数)
- POJ 1966 / ZOJ 2182 : Cable TV Network - 顶点连通度
- 给定一个连通图 q个询问 每次加一条边 每次求出有多少个桥 poj 3694
- POJ 1611 The Suspects 并查集 (统计不同一个树的其他树有多少个)