• 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)


    题意

    链接

    Sol

    虚树。

    首先建出虚树,然后直接树形dp就行了。

    最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案。

    任意两点的路径和可以考虑每条边两边的贡献,(d[x])表示到该节点的所有节点的路径和,转移的时候考虑一下两棵子树的siz就行(画一下图就很清楚了)

    每次询问可以用个队列记录一下被访问过的元素,清空的时候只清空队列里的点。

    #include<bits/stdc++.h> 
    #define Pair pair<int, int>
    #define MP(x, y) make_pair(x, y)
    #define fi first
    #define se second
    #define int long long 
    #define LL long long 
    #define Fin(x) {freopen(#x".in","r",stdin);}
    #define Fout(x) {freopen(#x".out","w",stdout);}
    using namespace std;
    const int MAXN = 2e6 + 10, mod = 1e9 + 7, INF = 1e9 + 10;
    const double eps = 1e-9;
    template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
    template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
    template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
    template <typename A> inline void debug(A a){cout << a << '
    ';}
    template <typename A> inline LL sqr(A x){return 1ll * x * x;}
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, dfn[MAXN];
    vector<int> v[MAXN], E[MAXN];
    namespace HLP {
    	int fa[MAXN], siz[MAXN], son[MAXN], dep[MAXN], top[MAXN], times;
    	void dfs1(int x, int _fa) {
    		siz[x] = 1; fa[x] = _fa; dep[x] = dep[_fa] + 1; dfn[x] = ++times;
    		for(auto &to : v[x]) {
    			if(to == _fa) continue;
    			dfs1(to, x);
    			siz[x] += siz[to];
    			if(siz[to] > siz[son[x]]) son[x] = to;
    		}
    	}
    	void dfs2(int x, int topf) {
    		top[x] = topf; 
    		if(!son[x]) return ;
    		dfs2(son[x], topf);
    		for(auto &to : v[x]) {
    			if(top[to]) continue;
    			dfs2(to, to);
    		}
    	}
    	int LCA(int x, int y) {
    		while(top[x] ^ top[y]) {
    			if(dep[top[x]] < dep[top[y]]) swap(x, y);
    			x = fa[top[x]];
    		}
    		return dep[x] < dep[y] ? x : y;
    	}		
    }	
    int p[MAXN], st[MAXN], top, f[MAXN], mx[MAXN], mn[MAXN], siz2[MAXN], d[MAXN], ans1, ans2, ans3;
    void AE(int x, int y) {E[x].push_back(y);}
    int comp(int a, int b) {return dfn[a] < dfn[b];}
    int dis(int x, int y) {if(dfn[x] > dfn[y]) swap(x, y); return HLP::dep[y] - HLP::dep[x];}
    queue<int> q;
    void insert(int x) {
    	if(top == 1) {st[++top] = x; return ;}
    	int lca = HLP::LCA(x, st[top]);
    	if(lca == st[top]) {st[++top] = x; return ;}
    	while(top > 1 && dfn[st[top - 1]] >= dfn[lca]) AE(st[top - 1], st[top]), top--;
    	if(lca != st[top]) 
    		AE(lca, st[top]), st[top] = lca;
    	st[++top] = x;
    }
    //ans1 和 ans2最大值 ans3最小值
    void DP(int x) {
    	q.push(x);
    	mn[x] = (siz2[x] ? 0 : 1e18);
    	mx[x] = 0; d[x] = 0;
    	for(auto &to : E[x]) {
    		int w = dis(x, to);
    		assert(w <= N);
    		DP(to);
    		if(siz2[x] > 0) {
    			ans1 += w * siz2[to] * siz2[x] + siz2[to] * d[x] + siz2[x] * d[to];
    			chmax(ans2, mx[x] + mx[to] + w);
    			chmin(ans3, mn[x] + mn[to] + w);
    		}
    		chmax(mx[x], w + mx[to]);
    		chmin(mn[x], w + mn[to]);
    		siz2[x] += siz2[to]; 
    		d[x] += d[to] + w * siz2[to];
    	}
    	E[x].clear();
    }
    void work() {	
    	ans1 = ans2 = 0; ans3 = INF;
    	int K = read(); st[top = 1] = 1;
    	for(int i = 1; i <= K; i++) p[i] = read();
    	sort(p + 1, p + K + 1, comp);
    	for(int i = 1; i <= K; i++) {
    		if(p[i] != 1) insert(p[i]);
    		siz2[p[i]] = 1;
    	}
    	while(top > 1) AE(st[top - 1], st[top]), top--;
    	DP(1);
    	while(!q.empty()) siz2[q.front()] = 0, q.pop();
    	cout << ans1 << ' ' << ans3 << ' ' << ans2 << '
    ';
    }
    signed main() {
        N = read();
        for(int i = 1; i <= N - 1; i++) {
        	int x = read(), y = read();
        	v[x].push_back(y);
        	v[y].push_back(x);
    	}
        HLP::dfs1(1, 0); HLP::dfs2(1, 1);
        int Q = read(); 
        while(Q--) {
        	work();
    	}
    	return 0;
    }
    
  • 相关阅读:
    搜狗输入法用户体验
    Day06
    Day05
    Spark-RDD操作(26个常用函数附实例)
    软件工程培训第五天(hive进阶)
    hive窗口函数
    hive操作(行转列,列转行)
    Hive中使用case then分情况求和
    hive分组排序(rank函数+partiton实现)
    软件工程培训第四天总结,hive的学习
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/10535448.html
Copyright © 2020-2023  润新知