CF1067E Random Forest Rank
Solution
考虑树的邻接矩阵的秩的意义,不难发现相当于每个点找一个“代表”,对于一个点 x x x,它的“代表”为一个与 x x x相邻的结点,要求保证所有点的“代表”不重复,求能找到“代表”的点的最大点集。
稍加思考可以发现这个最大点集等同于最大匹配的两倍,而森林的秩等同于所有树的秩的和,还是最大匹配的两倍。
于是我们要求的东西相当于每个点能够匹配的概率的和。
令
f
x
f_x
fx表示
x
x
x匹配到一个儿子结点的概率。
当
x
x
x的所有儿子结点被匹配时,
x
x
x无法匹配。因此
x
x
x匹配的概率为
1
−
∏
1
+
f
v
2
1-\prod{\frac{1+f_v}{2}}
1−∏21+fv,树形dp即可。
时间复杂度 O ( n ) O(n) O(n)。
Code
#include <bits/stdc++.h>
using namespace std;
template<typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; }
template<typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; }
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;
const lod eps = 1e-9;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 60;
const int mods = 998244353;
const int inv2 = (mods + 1) >> 1;
const int MAXN = 600005;
const int INF = 0x3f3f3f3f; //1061109567
/*--------------------------------------------------------------------*/
namespace FastIO{
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf, *oT = obuf + SIZE - 1, c;
#define gc() (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS ++)) : *iS ++)
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
inline void putc(char c) {
*oS ++ = c;
if (oS == oT) flush();
}
inline void getc(char &c) {
for (c = gc(); !isalpha(c) && c != EOF; c = gc());
}
inline void reads(char *st) {
char c;
int n = 0;
getc(st[++ n]);
for (c = gc(); isalpha(c) ; c = gc()) st[++ n] = c;
}
template<class I>
inline void read(I &x) {
for (f = 1, c = gc(); c < '0' || c > '9' ; c = gc()) if (c == '-') f = -1;
for (x = 0; c >= '0' && c <= '9' ; c = gc()) x = (x << 3) + (x << 1) + (c & 15);
x *= f;
}
template<class I>
inline void print(I x) {
if (x < 0) putc('-'), x = -x;
if (!x) putc('0');
while (x) que[++ num] = x % 10 + 48, x /= 10;
while (num) putc(que[num --]);
}
struct Flusher_{~Flusher_(){flush();}} io_Flusher_;
}
using FastIO :: read;
using FastIO :: putc;
using FastIO :: reads;
using FastIO :: print;
vector<int> e[MAXN];
int f[MAXN], ans = 0;
int upd(int x, int y) { return x + y >= mods ? x + y - mods : x + y; }
void tree_dp(int x, int father) {
for (auto v : e[x]) {
if (v == father) continue;
tree_dp(v, x);
}
int p = 1;
for (auto v : e[x]) {
if (v == father) continue;
p = 1ll * p * (1 + f[v]) % mods * inv2 % mods;
}
f[x] = upd(1, mods - p);
ans = upd(ans, f[x]);
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
#endif
int n, pw = 1;
read(n);
for (int i = 1, u, v; i < n ; ++ i) read(u), read(v), e[u].PB(v), e[v].PB(u), pw = upd(pw, pw);
tree_dp(1, 0);
print(2ll * ans * pw % mods), putc('\n');
return 0;
}