题目大意:有一棵$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]消耗战的更多相关文章

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

    P2495 [SDOI2011]消耗战 题目链接 题解: 虚树\(dp\)入门题吧.虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是 ...

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

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

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

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

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

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

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

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

  6. [洛谷P2459] SDOI2011 消耗战

    问题描述 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知 ...

  7. [bzoj2286] [洛谷P2495] [sdoi2015] 消耗战

    Description 在一场战争中,战场由 \(n\) 个岛屿和 \(n-1\) 个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足 ...

  8. AC日记——[SDOI2011]消耗战 洛谷 P2495

    [SDOI2011]消耗战 思路: 建虚树走树形dp: 代码: #include <bits/stdc++.h> using namespace std; #define INF 1e17 ...

  9. [洛谷P2491] [SDOI2011]消防

    洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...

随机推荐

  1. R语言使用过程中出现的问题--attach()函数的使用

    使用attach(file)时,一定要配合使用detach(file),否则再此运行程序时极易出现问题,The following objects are masked ... 此外工作空间中不能有与 ...

  2. swoole 相关

    安装虚拟机 VMware Workstation Pro 安装CentOS CentOS-7-x86_64-Minimal-1708.iso 安装FinalShell 教程地址 安装lnmp 教程地址 ...

  3. Python 列表下标操作

    Python  列表下标操作 引用网址: https://www.jianshu.com/p/a98e935e4d46

  4. Unity制作人物头像小图标和小地图

    人物头像的制作: 在场景中添加人物模型和环境模型 设置人物的layer为Player 在主摄像机的基础上,新建一个次摄像机并将摄像机镜头对准人物面部,调整至合适大小. 设置次摄像机 culling m ...

  5. gitignore 文件生效办法

    .gitignore 可以添加一些不加入git版本控制的文件 比如一些测试文件.因人而异的配置信息等等 .gitignore 文件展示如下 /.idea/target//.classpath /.pr ...

  6. LeetCode - 326, 342, 231 Power of Three, Four, and Two

    1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Po ...

  7. Train Problem(栈的应用)

    Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...

  8. JavaScript初探系列之String的基本操作

    1.字符串转换 字符串转换是最基础的要求和工作,你可以将任何类型的数据都转换为字符串,你可以用下面三种方法的任何一种: var myStr = num.toString(); // "19& ...

  9. 软工1816 · Alpha冲刺(1/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 前后端代码规范统一 针对之前的alpha冲刺安排进一步细化任务卡片 明确apl ...

  10. Jenkins系列-Jenkins修改主目录步骤说明

    在使用Jenkins做持续集成过程中,在构建很多次后发现有时在构建的时候系统提示磁盘空间不足,此时检查发现Jenkins的主目录挂载区放在了服务器根目录下,占用空间较大,此时除了对服务器的磁盘进行扩容 ...