Connected Graph
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 3010 |
|
Accepted: 1481 |
Description
An undirected graph is a set V of vertices and a set of e∈{v*v} edges. An undirected graph was connected if and only if for every pair (u,v) of Vertices,u are reachable from v.
You is to write a program, tries to calculate, the number of different connected undirected graph with n vertices.
For Example,there is 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. Assume that 1<=n<=50. The last test case was followed by one zero.
Output
The For each test case output the answer to a single line.
Sample Input
12340
Sample Output
11438
Source
[email protected]The title of the leader. The first question should be relatively simple compared to other topics. In fact, it was mentioned in Rujia's book. Set F (n) as the answer, G (n) is the number of non-connected graphs with n vertices, and H (n) is the total scheme number. Obviously, for H (n) we can find out directly, because there are two cases for each edge, there are N (n-1)/2 edges, so f (n) + g (n) = h (n) = 2n* (n-1)/2. Then we ask what the G (n) is. Select a point as the center, assuming that the V1, then together with V1 to become a connected block of the scheme number is sum{c (n-1,i-1) *f (i) *h (N-i)}, which represents n points in addition to V1 points, to take out i-1 vertices and V1 into the I point of the connected graph scheme number, of course, According to the multiplication principle, there is an H (n-i) condition in addition to the n-i points outside of the I point, so two results are multiplied.
The most pit father is with the decimal high precision will time out, we have to use MJ system to save time. The time complexity of this algorithm is O (N2) (except for high precision).
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;structbigint{Static Const intBASE =10000; ints[ +]; intLen; BigInt () {memset (s),0,sizeof(s)); Len=0; S[len++] =0; } BigIntoperator= (Const int&b) {memset (s),0,sizeof(s)); Len=0; S[len++] =b; inti =0; while(S[i] >BASE) {S[len+ +] = (S[i]/BASE); S[i]%=BASE; } return(* This); } BigIntoperator+ (Const int&b) {s[0] +=b; for(inti =1; i < Len; ++i) {s[i]+ = S[i-1] /BASE; S[i-1] %=BASE; } if(S[len-1] >BASE) {S[len]= S[len-1] /BASE; S[len-1] %=BASE; Len++; } return(* This); } BigIntoperator+ (ConstBigInt &b)Const{BigInt C; C.len=Max (B.len, Len); for(inti =0; i < C.len; ++i) {c.s[i]+ = S[i] +B.s[i]; C.s[i+1] + = C.s[i]/BASE; C.s[i]%=BASE; } while(C.s[c.len]) {C.s[c.len+1] = C.s[c.len]/BASE; C.s[c.len++] %=BASE; } returnC; } BigIntoperator* (intx) { for(inti =0; i < Len; ++i) S[i] *=x; for(inti =0; i < Len; ++i) {s[i+1] + = S[i]/BASE; S[i]%=BASE; } while(S[len]) {S[len+1] = S[len]/BASE; S[len++] %=BASE; } return(* This); } BigIntoperator* (ConstBigInt &b)Const{BigInt C; intx, M; for(inti =0; i < B.len; ++i) {x=0; for(intj =0; J < Len; ++j) {C.s[i+ j] + + x + b.s[i] *S[j]; X= C.s[i + j]/BASE; C.s[i+ j]%=BASE; } c.s[i+ len] + =x; } C.len= B.len +Len; while(C.len >1&& C.s[c.len-1] ==0) c.len--; while(C.s[c.len]) {C.s[c.len+1] = C.s[c.len]/BASE; C.s[c.len]%=BASE; } returnC; } BigIntoperator- (ConstBigInt &b) {BigInt C; memcpy (C.S, S,sizeof(s)); C.len=Len; for(inti =0; i < B.len; ++i) {c.s[i]= C.s[i]-B.s[i]; if(C.s[i] <0) {C.s[i]= BASE +C.s[i]; C.s[i+1]--; } } while(C.len >1&& C.s[c.len-1] ==0) c.len--; returnC; } BigIntoperator/ (Const int&B)Const{BigInt C; C=0; intx =0; for(inti = len-1; I >=0; --i) {x= x * BASE +S[i]; if(x >=b) {c= c * BASE + x/b; X%=b; } Elsec = c *BASE; } returnC; } };Const intMAXN = -; BigInt H[MAXN], G[MAXN], F[MAXN];intN; BigInt C (intMintk) {BigInt ans; Ans=1; for(inti = M-k +1; I <= m; ++i) ans = ans *i; for(inti =1; I <= K; ++i) ans = ans/i; returnans;}intMain () {h[0] =1; h[1] =1; f[1] =1; g[1] =0; for(inti =2; I <= -; ++i) {H[i]=1; for(intj =0; J < I * (i-1) /2; ++J) H[i] = h[i] *2; for(intj =1; J < I; ++j) G[i]= G[i] + (C (i-1, J-1) * F[J] * h[i-J]); F[i]= H[i]-G[i]; } while(SCANF ("%d", &n) = =1&&N) {printf ("%d", F[n].s[f[n].len-1]); for(inti = F[n].len-2; I >=0; -i) printf ("%04d", F[n].s[i]); printf ("\ n"); } return 0;}
"POJ 1737" Connected Graph