P2495 [SDOI2011]消耗战

题目链接

题解:

虚树\(dp\)入门题吧。虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是一样的,所以可以就求出dfs序并且利用它来构造。最后的图中只有关键点以及某些关键点对的lca。

具体构造方法就是利用一个栈,假设当前插入结点为\(x\),求出栈顶元素和\(x\)的lca,如果栈顶元素为lca,那么我们就继续延长这条链;否则(此时栈顶元素和\(x\)在lca的两颗子树上面)就将栈顶元素到\(lca\)上面的点弹出来并且建边,然后继续延长\(x\)这边的链。

感觉说得不是很清楚,详见代码吧:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 250005 ;
int n, m;
struct Edge{
int v, next, w;
}e[N << 1];
int head[N], tot;
void adde(int u, int v, int w) {
e[tot].v = v; e[tot].w = w; e[tot].next = head[u]; head[u] = tot++;
}
vector <int> g[N] ;
int f[N][20], deep[N], dfn[N];
ll mn[N];
int T;
void dfs(int u, int fa) {
deep[u] = deep[fa] + 1;
dfn[u] = ++T;
for(int i = head[u]; i != -1; i = e[i].next) {
int v = e[i].v;
if(v == fa) continue ;
mn[v] = min((ll)e[i].w, mn[u]) ;
f[v][0] = u;
for(int j = 1; j <= 17; j++) f[v][j] = f[f[v][j - 1]][j - 1] ;
dfs(v, u) ;
}
}
int LCA(int x, int y) {
if(deep[x] < deep[y]) swap(x, y) ;
for(int i = 17; i >= 0; i--) {
if(deep[f[x][i]] >= deep[y]) x = f[x][i] ;
}
if(x == y) return x;
for(int i = 17; i >= 0; i--) {
if(f[x][i] != f[y][i]) x = f[x][i], y = f[y][i] ;
}
return f[x][0] ;
}
int sta[N], a[N];
int top;
bool cmp(const int &x, const int &y) {
return dfn[x] < dfn[y] ;
}
void add_edge(int u, int v) {
g[u].push_back(v) ;
}
void insert(int x) {
int lca = LCA(x, sta[top]) ;
if(top == 1) {sta[++top] = x; return ;}
if(lca == sta[top]) return ;
while(top > 1 && dfn[sta[top - 1]] >= dfn[lca]) {
add_edge(sta[top - 1], sta[top]); top--;
}
if(sta[top] != lca) add_edge(lca, sta[top]), sta[top] = lca;
sta[++top] = x;
}
ll DP(int u) {
if(g[u].size() == 0) return mn[u];
ll sum = 0;
for(auto v : g[u]) sum += DP(v) ;
g[u].clear() ;
return min(sum, (ll)mn[u]) ;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
mn[1] = 1ll << 56;
memset(head, -1, sizeof(head)) ;
for(int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
adde(u, v, w); adde(v, u, w) ;
}
dfs(1, 0) ;
cin >> m;
while(m--) {
int k; cin >> k;
for(int i = 1; i <= k; i++) cin >> a[i] ;
sort(a + 1, a + k + 1, cmp) ;
sta[top = 1] = 1;
for(int i = 1; i <= k; i++) insert(a[i]) ;
while(top > 1) add_edge(sta[top - 1], sta[top]), top--;
cout << DP(1) << '\n' ;
}
return 0 ;
}

洛谷P2495 [SDOI2011]消耗战(虚树dp)的更多相关文章

  1. bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树

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

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

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

  3. [BZOJ2286][SDOI2011]消耗战(虚树DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4998  Solved: 1867[Submit][Statu ...

  4. ●洛谷P2495 [SDOI2011]消耗战

    题链: https://www.luogu.org/problemnew/show/P2495题解: 虚树入门,树形dp 推荐博客:http://blog.csdn.net/lych_cys/arti ...

  5. bzoj 2286 [Sdoi2011]消耗战 虚树+dp

    题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...

  6. 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP

    [题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...

  7. 洛谷P2495 [SDOI2011]消耗战(虚树)

    题面 传送门 题解 为啥一直莫名其妙\(90\)分啊--重构了一下代码才\(A\)掉-- 先考虑直接\(dp\)怎么做 树形\(dp\)的时候,记一下断开某个节点的最小值,就是从根节点到它的路径上最短 ...

  8. [洛谷P2495][SDOI2011]消耗战

    题目大意:有一棵$n(n\leqslant2.5\times10^5)$个节点的带边权的树,$m$个询问,每次询问给出$k(\sum\limits_{i=1}^mk_i\leqslant5\times ...

  9. 洛谷 P3233 [HNOI2014]世界树(虚树+dp)

    题面 luogu 题解 数据范围已经告诉我们是虚树了,考虑如何在虚树上面\(dp\) 以下摘自hzwer博客: 构建虚树以后两遍dp处理出虚树上每个点最近的议事处 然后枚举虚树上每一条边,考虑其对两端 ...

随机推荐

  1. 2018-2019-2 网络对抗技术 20165318 Exp 9 Web安全基础

    2018-2019-2 网络对抗技术 20165318 Exp 9 Web安全基础 基础问题回答 实践过程记录 WebGoat安装 SQL注入攻击 1.命令注入(Command Injection) ...

  2. prometheus添加自定义监控与告警(etcd为例)

    一.步骤及注意事项(前提,部署参考部署篇) 一般etcd集群会开启HTTPS认证,因此访问etcd需要对应的证书 使用证书创建etcd的secret 将etcd的secret挂在到prometheus ...

  3. PostgreSQL中的Toast Pointer

    1.分析背景 在使用数据库的过程中(PG的版本为9.2),遇到了错误"missing chunk number 0 for toast value XX in pg_toast_2619&q ...

  4. Semaphore源码分析

    public class SemaphoreExample1 { ; public static void main(String[] args) throws Exception { Executo ...

  5. Linux常用基础(一)

    1.命令解释器 shell---Unix操作系统 bash---Linux操作系统 本质:根据输入的命令,调用相应的执行程序. 2.Linux下的快捷键 (1)命令和路径补全 Tab键 (2)主键盘的 ...

  6. linux ------ 在Vm 安装 centos系统

    -------------   简介 熟悉的操作系统*(android apple windows) 主要分类 1.应用领域(桌面.服务器.嵌入式) 2.源码开放程度(开源.闭源) 3.所支持的用户数 ...

  7. [转帖]【mount】Linux根目录空间不足

    [mount]Linux根目录空间不足 2019.04.15 21:30:47字数 1094阅读 107 一.问题背景 一台数据库服务器,突然监控告警,报根目录空间不足(no space left o ...

  8. [转帖]为什么HikariCP被号称为性能最好的Java数据库连接池,如何配置使用

    为什么HikariCP被号称为性能最好的Java数据库连接池,如何配置使用 原创Clement-Xu 发布于2015-07-17 15:53:14 阅读数 57066  收藏 展开 HiKariCP是 ...

  9. 【c++primer练习】 typedef与指针、常量和类型别名

    # c++primer 61页 typedef char* ptr ; cstr 是一个指向 char 的常量指针, 一种错误的理解是将语句等同于const char* ptr cstr; 但 ptr ...

  10. Scala 数组操作之Array、ArrayBuffer以及遍历数组

    ArrayBuffer 在Scala中,如果需要类似于Java中的ArrayList这种长度可变的集合类,则可以使用ArrayBuffer. // 如果不想每次都使用全限定名,则可以预先导入Array ...