http://www.elijahqi.win/2018/01/14/poj1737-connected-graph/
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.
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
Source
LouTiancheng@POJ
必须膜icefox巨佬 真的太强啦qwq
这题很神 leoly:常说:输入一个数 输出一个数 是不是很妙啊
题意:求给N个节点 问n个节点全连通的个数有多少种
设f[n]表示n个节点我的连通图个数
怎么做 首先假设我需要将图分成两个只通过一条边连接的图那么两边我都可以用f[k],f[n-k]来表示状态 此时我两边各选一个代表将其固定下来 假设左边是k个右边是n-k个 那么左边满足连通 至少是我选定的那个点和其他k-1个点都有边这k-1个点怎么选
Ck−1n−2
好了这样我强行钦定左边连通了 那么k个点连通的方案数我有了就是f[k]这是选出哪k个点 同时我还需要满足我这两个连通块是连通的那么我需要通过另外一个连通块内钦定的点相连 那么一共是
2k−1
种方案 因为不能不连通
最后总的递推式子写出
f[n]=∑n−1k=1f[k]∗f[n−k]∗Ck−1n−2∗(2k−1)
得写高精orz
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 55
ll bin[N];
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x;
}
struct bigint{
ll d[N*N];int n;
ll& operator[](int x){return d[x];}
bigint(){memset(d,0,sizeof(d));n=0;}
friend bigint operator+(bigint a,bigint b){
bigint res;res.n=max(a.n,b.n);
for (int i=1;i<=res.n;++i) res[i]=a[i]+b[i];
for (int i=1;i<=res.n;++i) res[i+1]+=res[i]/10,res[i]%=10;
if (res[res.n+1]) ++res.n;return res;
}
friend bigint operator*(bigint a,bigint b){
bigint res;res.n=a.n+b.n-1;
for (int i=1;i<=a.n;++i) for (int j=1;j<=b.n;++j) res[i+j-1]+=a[i]*b[j];
for (int i=1;i<=res.n;++i) res[i+1]+=res[i]/10,res[i]%=10;
while(res[res.n+1]) ++res.n,res[res.n+1]+=res[res.n]/10,res[res.n]%=10;
return res;
}
friend bigint operator*(bigint a,ll b){
bigint res;res.n=a.n;
for (int i=1;i<=res.n;++i) res[i]=a[i]*b;
for (int i=1;i<=res.n;++i) res[i+1]+=res[i]/10,res[i]%=10;
while(res[res.n+1]) ++res.n,res[res.n+1]+=res[res.n]/10,res[res.n]%=10;
return res;
}
}f[N];
ll c[N][N];
inline void print(bigint a){
for (int i=a.n;i;--i) printf("%lld",a[i]);puts("");
}
int main(){
freopen("poj1737.in","r",stdin);
for (int i=0;i<51;++i) bin[i]=1LL<<i;
for (int i=0;i<=48;++i) c[i][0]=1;
for (int i=1;i<=48;++i)
for (int j=1;j<=i;++j) c[i][j]=c[i-1][j-1]+c[i-1][j];
f[1].n=1;f[1][1]=1;f[2].n=1;f[2][1]=1;
for (int i=3;i<=50;++i)
for (int j=1;j<i;++j)
f[i]=f[i]+f[j]*f[i-j]*(bin[j]-1)*c[i-2][j-1];
//f[3]=f[2]*5;print(f[3]);
while(1){
int n=read();if (!n) return 0;
print(f[n]);
}
return 0;
}