BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 5246 Solved: 1978
[Submit][Status][Discuss]
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行,分别代表每次任务的最小代价。
Sample Input
1 5 13
1 9 6
2 1 19
2 4 8
2 3 91
5 6 8
7 5 4
7 8 31
10 7 9
3
2 10 6
4 5 7 8 3
3 9 4 6
Sample Output
32
22
HINT
对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1
Source
感觉虚树还算比较好理解吧。
首先考虑最暴力的dp,
设$f[x]$表示处理完以$x$为根的子树的最小花费
转移有两种情况
1.断开自己与父亲的联系,代价为从根到该节点的最小值
2.将子树内的节点全都处理掉的代价
但这样时间复杂度是$O(nm)$的,显然过不了
虚树就是把有用的节点都拿出来。这里有用的节点指的是询问节点和他们的lca
然后每次DP的时候只在虚树上DP就可以了
这样时间复杂度为$2*sum_k$
更多关于虚树的姿势可以去这里https://www.cnblogs.com/SovietPower/p/9142068.html
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
#define LL long long
char buf[( << ) + ], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
char obuf[ << ], *O=obuf;
void print(LL x) {
if(x > ) print(x / );
*O++= x % + '';
}
int N, M;
struct Edge {
int u, v, w, nxt;
}E[MAXN << ];
int head[MAXN], num = ;
inline void AddEdge(int x, int y, int z) {
E[num] = (Edge) {x, y, z, head[x]};
head[x] = num++;
}
vector<int> v[MAXN];
void add_edge(int x, int y) {
v[x].push_back(y);
}
int a[MAXN], dfn[MAXN], topf[MAXN], siz[MAXN], son[MAXN], s[MAXN], top, deep[MAXN], fa[MAXN], ID = ;
LL mn[MAXN];
void dfs1(int x, int _fa) {
siz[x] = ; fa[x] = _fa;
for(int i = head[x]; i != -; i = E[i].nxt) {
if(E[i].v == _fa) continue;
deep[E[i].v] = deep[x] + ;
mn[E[i].v] = min(mn[x], (LL)E[i].w);
dfs1(E[i].v, x);
siz[x] += siz[E[i].v];
if(siz[E[i].v] > son[x]) son[x] = E[i].v;
}
}
void dfs2(int x, int topfa) {
topf[x] = topfa;
dfn[x] = ++ID;
if(!son[x]) return ;
dfs2(son[x], topfa);
for(int i = head[x]; i != -; i = E[i].nxt)
if(!topf[E[i].v])
dfs2(E[i].v, E[i].v);
}
int LCA(int x, int y) {
while(topf[x] != topf[y]) {
if(deep[topf[x]] < deep[topf[y]]) swap(x, y);
x = fa[topf[x]];
}
if(deep[x] < deep[y]) swap(x, y);
return y;
}
void insert(int x) {
if(top == ) {s[++top] = x; return ;}
int lca = LCA(x, s[top]);
if(lca == s[top]) return ;
while(top > && dfn[s[top - ]] >= dfn[lca]) add_edge(s[top - ], s[top]), top--;
if(lca != s[top]) add_edge(lca, s[top]), s[top] = lca;//
s[++top] = x;
}
LL DP(int x) {
if(v[x].size() == ) return mn[x];
LL sum = ;
for(int i = ; i < v[x].size(); i++)
sum += DP(v[x][i]);
v[x].clear();
return min(sum, (LL)mn[x]);
}
int comp(const int &a, const int &b) {
return dfn[a] < dfn[b];
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
memset(head, -, sizeof(head));
mn[] = 1ll << ;
N = read();
for(int i = ; i <= N - ; i++) {
int x = read(), y = read(), z = read();
AddEdge(x, y, z); AddEdge(y, x, z);
}
deep[] = ;
dfs1(, );
dfs2(, );
M = read();
while(M--) {
int K = read();
for(int i = ; i <= K; i++) a[i] = read();
sort(a + , a + K + , comp);
s[top = ] = ;
for(int i = ; i <= K; i++) insert(a[i]);
while(top > ) add_edge(s[top - ], s[top]), top--;
print(DP()), *O++ = '\n';
}
fwrite(obuf, O-obuf, , stdout);
return ;
}
BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)的更多相关文章
- 【BZOJ-2286】消耗战 虚树 + 树形DP
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2120 Solved: 752[Submit][Status] ...
- BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- 【BZOJ2286】【SDOI2011】消耗战 [虚树][树形DP]
消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一场战争中,战场由n个岛屿和n-1 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
- [SDOI2011]消耗战(虚树+树形动规)
虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...
- bzoj2286: [Sdoi2011]消耗战 虚树
在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知在其他k个 ...
- luogu P2495 [SDOI2011]消耗战 |虚树+LCA+dp
题目描述 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知 ...
随机推荐
- web前端开发需要具备的技能
web前端开发需要具备以下7种技能: 1.页面标记(HTML) HTML页面固定,标签不多,相对来说学起来比较容易.编写HTML代码需遵循HTML代码规范(http://www.cnblogs.com ...
- React Native之React速学教程(中)
概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解 ...
- eayui grid 每一页的行号都是从1开始
问题背景: easyui 需要显示行号的时候,我们只需要设置 rownumbers: true, 但是 不管是在哪一页,行号都是从1开始,不能连续 我们在分页的 onSelectPage 函数里去执 ...
- php基础部分(1)
PHP 输出文本的基础指令:echo 和 print.echo和print的区别echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用)echo 输出一 ...
- google离线小恐龙-备份
开启方法: 地址栏输入: chrome://dino 空格开始
- undo表空间不足,ORA-30036: unable to extend segment by 8 in undo tablespace 'UNDOTBS2'
故障现象:UNDO表空间越来越大,长此下去最终数据因为磁盘空间不足而崩溃: 问题分析:本问题在ORACLE系统管理中属于比较正常的一现象,产生问题的原因主要以下两点: 1. 有较大的事务量让Oracl ...
- vscode环境配置
"go.goroot": "/home/ken/go", "go.gopath": "/home/ken/gopath" ...
- Congestion Avoidance in TCP
Congestion Avoidance in TCP Consequence of lack of congestion control When a popular resource is sha ...
- cmd:相关命令和笔记
(1)查看git版本:git --version (2)
- easyui学习笔记7—在手风琴中显示表格
在这一篇中我们看看如何在手风琴里面显示表格数据的. 1.先看看引用的资源 <link rel="stylesheet" type="text/css" h ...