[洛谷P2495][SDOI2011]消耗战
题目大意:有一棵$n(n\leqslant2.5\times10^5)$个节点的带边权的树,$m$个询问,每次询问给出$k(\sum\limits_{i=1}^mk_i\leqslant5\times10^5)$个点,要求用最小的代价砍断一些边,使得$1$号点与这$k$个点都不连通,输出最小代价
题解:先考虑一个一次$O(n)$的$DP$,可以把所有子树内没有特殊点的点先删去,令$f(i)$表示把他于他子树内所有的特殊点割断所需要的代价,即为$\sum\limits_{v\in son_u}\min\{w,f_v\}$($w$为这条边边权,若$v$为关键点,$f_v=\inf$)。这样的复杂度是$O(n^2)$的,不可以通过。
可以建虚树,一般建一棵虚树的复杂度是$O(k\log_2n)$的,它可以使得建出来的树中只包含最多$2k$个节点。于是就可以通过本题
卡点:最多$2k$个点,$k\leqslant n$,没有开两倍的空间
C++ Code:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
namespace std {
struct istream {
#define M (1 << 25 | 3)
char buf[M], *ch = buf - 1;
inline istream() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
fread(buf, 1, M, stdin);
}
inline istream& operator >> (int &x) {
while (isspace(*++ch));
for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
return *this;
}
#undef M
} cin;
struct ostream {
#define M (1 << 25 | 3)
char buf[M], *ch = buf - 1;
inline ostream& operator << (int x) {
if (!x) {*++ch = '0'; return *this;}
static int S[11], *top; top = S;
while (x) {*++top = x % 10 ^ 48; x /= 10;}
for (; top != S; --top) *++ch = *top;
return *this;
}
inline ostream& operator << (long long x) {
if (!x) {*++ch = '0'; return *this;}
static int S[20], *top; top = S;
while (x) {*++top = x % 10 ^ 48; x /= 10;}
for (; top != S; --top) *++ch = *top;
return *this;
}
inline ostream& operator << (const char x) {*++ch = x; return *this;}
inline ~ostream() {
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
#endif
fwrite(buf, 1, ch - buf + 1, stdout);
}
#undef M
} cout;
} #define maxn 250010 int head[maxn], cnt;
struct Edge {
int to, nxt, w;
} e[maxn << 1];
inline void addedge(int a, int b, int c) {
e[++cnt] = (Edge) {b, head[a], c}; head[a] = cnt;
} int in[maxn], out[maxn], idx, dep[maxn];
namespace Tree {
#define M 17
int fa[maxn][M + 1], Min[maxn][M + 1];
void dfs(int u) {
in[u] = ++idx;
for (int i = 1; i <= M; i++) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
Min[u][i] = std::min(Min[u][i - 1], Min[fa[u][i - 1]][i - 1]);
}
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != *fa[u]) {
*fa[v] = u;
*Min[v] = e[i].w;
dep[v] = dep[u] + 1;
dfs(v);
}
}
out[u] = idx;
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) std::swap(x, y);
for (int i = dep[x] - dep[y]; i; i &= i - 1) x = fa[x][__builtin_ctz(i)];
if (x == y) return x;
for (int i = M; ~i; i--) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return *fa[x];
}
const int inf = 0x3f3f3f3f;
inline int query(int x, int k) {
int res = inf;
for (int i = k; i; i &= i - 1) {
res = std::min(res, Min[x][__builtin_ctz(i)]);
x = fa[x][__builtin_ctz(i)];
}
return res;
}
#undef M
} int n, m;
namespace Work {
const long long inf = 0x3f3f3f3f3f3f3f3f;
inline bool cmp(int a, int b) {return in[a] < in[b];} int k;
int list[maxn << 1], S[maxn], top;
long long f[maxn];
bool imp[maxn]; void dfs(int u) {
f[u] = 0;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
dfs(v);
f[u] += std::min(f[v], static_cast<long long> (e[i].w));
}
if (imp[u]) f[u] = inf, imp[u] = false;
head[u] = 0;
} void solve() {
cnt = 0;
std::cin >> k;
for (int i = 0; i < k; i++) {
std::cin >> list[i];
imp[list[i]] = true;
} std::sort(list, list + k, cmp);
int tot = k;
for (int i = 0; i < k - 1; i++) list[tot++] = Tree::LCA(list[i], list[i + 1]);
list[tot++] = 1;
tot = (std::sort(list, list + tot, cmp), std::unique(list, list + tot) - list);
top = 0;
for (int I = 0, i = *list; I < tot; I++, i = list[I]) {
while (top && out[S[top]] < in[i]) top--;
if (top) addedge(S[top], i, Tree::query(i, dep[i] - dep[S[top]]));
S[++top] = i;
} dfs(1);
std::cout << f[1] << '\n';
}
} int main() {
std::cin >> n;
for (int i = 1, u, v, w; i < n; i++) {
std::cin >> u >> v >> w;
addedge(u, v, w);
addedge(v, u, w);
}
Tree::dfs(1);
std::cin >> m;
memset(head, 0, sizeof head);
while (m --> 0) Work::solve();
return 0;
}
[洛谷P2495][SDOI2011]消耗战的更多相关文章
- 洛谷P2495 [SDOI2011]消耗战(虚树dp)
P2495 [SDOI2011]消耗战 题目链接 题解: 虚树\(dp\)入门题吧.虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是 ...
- 洛谷 P2495 [SDOI2011]消耗战(虚树,dp)
题面 洛谷 题解 虚树+dp 关于虚树 了解一下 具体实现 inline void insert(int x) { if (top == 1) {s[++top] = x; return ;} int ...
- ●洛谷P2495 [SDOI2011]消耗战
题链: https://www.luogu.org/problemnew/show/P2495题解: 虚树入门,树形dp 推荐博客:http://blog.csdn.net/lych_cys/arti ...
- 洛谷P2495 [SDOI2011]消耗战(虚树)
题面 传送门 题解 为啥一直莫名其妙\(90\)分啊--重构了一下代码才\(A\)掉-- 先考虑直接\(dp\)怎么做 树形\(dp\)的时候,记一下断开某个节点的最小值,就是从根节点到它的路径上最短 ...
- bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286 https://www.luogu.org/problemnew/show/P2495 ...
- [洛谷P2459] SDOI2011 消耗战
问题描述 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知 ...
- [bzoj2286] [洛谷P2495] [sdoi2015] 消耗战
Description 在一场战争中,战场由 \(n\) 个岛屿和 \(n-1\) 个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足 ...
- AC日记——[SDOI2011]消耗战 洛谷 P2495
[SDOI2011]消耗战 思路: 建虚树走树形dp: 代码: #include <bits/stdc++.h> using namespace std; #define INF 1e17 ...
- [洛谷P2491] [SDOI2011]消防
洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...
随机推荐
- Java: 数组、列表和集合的互相转换
1. Array 转 List String[] city = {"Nanjing","Shanghai","Beijing"}; List ...
- dvs-panotracking编译运行
编译运行dvs-panotracking > 编译dvs-panotracking之前首先需要安装imageutilities . 源码下载 https://github.com/VLOGrou ...
- IO 转换流
package TestIo; import java.io.*; /** * 转换流 */ public class TestConvertStream { public static void m ...
- android分析windowManager、window、viewGroup之间关系(一)
本文将主要介绍addview方法,在windowManager.window.viewGroup中的实现原理.首先将介绍这些类结构关系,然后分析其内在联系,介绍实现原理,最后介绍重要的一个参数wind ...
- 基于Spring的最简单的定时任务实现与配置(二)
接上一篇,原本我以为我实现的方式很简单了,在准备写(一)的时候,就去查了查别人是怎么实现定时任务的.不查还好,这一查,发现还有更简单的.所以就会有这篇文章. 本文主要是讨论,在完成Spring 项目搭 ...
- 在Linux CentOS7系统中搭建LNMP
LNMP就是Linux+Nginx+MySQL+PHP,既然是在Linux CentOS7那么Linux就是已经安装好了.所以接下百度一下接下来的教程,整理测试如下: 教程是centos6.2的有点老 ...
- WEB页面常用基本控件测试用例
一.树控件的测试外观操作 1)项目中的所有树是否风格一致 2)树结构的默认状态是怎样的.比如默认树是否是展开,是展开几级? 是否有默认的焦点? 默认值是什么?展开的节点图标和颜色? 2.执行操作 1 ...
- python操作字符串内容并重新输出
今天在做一个函数的作业,题目如下: 编写一个函数实现大写转小写,小写变大写,并且转换为镜像字符串,并且将字符串变为镜像字符串. 例如:’A’变为’Z’,’b’变为’y 示范字符串: ”sdSdsfdA ...
- org.apache.spark.launcher.Main源码分析
public static void main(String[] argsArray) throws Exception { //org.apache.spark.launcher.Main chec ...
- 如何处理 jQuery $(window).resize() 中的方法被多次执行的小问题
引言: 估计很多同志们在编写浏览器resize()的方法时,都会遇到这样的情况: 当拖动浏览器的边角时,页面中的一些效果随浏览器大小的改变而触发,这一过程开始到结束,resize() 中的方法被执行了 ...