题意

链接

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 << '\n';}
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 << '\n';
}
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;
}

洛谷P4103 [HEOI2014]大工程(虚树 树形dp)的更多相关文章

  1. luogu P4103 [HEOI2014]大工程 虚树 + 树形 DP

    Description 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道.  我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上.  在 2 个国家 a,b 之间建一条新通 ...

  2. bzoj 3611(洛谷 4103) [Heoi2014]大工程——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3611 https://www.luogu.org/problemnew/show/P4103 ...

  3. BZOJ.3611.[HEOI2014]大工程(虚树 树形DP)

    题目链接 要求的和.最大值.最小值好像都可以通过O(n)的树形DP做,总询问点数<=2n. 于是建虚树就可以了.具体DP见DP()函数,维护三个值sum[],mx[],mn[]. sum[]要开 ...

  4. 洛谷P4426 毒瘤 [HNOI/AHOI2018] 虚树+树上dp

    正解:虚树+树上dp 解题报告: 传送门! 首先解释一下题意趴,,,语文70pts选手已经开始看不懂题辣QAQ 大概就是个给一个图,求独立集方案,且保证图是联通的,边的数量最多只比点多10 首先思考如 ...

  5. 洛谷4103 HEOI2014大工程(虚树+dp)

    又是一道虚树好题啊 我们建出来虚树,然后考虑dp过程,我们分别令\(sum[x],mndis[x],mxdis[x],size[x]\)为子树内的路径长度和,最短链,最长链,子树内关键点个数. 对于一 ...

  6. bzoj 3611: [Heoi2014]大工程 虚树

    题目: 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 ...

  7. BZOJ 3611 [Heoi2014]大工程 ——虚树

    虚树第二题.... 同BZOJ2286 #include <map> #include <cmath> #include <queue> #include < ...

  8. 洛谷 P2495 [SDOI2011]消耗战(虚树,dp)

    题面 洛谷 题解 虚树+dp 关于虚树 了解一下 具体实现 inline void insert(int x) { if (top == 1) {s[++top] = x; return ;} int ...

  9. bzoj 3611[Heoi2014]大工程 虚树+dp

    题意: 给一棵树 每次选 k 个关键点,然后在它们两两之间 新建 C(k,2)条 新通道. 求: 1.这些新通道的代价和 2.这些新通道中代价最小的是多少 3.这些新通道中代价最大的是多少 分析:较常 ...

随机推荐

  1. 10分钟看懂Docker和K8S

    本文来源:鲜枣课堂 2010年,几个搞IT的年轻人,在美国旧金山成立了一家名叫"dotCloud"的公司. 这家公司主要提供基于PaaS的云计算技术服务.具体来说,是和LXC有关的 ...

  2. CentOS7编译安装MySQL5.7.24

    目录 安装依赖 安装boost 编译安装MySQL 配置 登录MySQL,修改密码 安装依赖 (1)cmake是新版MySQL的编译工具 sudo yum install gcc gcc-c++ pc ...

  3. 如何写gdb命令脚本

    作为UNIX/Linux下使用广泛的调试器,gdb不仅提供了丰富的命令,还引入了对脚本的支持:一种是对已存在的脚本语言支持,比如python,用户可以直接书写python脚本,由gdb调用python ...

  4. 安卓TabLayout+ViewPager实现切页

    安卓使用TabLayout+ViewPager+Fragment 实现页面切换,可实现左右滑动切换视图界面和点击切换 可自定义菜单栏是在顶部还是在底部 一.实现效果: 二.实现过程: 2.1 一些重要 ...

  5. jsbridge的js封装

    /*注意:源生app需要配置jsbridge的环境,而在前端页面中需要下方封装代码,既可以达到调用app方法的功能和注册供app调用的方法1.注册方法:注册后,供app调用,注册时,同名函数,下一个会 ...

  6. Java核心技术及面试指南 数据库方面的面试题归纳以及总结

    5.1.7.1 事务的四大特性是什么? ⑴ 原子性(Atomicity) 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚. ⑵ 一致性(Consistency) 一致性是指事务必须使数据库 ...

  7. MQ(2)---JMS

    JMS 一.理解JMS   1.什么是JMS?         JMS即Java消息服务(Java Message Service)应用程序接口,API是一个消息服务的标准或者说是规范,允许应用程序组 ...

  8. redis 系列2 知识点概述

    一.概述 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表 ...

  9. CentOS7 nexus 3 搭建maven或gradle 私有代理服务器

    1.下载nexus 3, 选择与操作系统对应版本 2.解压nexus并运行: 3.在浏览器中输入http://192.168.127.128:8081/,并以amdin为用户名,以admin123为密 ...

  10. Java——static关键字

    前言 static关键字算是Java中比较复杂的关键字之一,它可以修饰变量.方法.类以及代码块.下面将介绍static的具体使用. static引入的目的 static的作用 static修饰变量 s ...