题目大意:有一棵$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. LimitedConcurrencyLevelTaskScheduler

    //-------------------------------------------------------------------------- // // Copyright (c) Mic ...

  2. 【完美解决】Spark-SQL、Hive多 Metastore、多后端、多库

    [完美解决]Spark-SQL.Hive多 Metastore.多后端.多库 [完美解决]Spark-SQL.Hive多 Metastore.多后端.多库 SparkSQL 支持同时连接多种 Meta ...

  3. 用ext_skel,实现一个PHP扩展,添加到PHP并调用

    1 创建函数定义文件 #mkdir /home/phpext #vi mydefined.skel string get_text(string str) 2 根据README所提供的信息创建预定义文 ...

  4. 解决Entity Framework查询匿名对象后的跨域访问的一种方式

    在Entity Framework中,可以使用lambda表达式进行对数据的查询,而且可以将查询结果直接映射为对象或者对象列表,这极大的提高的开发速度,并且使数据层的数据更加方便处理和传递.但是很多时 ...

  5. VS中添加新项 数据选项卡下没有ADO.NET实体数据模型解决方案

    第一种:C:\ProgramData下面搜索EFTools找到你vs对应版本的EFTools.msi 先remove 然后再Install 重启电脑再看 第二种:如果意外地删除了 Visual Stu ...

  6. Ajax中post请求和get请求的区别

    首先提出两点Post比Get大的不同地方 1.post请求浏览器每次不会缓存,每次都会重新请求,而get请求不要缓存的时候,需要手动设置 写上xhr.setRequestHeader("If ...

  7. 2019年猪年海报PSD模板-第七部分

    14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1pE3X9AYirog1W8FSxbMiAQ              

  8. JDK11安装后,环境变量的坑

    安装了最新的JDK11,安装完后设置环境变量,打开CMD,没生效 检查了3遍,都没发现问题,在PATH中将JAVA设置移到第一也不行 最后偶然发现,在点击如图右下的‘编辑文本’,用文本方式编辑时,发现 ...

  9. Java学习 · 初识 面向对象基础二

    Package 为什么需要使用package a)   解决类重名的问题 b)   便于管理类 怎么使用package a)   类的第一句非注释性语句 b)   命名:域名倒着写,再加上模块名 注意 ...

  10. mybatis 枚举类型使用

    一.首先定义接口,提供获取数据库存取的值得方法,如下: public interface BaseEnum { int getCode(); } 二.定义mybatis的typeHandler扩展类, ...