-
Notifications
You must be signed in to change notification settings - Fork 0
/
connected_graph.cpp
148 lines (139 loc) · 3.03 KB
/
connected_graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 51;
struct BigInt;
BigInt &countConnectedGraphs(int n);
BigInt &countNonConnectedGraph(int n);
inline BigInt &countAllGraphs(int n);
struct BigInt{
vector<char> v;
BigInt(){
*this = 0;
}
BigInt(int x){
*this = x;
}
BigInt &operator=(int x){
v.clear();
do v.push_back(x%10);while(x/=10);
return *this;
}
BigInt &operator=(const BigInt x){
v.resize(x.v.size());
memcpy(const_cast<char *>(v.data()),x.v.data(),x.v.size()*sizeof(char));
return *this;
}
};
ostream &operator<<(ostream &out,const BigInt &x){
for(int i = x.v.size()-1;i >=0; i--)out<<(char)(x.v[i]+'0');
return out;
}
BigInt operator+(const BigInt &a,const BigInt &b){
BigInt result;
result.v.clear();
bool flg = false;
for(int i = 0;i < (int) max(a.v.size(),b.v.size());i++){
int tmp = 0;
if(i < (int)a.v.size())tmp += a.v[i];
if(i < (int)b.v.size())tmp += b.v[i];
if(flg)tmp++;
flg = false;
if(tmp >= 10)tmp -= 10,flg = true;
result.v.push_back(tmp);
}
if(flg)result.v.push_back(1);
return result;
}
BigInt &operator+=(BigInt &a,const BigInt &b){
a = a+b;
return a;
}
BigInt operator-(const BigInt &a,const BigInt &b){
BigInt res;
res.v.clear();
bool flg = false;
for(int i = 0;i < a.v.size();i++){
int tmp = a.v[i];
if(i < b.v.size())tmp -= b.v[i];
if(flg)tmp--,flg = false;
if(tmp < 0)tmp += 10,flg = true;
res.v.push_back(tmp);
}
int sz = res.v.size();
while(sz >1&&res.v[sz-1]==0)sz--;
res.v.resize(sz);
return res;
}
BigInt operator*(const BigInt &a,const BigInt &b){
BigInt res;
res.v.resize(a.v.size()+b.v.size());
for(int i = 0;i < a.v.size();i++)
for(int j = 0;j < b.v.size();j++){
res.v[i+j] += a.v[i]*b.v[j];
res.v[i+j+1] += res.v[i+j]/10;
res.v[i+j] %= 10;
}
int size = res.v.size();
while(size>1&&res.v[size-1]==0)size--;
res.v.resize(size);
return res;
}
BigInt combo[MAXN][MAXN];
void combination(){
for(int i = 1; i < MAXN;i++){
combo[i][i] = 1,combo[i][0] = 1;
for(int j = 1;j < i;j++){
combo[i][j] = combo[i-1][j] + combo[i-1][j-1];
}
}
}
inline BigInt& C(int n,int k){
return combo[n][k];
}
BigInt &countConnectedGraphs(int n){
static bool calced[MAXN];
static BigInt mem[MAXN];
BigInt &ans = mem[n];
if(calced[n])return ans;
calced[n] = true;
ans = countAllGraphs(n)-countNonConnectedGraph(n);
return ans;
}
BigInt &countAllGraphs(int n){
static bool calced[MAXN];
static BigInt mem[MAXN];
BigInt &ans = mem[n];
if(calced[n])return ans;
calced[n] = true;
BigInt x = 2;
int t = n*(n-1)/2;
for(ans = 1;t;t>>=1,x = x*x){
if(t&1)ans = ans*x;
}
return ans;
}
BigInt &countNonConnectedGraph(int n){
static bool calced[MAXN];
static BigInt mem[MAXN];
BigInt &ans = mem[n];
if(calced[n])return ans;
calced[n] = true;
ans = 0;
for(int k = 1;k <= n-1;k++){
ans += C(n-1,k-1)*countConnectedGraphs(k)*countAllGraphs(n-k);
}
return ans;
}
int main(){
combination();
int n;
while(true){
cin>>n;
if(n==0)break;
cout<<countConnectedGraphs(n)<<endl;
}
}