LOJ6496 - 「雅礼集训 2018 Day1」仙人掌

分治套 NTT 优化仙人掌 DP 题目。往这个方向想其实不是很难推,于是就懒得写题解直接贴代码了 qwq。

代码:

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
// =================================
// author: memset0
// date: 2018.12.26 15:53:02
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define pb push_back
#define rep(i, l, r) for (register int i = l; i <= r; i++)

namespace ringo {
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void maxd(T &a, T b) { if (b > a) a = b; }
template <class T> inline void mind(T &a, T b) { if (b < a) a = b; }
template <class T> inline void print(T x, char c) { print(x), putchar(c); }
template <class T> inline T abs(const T &a) { if (a < 0) return -a; return a; }

const int N = 2e5 + 10, p = 998244353;
int n, m, tim, nod, top;
int a[N], dfn[N], low[N], stk[N], f[N][3], g[N][2];
typedef std::vector <int> vector;
vector t[N];

#define walk(i, u, v, G) for (int i = G.hed[u], v = G.to[i]; i; i = G.nxt[i], v = G.to[i])
struct graph {
int tot, hed[N], nxt[N << 1], to[N << 1];
graph () { tot = 2; }
inline void add(int u, int v) { nxt[tot] = hed[u], to[tot] = v, hed[u] = tot++; }
} G, P;

int inv(int x) { return !x || x == 1 ? 1 : (ll)(p - p / x) * inv(p % x) % p; }
inline int fpow(int a, int b) {
int s; for (s = 1; b; b >>= 1, a = (ll)a * a % p)
if (b & 1) s = (ll)s * a % p;
return s;
}

int _a[N << 2], _b[N << 2], rev[N << 2];
inline void ntt(int *a, int lim, int g) {
for (int i = 0; i < lim; i++) if (i < rev[i]) std::swap(a[i], a[rev[i]]);
for (int len = 1; len < lim; len <<= 1)
for (int i = 0, wn = fpow(g, (p - 1) / (len << 1)); i < lim; i += (len << 1))
for (int j = 0, w = 1; j < len; j++, w = (ll)w * wn % p) {
int x = a[i + j], y = (ll)w * a[i + j + len] % p;
a[i + j] = (x + y) % p, a[i + j + len] = (x - y + p) % p;
}
}
inline vector operator * (const vector &a, const vector &b) {
if (!a.size()) return b;
if (!b.size()) return a;
int lim = 1, k = 0;
while (lim <= (int)(a.size() + b.size())) lim <<= 1, ++k;
for (int i = 0; i < (int)a.size(); i++) _a[i] = a[i];
for (int i = 0; i < (int)b.size(); i++) _b[i] = b[i];
for (int i = a.size(); i < lim; i++) _a[i] = 0;
for (int i = b.size(); i < lim; i++) _b[i] = 0;
for (int i = 0; i < lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (k - 1));
ntt(_a, lim, 3), ntt(_b, lim, 3);
for (int i = 0; i < lim; i++) _a[i] = (ll)_a[i] * _b[i] % p;
ntt(_a, lim, 332748118), lim = inv(lim);
vector c(a.size() + b.size() - 1);
for (int i = 0; i < (int)c.size(); i++) c[i] = (ll)_a[i] * lim % p;
return c;
}

void tarjan(int u, int from) {
dfn[u] = low[u] = ++tim, stk[++top] = u;
walk(i, u, v, P)
if (!dfn[v]) {
tarjan(v, i), mind(low[u], low[v]);
if (low[v] == dfn[u]) {
G.add(u, ++nod);
do G.add(nod, stk[top]);
while (stk[top--] != v);
} else if (low[v] > dfn[u])
G.add(u, v), top--;
} else if ((i ^ 1) != from) mind(low[u], dfn[v]);
}

vector solve(int l, int r) {
if (l == r) return t[l];
int mid = (l + r) >> 1; vector _l = solve(l, mid), _r = solve(mid + 1, r);
return _l * _r;
}

void dfs(int u) {
walk(i, u, v, G) dfs(v);
if (u <= n) {
if (!G.hed[u]) { f[u][0] = a[u] >= 0, f[u][1] = a[u] >= 1, f[u][2] = a[u] >= 2; return; }
int cnt = 0;
walk(i, u, v, G) {
t[++cnt].clear();
if (v <= n) t[cnt].pb(f[v][1]), t[cnt].pb(f[v][0]);
else t[cnt].pb(f[v][2]), t[cnt].pb(f[v][1]), t[cnt].pb(f[v][0]);
}
vector now = solve(1, cnt); now.resize(a[u] + 1);
for (int i = 1; i < (int)now.size(); i++) now[i] = (now[i] + now[i - 1]) % p;
if (a[u] >= 0) f[u][0] = now[a[u]];
if (a[u] >= 1) f[u][1] = now[a[u] - 1];
if (a[u] >= 2) f[u][2] = now[a[u] - 2];
} else {
std::vector <int> son;
walk(i, u, v, G) son.pb(v);
for (int pre = 0; pre < 2; pre++) {
g[0][pre] = 1, g[0][pre ^ 1] = 0;
for (int i = 0, v; i < (int)son.size(); i++) {
v = son[i];
g[i + 1][0] = ((ll)g[i][0] * f[v][1] + (ll)g[i][1] * f[v][2]) % p;
g[i + 1][1] = ((ll)g[i][0] * f[v][0] + (ll)g[i][1] * f[v][1]) % p;
}
if (pre) (f[u][2] += g[son.size()][0]) %= p, (f[u][1] += g[son.size()][1]) %= p;
else (f[u][1] += g[son.size()][0]) %= p, (f[u][0] += g[son.size()][1]) %= p;
}
}
}

void main() {
read(n), read(m);
for (int i = 1, u, v; i <= m; i++) read(u), read(v), P.add(u, v), P.add(v, u);
for (int i = 1; i <= n; i++) read(a[i]);
nod = n, tarjan(1, 0), dfs(1);
print(f[1][0], '\n');
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×