Connected Graph
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3459 | Accepted: 1679 |
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.
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
啊啊啊 太棒了 这个题的英文我都认识
设 f[i] 表示 i 个点的答案
之后考虑加入 n 号点 统计即可以 n 为枢纽用不同的方案链接所有的点
套路枚举 1 所在联通块就好
更详细可以看这个
http://blog.youkuaiyun.com/blackjack_/article/details/78377228
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=210,rad=1000;
struct bint
{
int a[N];
bint(){memset(a,0,sizeof(a));a[0]=1;}
friend bint operator +(const bint &x,const bint &y)
{
bint res;
res.a[0]=max(x.a[0],y.a[0]);
for(int i=1;i<=res.a[0];++i)
res.a[i]=x.a[i]+y.a[i];
for(int i=1;i<=res.a[0];++i)
{
if(res.a[i]>=rad)
{
res.a[i+1]+=res.a[i]/rad;res.a[i]%=rad;
res.a[0]=max(res.a[0],i+1);
}
}
return res;
}
friend bint operator *(const bint &x,int y)
{
bint res=x;
for(int i=1;i<=res.a[0];++i) res.a[i]*=y;
for(int i=1;i<=res.a[0];++i)
{
if(res.a[i]>=rad)
{
res.a[i+1]+=res.a[i]/rad;res.a[i]%=rad;
res.a[0]=max(res.a[0],i+1);
}
}
return res;
}
friend bint operator *(const bint &x,const bint &y)
{
bint res;res.a[0]=x.a[0]+y.a[0]-1;
for(int i=1;i<=x.a[0];++i)
for(int j=1;j<=y.a[0];++j)
res.a[i+j-1]+=x.a[i]*y.a[j];
for(int i=1;i<=res.a[0];++i)
{
if(res.a[i]>=rad)
{
res.a[i+1]+=res.a[i]/rad;res.a[i]%=rad;
res.a[0]=max(res.a[0],i+1);
}
}
return res;
}
friend void print_bint(const bint &x)
{
printf("%d",x.a[x.a[0]]);
for(int i=x.a[0]-1;i;i--)
printf("%03d",x.a[i]);
putchar('\n');
}
}c[51][51],f[51],pw[51];
void initial()
{
register int i,j,s;
bint tmp;tmp.a[1]=1;
for(i=1;i<=50;++i) pw[i]=pw[i-1]*2+tmp;
c[0][0].a[1]=1;
for(i=1;i<=50;++i)
{
c[i][0].a[1]=1;
for(j=1;j<=i;++j)
c[i][j]=c[i-1][j]+c[i-1][j-1];
}
f[1].a[1]=1;
for(i=2;i<=50;++i)
for(s=1;s<=i-1;++s)
f[i]=f[i]+c[i-2][s-1]*f[s]*f[i-s]*pw[s];
}
int main()
{
initial();
while(1)
{
int n=read();
if(!n) break;
print_bint(f[n]);
}
return 0;
}