题解:CF1032F Vasya and Maximum Matching

· · 题解

jzp 讲的喵喵 dp 题。

题目大意:对一棵树求删边方案个数使得删完存在唯一最大匹配。

我们容易发现对于一棵树进行删边形成的结构是一个森林,即多棵树。那么可以分析一下一棵树存在唯一最大匹配的条件。

手玩一下样例再构造几种树会发现,每一个结点在最大匹配中必须恰好被一条边选择,否则可以与相邻的点进行交换这样匹配数量就不唯一。

当然还有一种可能是孤立点,即没有边的树。

我们的 dp 方式有眉目了,考虑记 f_{u, 0/1/2} 为 u 是单点 / 与儿子连边 / 与父亲连边时子树内部的删点方案数。

$$f_{u, 0} = \prod _ {v \in g_u} (f_{v, 1} + f_{v, 0})$$ $f_{u, 2}$ 次之,对于所有 $v$ 进行决策,若 $v$ 是孤立点则该边必须删掉,否则可删可不删,故 $f_{v, 1}$ 项要带一个 $2$ 的系数: $$f_{u, 2} = \prod _ {v \in g_u} (2 \cdot f_{v, 1} + f_{v, 0})$$ $f_{u, 1}$ 则是枚举向父亲连边的 $v$,除 $v$ 外剩余转移与 $f_{u, 2}$ 同理: $$f_{u, 1} = \sum _ {v \in g_u} f_{v, 2} \times \prod_{w \ne v} (2 \cdot f_{w, 1} + f_{w, 0})$$ 显然这个式子可以转化成全局积除以单点值,然而会被卡(详见后文);故而我们使用前后缀积的形式。 :::warning[code - 逆元版本] ```cpp #include <bits/stdc++.h> // #define fastio #define int long long #define bw(x) (1 << x) #define eb emplace_back #define pii pair<int, int> #define inf (0x3f3f3f3f3f3f3f3f) #define F(x, v) for (auto x : (v)) #define ALL(x) (x).begin(), (x).end() #define L(i, a, b) for (register int i = (a); i <= (b); ++i) #define R(i, a, b) for (register int i = (a); i >= (b); --i) #define FRE(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout) using namespace std; bool bgmem; #ifdef fastio struct IO { #define ion bw(20) char i[ion], o[ion], *icl = i, *icr = i, *oc = o; char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; } void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; } void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } IO& operator>>(char &c) { return c = gc(), *this; } IO& operator<<(char c) { return pc(c), *this; } IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; } IO& operator>>(auto &x) { return rd(x), *this; } IO& operator<<(auto x) { return pr(x), *this; } } io; #define cin io #define cout io #endif template < const int P > class mint { public: int x; mint(int _ = 0) { x = (_ % P + P) % P; } explicit operator int() { return x; } friend mint operator+(mint x, mint y) { return x.x + y.x < P ? x.x + y.x : x.x + y.x - P; } friend mint operator-(mint x, mint y) { return x.x - y.x < 0 ? x.x - y.x + P : x.x - y.x; } friend mint operator*(mint x, mint y) { return mint((__int128)(x.x * y.x)); } mint& operator+=(mint x) { return (*this) = (*this) + x; } mint& operator-=(mint x) { return (*this) = (*this) - x; } mint& operator*=(mint x) { return (*this) = (*this) * x; } }; inline int cmax(int& x, int c) { return x = max(x, c); } inline int cmin(int& x, int c) { return x = min(x, c); } int tes = 1, cas; namespace qwq { const int N = 3e5 + 5, P = 998244353; int n, u, v; vector< int > g[N]; mint< P > f[N][3], c[N]; mint< P > fpw(mint< P > x, int y, mint< P > ans = 1) { for (; y; y >>= 1) ans *= ((y & 1) ? x : 1), x *= x; return ans; } void dfs(int u, int fa = 0) { c[u] = 1; F(v, g[u]) if (v != fa) dfs(v, u), c[u] *= (f[v][0] + f[v][1] * 2); f[u][0] = f[u][2] = 1; F(v, g[u]) if (v != fa) { f[u][0] *= (f[v][0] + f[v][1]); f[u][1] += f[v][2] * c[u] * fpw(f[v][0] + f[v][1] * 2, P - 2); f[u][2] *= (f[v][0] + f[v][1] * 2); } } void init() {} void clear() {} void solve() { cin >> n; L(i, 1, n - 1) cin >> u >> v, g[u].eb(v), g[v].eb(u); dfs(1); cout << (f[1][0] + f[1][1]).x << "\n"; } } bool edmem; signed main() { // FRE("", ""); #ifndef fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #endif // cin >> tes; qwq::init(); while (++cas <= tes) qwq::clear(), qwq::solve(); #ifdef fastio fwrite(io.o, 1, io.oc - io.o, stdout); #endif cerr << "time : " << (double)clock() / CLOCKS_PER_SEC * 1000 << "ms\n"; cerr << "memory: " << fabs(&edmem - &bgmem) / 1024 / 1024 << "mb\n"; return 0; } // I will never love You Know Who again. =( ``` ::: 然而这份代码被[叉](https://www.luogu.com.cn/discuss/1228893)了。 :::info[code - 前后缀积版本] ```cpp #include <bits/stdc++.h> // #define fastio #define int long long #define bw(x) (1 << x) #define eb emplace_back #define pii pair<int, int> #define inf (0x3f3f3f3f3f3f3f3f) #define F(x, v) for (auto x : (v)) #define ALL(x) (x).begin(), (x).end() #define L(i, a, b) for (register int i = (a); i <= (b); ++i) #define R(i, a, b) for (register int i = (a); i >= (b); --i) #define FRE(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout) using namespace std; bool bgmem; #ifdef fastio struct IO { #define ion bw(20) char i[ion], o[ion], *icl = i, *icr = i, *oc = o; char gc() { return (icl == icr && (icr = (icl = i) + fread(i, 1, ion, stdin), icl == icr)) ? EOF : *icl++; } void pc(char c) { if (oc - o == ion) fwrite(o, 1, ion, stdout), oc = o; *oc++ = c; } void rd(auto &x) { char c; int f = 1; x = 0; while (c = gc(), c < '0' || c > '9') if (c == '-') f = -1; while ('0' <= c && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = gc(); x *= f; } void pr(auto x) { int a[64], p = 0; if (x < 0) x = -x, pc('-'); do a[p++] = x % 10; while (x /= 10); while (p--) pc(a[p] + '0'); } IO& operator>>(char &c) { return c = gc(), *this; } IO& operator<<(char c) { return pc(c), *this; } IO& operator<<(const char *c) { while (*c) pc(*c++); return *this; } IO& operator>>(auto &x) { return rd(x), *this; } IO& operator<<(auto x) { return pr(x), *this; } } io; #define cin io #define cout io #endif template < const int P > class mint { public: int x; mint(int _ = 0) { x = (_ % P + P) % P; } explicit operator int() { return x; } friend mint operator+(mint x, mint y) { return x.x + y.x < P ? x.x + y.x : x.x + y.x - P; } friend mint operator-(mint x, mint y) { return x.x - y.x < 0 ? x.x - y.x + P : x.x - y.x; } friend mint operator*(mint x, mint y) { return mint((__int128)(x.x * y.x)); } mint& operator+=(mint x) { return (*this) = (*this) + x; } mint& operator-=(mint x) { return (*this) = (*this) - x; } mint& operator*=(mint x) { return (*this) = (*this) * x; } }; inline int cmax(int& x, int c) { return x = max(x, c); } inline int cmin(int& x, int c) { return x = min(x, c); } int tes = 1, cas; namespace qwq { const int N = 3e5 + 5, P = 998244353; int n, u, v; vector< int > g[N]; mint< P > f[N][3], c[N]; mint< P > fpw(mint< P > x, int y, mint< P > ans = 1) { for (; y; y >>= 1) ans *= ((y & 1) ? x : 1), x *= x; return ans; } void dfs(int u, int fa = 0) { f[u][0] = f[u][2] = 1; vector< mint< P > > pre(g[u].size() + 1, 1), suf(g[u].size() + 1, 1); L(i, 0, (int)g[u].size() - 1) if (g[u][i] != fa) dfs(g[u][i], u), pre[i] = suf[i] = (f[g[u][i]][0] + f[g[u][i]][1] * 2); L(i, 1, (int)g[u].size() - 1) pre[i] = pre[i - 1] * pre[i]; R(i, (int)g[u].size() - 2, 0) suf[i] = suf[i + 1] * suf[i]; L(i, 0, (int)g[u].size() - 1) { v = g[u][i]; if (v != fa) { f[u][0] *= (f[v][0] + f[v][1]); f[u][1] += f[v][2] * (!i ? 1 : pre[i - 1]) * suf[i + 1]; f[u][2] *= (f[v][0] + f[v][1] * 2); } } } void init() {} void clear() {} void solve() { cin >> n; L(i, 1, n - 1) cin >> u >> v, g[u].eb(v), g[v].eb(u); dfs(1); cout << (f[1][0] + f[1][1]).x << "\n"; } } bool edmem; signed main() { // FRE("", ""); #ifndef fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #endif // cin >> tes; qwq::init(); while (++cas <= tes) qwq::clear(), qwq::solve(); #ifdef fastio fwrite(io.o, 1, io.oc - io.o, stdout); #endif cerr << "time : " << (double)clock() / CLOCKS_PER_SEC * 1000 << "ms\n"; cerr << "memory: " << fabs(&edmem - &bgmem) / 1024 / 1024 << "mb\n"; return 0; } // I will never love You Know Who again. =( ``` :::