-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunlabelled-graphs.cpp
124 lines (116 loc) · 2.53 KB
/
unlabelled-graphs.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
#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;
const lli mod = 998244353;
using pii = pair<int, int>;
const int M = 5000;
int fact[M+1], ncr[M+1][M+1];
lli power(lli a, lli b){
lli ans = 1;
while(b){
if(b & 1) ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
vector<int> operator*(const vector<int> & a, const vector<int> & b){
vector<int> c(a.size() + b.size() - 1);
for(int i = 0; i < a.size(); ++i){
for(int j = 0; j < b.size(); ++j){
c[i+j] += (lli)a[i]*b[j] % mod;
if(c[i+j] >= mod) c[i+j] -= mod;
}
}
return c;
}
vector<int> operator+(const vector<int> & a, const vector<int> & b){
vector<int> c(max(a.size(), b.size()));
for(int i = 0; i < c.size(); ++i){
c[i] = (i < a.size() ? a[i] : 0) + (i < b.size() ? b[i] : 0);
if(c[i] >= mod) c[i] -= mod;
}
return c;
}
vector<int> cnt_cycles(const vector<int> & pi, int n){
vector<bool> vis(n*n);
vector<int> cnt(n*(n-1)/2+1);
for(int i = 0; i < n-1; ++i){
for(int j = i+1; j < n; ++j){
if(!vis[i*n+j]){
int u = i*n+j;
int len = 0;
do{
vis[u] = true;
u = pi[u];
int ip = u/n, jp = u%n;
if(ip > jp) swap(ip, jp);
u = ip*n + jp;
len++;
}while(u != i*n+j);
cnt[len]++;
}
}
}
return cnt;
}
map<vector<int>, int> cycle_index_pair(int n){
vector<int> pi(n);
iota(pi.begin(), pi.end(), 0);
map<vector<int>, int> ans;
do{
vector<int> sigma(n*n);
for(int i = 0; i < n-1; ++i){
for(int j = i+1; j < n; ++j){
sigma[i*n + j] = pi[i]*n + pi[j];
sigma[j*n + i] = pi[j]*n + pi[i];
}
}
ans[cnt_cycles(sigma, n)]++;
}while(next_permutation(pi.begin(), pi.end()));
return ans;
}
vector<int> all_unlabelled_graphs(int n){
vector<int> ans = {0};
for(const auto & a : cycle_index_pair(n)){
const vector<int> & c = a.first;
int coef = a.second;
vector<int> Z = {coef};
for(int i = 1; i <= n*(n-1)/2; ++i){
if(c[i] == 0) continue;
// P = (1+x^i)^c[i]
vector<int> P(i*c[i]+1);
for(int j = 0; j <= c[i]; ++j){
P[i*j] = ncr[c[i]][j];
}
Z = Z * P;
}
ans = ans + Z;
}
int inv = power(fact[n], mod-2);
for(int & x : ans){
x = (lli)x * inv % mod;
}
return ans;
}
int main(){
fact[0] = 1;
for(int i = 1; i <= M; ++i){
fact[i] = (lli)i * fact[i-1] % mod;
}
ncr[0][0] = 1;
for(int i = 1; i <= M; ++i){
ncr[i][0] = ncr[i][i] = 1;
for(int j = 1; j < i; ++j){
ncr[i][j] = (ncr[i-1][j-1] + ncr[i-1][j]) % mod;
}
}
int n;
cin >> n;
vector<int> graphs = all_unlabelled_graphs(n);
for(lli x : graphs){
cout << x << " ";
}
cout << "\n";
return 0;
}