[Gym102268I] Interesting Graph

做法

一个图的染色方案数等于各个点双的染色方案数乘积除以颜色数的(点双数 - 连通块数)次方(考虑对每个连通块在圆方树上从上往下对每个点双染色)。

根据题目中的条件,一个点双的大小不会超过 \(7\),暴力枚举集合划分可以求出一个点双的色多项式,分治 fft 一下即可得到给定的图的色多项式,然后多点求值一下即可。

(为啥一定要给各种染色方案数题套个多点求值...好无聊)

完全不会写点双..调了一天。

代码

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pi;

const int maxn = 100010;
const int mod = 998244353;
const int g = 3;

int n, m, C;

namespace NTT {
int tot, fac[maxn], ifac[maxn], inv[maxn], ans[maxn];
int wa[maxn*16], wb[maxn*16], wc[maxn*16], rev[maxn*16];

int qpow(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = 1LL * ret * x % mod;
x = 1LL * x * x % mod;
y >>= 1;
}
return ret;
}

struct poly {
int *a, len;
poly(int len_=0) {
len = len_;
a = new int[len];
for (int i = 0; i < len; i++) {
a[i] = 0;
}
}
int cal(int x) {
int t = 1;
int ret = 0;
for (int i = 0; i < len; i++) {
ret = (ret + 1LL * t * a[i] % mod) % mod;
t = 1LL * t * x % mod;
}
return ret;
}
} p[maxn], P[maxn << 2];

void ntt(int *a, int l, int ty) {
rev[0] = 0;
for (int i = 1; i < (1<<l); i++) rev[i] = ((rev[i >> 1] >> 1) | ((i&1) << (l-1)));
for (int i = 0; i < (1<<l); i++) if (rev[i] > i) swap(a[i], a[rev[i]]);
for (int len = 2; len <= (1<<l); len <<= 1) {
int wn = qpow(g, (mod-1)/len);
for (int i = 0; i < (1<<l); i += len) {
int w = 1;
for (int j = 0; j < (len>>1); j++) {
int v1 = a[i+j], v2 = 1LL*w*a[i+j+(len>>1)]%mod;
a[i+j] = (v1 + v2) % mod;
a[i+j+(len >> 1)] = (v1 + mod - v2) % mod;
w = 1LL*w*wn%mod;
}
}
}
if (ty == -1) {
int inv = qpow(1<<l, mod-2);
for (int i = 0; i < (1<<l); i++) a[i] = 1LL*a[i]*inv%mod;
if (l) for (int i = 1; i < (1<<(l-1)); i++) swap(a[i], a[(1<<l)-i]);
}
}

poly operator*(const poly &p1, const poly &p2) {
poly ret(p1.len + p2.len - 1);
/*for (int i = 0; i < p1.len; i++) {
for (int j = 0; j < p2.len; j++) {
ret.a[i+j] = (ret.a[i+j] + 1LL * p1.a[i] * p2.a[j] % mod) % mod;
}
}*/
//return ret;
int l = 0; while ((1<<l) < ret.len) ++ l;
for (int i = 0; i < (1<<l); i++) wa[i] = wb[i] = 0;
for (int i = 0; i < p1.len; i++) wa[i] = p1.a[i];
for (int i = 0; i < p2.len; i++) wb[i] = p2.a[i];
ntt(wa, l, 1); ntt(wb, l, 1);
for (int i = 0; i < (1<<l); i++) wc[i] = 1LL*wa[i]*wb[i]%mod;
ntt(wc, l, -1);
for (int i = 0; i < ret.len; i++) ret.a[i] = wc[i];
return ret;
}

poly polyInv(const poly &p) {
if (p.len == 1) {
poly ret(1);
ret.a[0] = qpow(p.a[0], mod-2);
return ret;
}
int l = p.len;
int tl = (l + 1) / 2;
poly p0(tl);
for (int i = 0; i < tl; i++) p0.a[i] = p.a[i];
poly q0 = polyInv(p0);
poly t0 = q0 * p; t0.a[0] = (t0.a[0] + mod - 2) % mod; t0.len = p.len;
for (int i = 0; i < l; i++) t0.a[i] = (mod - t0.a[i]) % mod;
poly ret = q0 * t0;
ret.len = l;
return ret;
}

poly polyMod(const poly &p, const poly &q) {
if (q.len > p.len) return p;
poly pr(p.len - q.len + 1), qr(p.len - q.len + 1);
for (int i = 0; i < pr.len; i++) pr.a[i] = p.a[p.len - 1 - i];
for (int i = 0; i < qr.len; i++) if (q.len - 1 - i >= 0) qr.a[i] = q.a[q.len - 1 - i];
poly dr = polyInv(qr) * pr; dr.len = p.len - q.len + 1;
poly d(dr.len);
for (int i = 0; i < d.len; i++) d.a[i] = dr.a[dr.len - 1 - i];
poly res = d * q;
poly ret(q.len - 1);
for (int i = 0; i < ret.len; i++) {
if (i < res.len) ret.a[i] = (p.a[i] + mod - res.a[i]) % mod;
else ret.a[i] = p.a[i];
}
return ret;
}

void _solve(int l, int r, int rt) {
if (l == r) {
P[rt] = poly(2);
P[rt].a[0] = mod - l, P[rt].a[1] = 1;
return;
}
int m = (l + r) >> 1;
_solve(l, m, rt<<1);
_solve(m+1, r, rt<<1|1);
P[rt] = P[rt<<1] * P[rt<<1|1];
}

void calAns(const poly &p, int l, int r, int rt) {
if (l == r) {
ans[l] = p.a[0];
return;
}
int m = (l + r) >> 1;
calAns(polyMod(p, P[rt<<1]), l, m, rt<<1);
calAns(polyMod(p, P[rt<<1|1]), m+1, r, rt<<1|1);
}

poly cal(int l, int r) {
if (l == r) return p[l];
int m = (l + r) >> 1;
return cal(l, m) * cal(m+1, r);
}

void solve() {
fac[0] = ifac[0] = 1;
inv[1] = 1;
for (int i = 2; i <= m; i++) inv[i] = mod - 1LL * inv[mod % i] * (mod / i) % mod;
for (int i = 1; i <= m; i++) {
fac[i] = 1LL * fac[i-1] * i % mod;
ifac[i] = 1LL * ifac[i-1] * inv[i] % mod;
}
poly _res = cal(1, tot);
poly res(n + m + 1);
for (int i = 0; i <= n + m; i++) {
if (i + C < _res.len) {
res.a[i] = _res.a[i + C];
}
}
// cout << res.cal(3) << endl;
_solve(1, n, 1);
calAns(polyMod(res, P[1]), 1, n, 1);
for (int i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
puts("");
}
}

int l[maxn], vis[maxn], dfn[maxn], low[maxn], tim, e, top;
pi sta[maxn];
vector<int> son[maxn], va[maxn];
vector<int> vertex_set;
vector<pi> edge_set;
vector<int> G[maxn];

struct Edge {
int v, x;
} E[maxn << 1];

inline void addEdge(int u, int v) {
E[e].v = v, E[e].x = l[u], l[u] = e++;
}

// 第一类斯特林数
int S[10][10];

// 下降幂系数
int _a[10], _b[10], vc[10], col[maxn];

void __dfs(int in, int mx) {
if (in >= vertex_set.size()) {
++ _a[mx];
return;
}
for (int i = 0; i <= 7; i++) vc[i] = 0;
int u = vertex_set[in];
for (int _ = 0; _ < G[u].size(); _++) {
int v = G[u][_];
vc[col[v]] = 1;
}
vector<int> ok_col;
for (int i = 1; i <= mx; i++) {
if (!vc[i]) {
ok_col.push_back(i);
}
}
for (int _ = 0; _ < ok_col.size(); _++) {
int c = ok_col[_];
col[u] = c;
__dfs(in + 1, mx);
col[u] = 0;
}
col[u] = mx + 1;
__dfs(in + 1, mx + 1);
col[u] = 0;
}

NTT::poly cal() {
sort(vertex_set.begin(), vertex_set.end());
vertex_set.erase(unique(vertex_set.begin(), vertex_set.end()), vertex_set.end());
for (int _ = 0; _ < vertex_set.size(); _++) {
int u = vertex_set[_];
col[u] = 0;
G[u].clear();
for (int i = 0; i < va[u].size(); i++) {
edge_set.push_back(pi(u, va[u][i]));
}
//printf("%d ", u);
}
//puts("");
for (int _ = 0; _ < edge_set.size(); _++) {
int u = edge_set[_].first, v = edge_set[_].second;
G[u].push_back(v), G[v].push_back(u);
}
for (int i = 0; i <= 7; i++) {
_a[i] = _b[i] = 0;
vc[i] = 0;
}
int s = vertex_set.size();
if (s > 7) exit(-1);
__dfs(0, 0);
NTT::poly ret(s + 1);
for (int i = 0; i <= s; i++) {
for (int j = 0; j <= i; j++) {
int K = S[i][j];
if ((i + j) & 1) K = (mod - K) % mod;
_b[j] = (_b[j] + 1LL * K * _a[i] % mod) % mod;
}
}
for (int i = 0; i <= s; i++) {
ret.a[i] = _b[i];
}
return ret;
}

void dfs(int u, int f) {
dfn[u] = low[u] = ++ tim, vis[u] = 1;
for (int p = l[u]; p >= 0; p = E[p].x) {
int v = E[p].v;
if (!dfn[v]) {
son[u].push_back(v);
sta[++ top] = pi(u, v);
dfs(v, u);
low[u] = min(low[u], low[v]);
} else if (v != f) {
low[u] = min(low[u], dfn[v]);
if (vis[v]) {
va[u].push_back(v);
}
}
}
if (f && low[u] >= dfn[f]) {
pi t; edge_set.clear(); vertex_set.clear();
do {
t = sta[top --];
edge_set.push_back(t);
vertex_set.push_back(t.first), vertex_set.push_back(t.second);
} while (t != pi(f, u));
++ C;
NTT::p[++ NTT::tot] = cal();
}
vis[u] = 0;
}

int main() {
memset(l, -1, sizeof(l));
S[0][0] = 1;
for (int i = 1; i <= 7; i++) {
for (int j = 1; j <= i; j++) {
S[i][j] = S[i-1][j-1] + 1LL * (i-1) * S[i-1][j] % mod;
}
}
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
addEdge(u, v), addEdge(v, u);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
int _tim = tim;
-- C;
dfs(i, 0);
if (tim == _tim + 1) {
vertex_set.clear(); edge_set.clear();
vertex_set.push_back(i);
++ C;
NTT::p[++ NTT::tot] = cal();
}
}
}
NTT::solve();
return 0;
}