BZOJ 2286: [Sdoi2011]消耗战 虚树
Description
Input
第一行一个整数n,代表岛屿数量。
接下来n-1行,每行三个整数u,v,w,代表u号岛屿和v号岛屿由一条代价为c的桥梁直接相连,保证1<=u,v<=n且1<=c<=100000。
第n+1行,一个整数m,代表敌方机器能使用的次数。
接下来m行,每行一个整数ki,代表第i次后,有ki个岛屿资源丰富,接下来k个整数h1,h2,…hk,表示资源丰富岛屿的编号。
Output
输出有m行,分别代表每次任务的最小代价。
题解:
朴素做法是 $O(mn)$ 的树形DP,效率太低
虽然询问次数非常多,然而 $\sum k$ 却只有 $5\times 10^5$
我们引入虚树
虚树就是每次只保留有用的节点,即关键节点与它们之间的 $LCA$
先对所有关键点按照在原树中 $dfs$ 序前后排一下序
开一个栈 $S$ 来存储一条深度依次递增的链(注意,$S$ 存的是链)
考虑每次新扩展一个关键节点 $x$
(1) 栈中元素小于等于 $1$ 个,直接加入即可
(2) 令 $lca$ 表示 $LCA(S_{top},x)$
若 $lca=S_{top}$, 那么 $x$ 与 $S_{top}$ 以及栈中的链还是会构成一条链,没有分叉,直接将 $x$ 加入栈中即可
若 $lca\neq S_{top}$ ,那么说明 $S_{top}$ 及其子树已全部扩展完毕,我们需要一步一步退栈
while(top > 1 && dep[S[top - 1]] >= dep[lca]) add_edge(S[top - 1], S[top]), --top;
if(S[top] != lca) add_edge(lca, S[top]), S[top] = lca;
S[++top] = x;
条件是 $S_{top-1}$ 的深度要大于等于 $lca$ 的深度
由于是大于等于,所以如果 $lca$ 在栈中的话最后 $S_{top}$ 一定会等于 $lca$,那么就无需加入 $lca$
如果 $lca$ 不等于 $S_{top}$ 的话,那么一定是 $S_{top}$ 与 $S_{top-1}$ 之间夹着 $lca$ ,直接由 $lca$ 向 $S_{top}$ 连一条边,并将栈顶改为 $lca$ 即可
#include <bits/stdc++.h>
#define setIO(s) freopen(s".in", "r", stdin)
#define maxn 500004
#define LOG 23
#define inf 100000000000
#define ll long long
using namespace std;
vector <int> G[maxn];
int edges, tim, n, top;
int hd[maxn], to[maxn << 1], nex[maxn << 1], val[maxn << 1];
int dfn[maxn], f[LOG][maxn], arr[maxn], S[maxn], mk[maxn], dep[maxn];
ll mn[maxn];
inline void addedge(int u, int v, int c)
{
nex[++edges] = hd[u], hd[u] = edges, to[edges] = v, val[edges] = c;
}
void dfs1(int u, int ff)
{
f[0][u] = ff;
for(int i = 1; i < 22; ++i) f[i][u] = f[i - 1][f[i - 1][u]];
dep[u] = dep[ff] + 1, dfn[u] = ++tim;
for(int i = hd[u]; i ; i = nex[i])
{
int v = to[i];
if(v == ff) continue;
mn[v] = min(mn[u], 1ll*val[i]);
dfs1(v, u);
}
}
inline int LCA(int a, int b)
{
if(dep[a] > dep[b]) swap(a, b);
if(dep[a] != dep[b])
{
for(int i = 21; i >= 0; --i) if(dep[f[i][b]] >= dep[a]) b = f[i][b];
}
if(a == b) return a;
for(int i = 21; i >= 0; --i) if(f[i][a] != f[i][b]) a = f[i][a], b = f[i][b];
return f[0][a];
}
bool cmp(int a, int b)
{
return dfn[a] < dfn[b];
}
inline void add_edge(int u, int v)
{
G[u].push_back(v);
}
inline void insert(int x)
{
if(top <= 1)
{
S[++top] = x;
return;
}
int lca = LCA(x, S[top]);
if(lca == S[top]) return;
while(top > 1 && dep[S[top - 1]] >= dep[lca]) add_edge(S[top - 1], S[top]), --top;
if(S[top] != lca) add_edge(lca, S[top]), S[top] = lca;
S[++top] = x;
}
ll DP(int x)
{
ll sum = 0, re;
for(int i = 0; i < G[x].size(); ++i) sum += DP(G[x][i]);
if(mk[x]) re = mn[x];
else re = min(mn[x], sum);
mk[x] = 0;
G[x].clear();
return re;
}
int main()
{
// setIO("input");
scanf("%d",&n);
for(int i = 1; i < n ; ++i)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c), addedge(a, b, c), addedge(b, a, c);
}
dep[1] = 1, mn[1] = inf, dfs1(1, 0);
int Q;
scanf("%d",&Q);
while(Q--)
{
int k;
scanf("%d",&k);
for(int i = 1; i <= k ; ++i) scanf("%d",&arr[i]);
sort(arr + 1, arr + 1 + k, cmp);
S[++top] = 1;
for(int i = 1; i <= k ; ++i) insert(arr[i]), mk[arr[i]] = 1;
while(top > 0) add_edge(S[top - 1], S[top]) , --top;
printf("%lld\n",DP(1));
for(int i = 1; i <= k ; ++i) mk[arr[i]] = 0;
}
return 0;
}
BZOJ 2286: [Sdoi2011]消耗战 虚树的更多相关文章
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- BZOJ 2286 [Sdoi2011]消耗战 ——虚树
虚树第一题. 大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP #include <map> #include <ctime> #include <cmath&g ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
- BZOJ 2286: [Sdoi2011]消耗战
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2082 Solved: 736[Submit][Status] ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
随机推荐
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_5_迭代器的代码实现
迭代器的类型和collection一样.都是String类型的 判断集合内是不是有元素 取出第一个元素 多次next获取所有的值 没有元素,再去取就会抛出异常. 适应while for循环的格式了解一 ...
- maven(一) maven到底是什么
为了方便自己查找,这里转载他人文章,原文出处http://www.cnblogs.com/whgk/p/7112560.html 我记得在搞懂maven之前看了几次重复的maven的教学视频.不知道是 ...
- 每日js练习
第一次玩codewars,选了个最简单的题目 要求是: You have to write a function that describe Leo: if oscar was (integer) 8 ...
- C#之委托(二)
其实在上一篇委托(一)中,创建委托还是太繁琐了点.代码量过多,可能会妨碍我们对代码和逻辑的理解.有些时候可能处理逻辑的代码都笔声明委托的代码要少,这就不可避免的增加了重复代码的量.所以在c#2中极大的 ...
- 【ABAP系列】SAP ABAP 仓库库存-物料拆分的算法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 仓库库存-物料 ...
- java对象的方法属性和代码块的加载顺序
1.静态变量 2.静态代码块 3.局部代码块 4.构造函数 5.普通代码块 6.静态方法 7.普通方法 8.普通属性 for example: package com.JavaTest2; publi ...
- SVN检出新项目
1.新建文件夹SourseCode -->打开SourseCode文件夹,右键空白处 ---> 选择SVN Checkout --选择URL of repository,选择Checkou ...
- python+selenium文本框对象以及按钮对象操作
文本框对象 from selenium import webdriverfrom time import sleep driver = webdriver.Firefox() # 指定和打开浏览器ur ...
- 将查询列表内容保存到excel表格中,并保存到相应的盘中
1.先导入相应的jar包 2.一个小的Demo测试[实体类+测试类:保存excel的方法] Student实体类 public class Student{ private int id; priva ...
- HardFault_Handler
STM32程序一运行就进入HardFault_Handler,原因很可能是堆栈溢出 ]={}; // 我把程序中上面这句注释了就没事了 :进入HardFault_Handler也可能是数组越界引起的: ...