• 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{sumlimits_{(x, y) in T} dp[y]}{deg_x})

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

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

    所以总时间复杂度为 (O(n2^nlog P))


    关于树上高斯消元

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

    比如说前面的 (dp[x] = 1 + sumlimits_{(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) + sumlimits_{(x, y) in T} f(y)}{deg_x} ]

    (deg_x) 移动到左边

    [deg_xcdot f(x) = deg_x + f(fa_x) + sumlimits_{(x, y) in T} f(y) ]

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

    [deg_xcdot f(x) = deg_x + f(fa_x) + sumlimits_{(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 - sumlimits_{(x, y) in T} A_y)} f(fa_x) + frac {deg_x + sumlimits_{(x , y) in T} B_y}{deg_x - sumlimits_{(x, y) in T} A_y} ]

    [egin{align*}f(x) &= frac 1{(deg_x - sumlimits_{(x, y) in T} A_y)}\f(y) &= frac {deg_x + sumlimits_{(x , y) in T} B_y}{deg_x - sumlimits_{(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
    ", 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;
    }
    
  • 相关阅读:
    [转载]AXIS学习笔记
    [转]MQ(JMS) 的姊妹篇,Web service实践浅谈原理和用途
    [转]Oracle EXTRACT()函数与to_char() 函数
    [转]Webservice,基于Axis的最佳实践
    IIS出现The specified module could not be found解决方法
    转: ORA12560: TNS:protocol adapter error(TNS:协议适配器错误)
    PLSQL_案例优化系列_分析体系结构如何左右SQL性能(案例2)
    WebADI_数据验证3_建立基于Table的LOV验证(案例)
    PLSQL_案例优化系列_体会索引让SQL举步维艰的一面(案例6)
    PLSQL_案例优化系列_体会函数及位图索引与SQL优化(案例7)
  • 原文地址:https://www.cnblogs.com/hankeke/p/loj2542.html
Copyright © 2020-2023  润新知