loj2542 「PKUWC2018」随机游走 MinMax 容斥+树上高斯消元+状压 DP

题目传送门

https://loj.ac/problem/2542

题解

肯定一眼 MinMax 容斥吧。

然后问题就转化为,给定一个集合 \(S\),问期望情况下多少步可以走到 \(S\) 中的点。

考虑 dp 的话,令 \(dp[x]\) 表示从 \(x\) 开始走的答案。

如果 \(x \in S\),那么 \(dp[x] = 0\)

否则,\(dp[x] = 1 + \frac{\sum\limits_{(x, y) \in T} dp[y]}{deg_x}\)

这个东西直接树上高斯消元可以 \(O(n\log P)\) 求出来,\(\log P\) 是求逆元。

然后求出来了以后,令 \(f[S]\) 表示期望情况下多少步可以走到 \(S\) 中的点。如果 \(|T|\) 是偶数,那么久取反。然后可以用 SoSDP 来求出所有子集的贡献和。

所以总时间复杂度为 \(O(n2^n\log P)\)


关于树上高斯消元

大概就是从底向上把每个点的 \(dp\) 值(下面为了方便写成 \(f(x)\))写成 \(f(x) = A_xf(fa_x) + B_x\) 的形式。

比如说前面的 \(dp[x] = 1 + \sum\limits_{(x, y) \in E} dp[y]\)

首先叶子一定是 \(f(x) = 1 \cdot f(fa) + 0\),即 \(A_x = 1, B_x = 0\)

然后对于非叶子来说,上面的转移可以表示成

\[f(x) = 1 + \frac{f(fa_x) + \sum\limits_{(x, y) \in T} f(y)}{deg_x} \]

\(deg_x\) 移动到左边

\[deg_x\cdot f(x) = deg_x + f(fa_x) + \sum\limits_{(x, y) \in T} f(y) \]

\(f(y)\) 替换为 \(A_yf(x) + B_y\)

\[deg_x\cdot f(x) = deg_x + f(fa_x) + \sum\limits_{(x, y) \in T} A_yf(x) + B_y \]

移项

\[(deg_x - \sum_{(x, y) \in T} A_y) f(x) = f(fa_x) + deg_x + \sum_{(x , y) \in T} B_y \]

把左边的系数除过去

\[f(x) = \frac 1{(deg_x - \sum\limits_{(x, y) \in T} A_y)} f(fa_x) + \frac {deg_x + \sum\limits_{(x , y) \in T} B_y}{deg_x - \sum\limits_{(x, y) \in T} A_y} \]

\[\begin{align*}f(x) &= \frac 1{(deg_x - \sum\limits_{(x, y) \in T} A_y)}\\f(y) &= \frac {deg_x + \sum\limits_{(x , y) \in T} B_y}{deg_x - \sum\limits_{(x, y) \in T} A_y}\end{align*} \]

最后根节点的 \(B\) 值就是根的 \(dp\) 值(根的 \(A\) 一定为 \(0\))。如果还需要别的点的 \(dp\) 值就回带回去就可以了。


下面是代码。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back

template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}

typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;

template<typename I> inline void read(I &x) {
	int f = 0, c;
	while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
	x = c & 15;
	while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
	f ? x = -x : 0;
}

const int N = 18 + 7;
const int M = (1 << 18) + 7;
const int P = 998244353;

int n, Q, st, S;
int dp[M], ba[N];
int A[N], B[N];

struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }

inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
	int ans = 1;
	for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
	return ans;
}

inline void dfs(int x, int fa = 0) {
	if (ba[x]) return (void)(A[x] = B[x] = 0);
	int suma = 0, sumb = 0, deg = !!fa;
	for fec(i, x, y) if (y != fa) {
		dfs(y, x), ++deg;
		sadd(suma, A[y]), sadd(sumb, B[y]);
	}
	A[x] = fpow(deg + P - suma, P - 2);
	B[x] = (ll)(sumb + deg) * A[x] % P;
}

inline void gauss() {
	S = (1 << n) - 1;
	for (int s = 1; s <= S; ++s) {
		int cnt = 0;
		for (int i = 1; i <= n; ++i) ba[i] = (s >> (i - 1)) & 1, cnt += ba[i];
		dfs(st);
		if (cnt & 1) dp[s] = B[st];
		else dp[s] = smod(P - B[st]);
	}
}

inline void sos() {
	for (int i = 0; i < n; ++i)
		for (int s = 0; s <= S; ++s)
			if ((s >> i) & 1) sadd(dp[s], dp[s ^ (1 << i)]);
}

inline void work() {
	gauss();
	sos();
	while (Q--) {
		int m, x, s = 0;
		read(m);
		while (m--) read(x), s |= 1 << (x - 1);
		printf("%d\n", dp[s]);
	}
}

inline void init() {
	read(n), read(Q), read(st);
	int x, y;
	for (int i = 1; i < n; ++i) read(x), read(y), adde(x, y);
}

int main() {
#ifdef hzhkk
	freopen("hkk.in", "r", stdin);
#endif
	init();
	work();
	fclose(stdin), fclose(stdout);
	return 0;
}
posted @ 2019-09-28 11:38  hankeke303  阅读(213)  评论(0编辑  收藏  举报