https://www.luogu.org/problemnew/show/P2495

Dp 方程很显然

设 Dp[u] 表示——使 u 不与其子树中任意一个关键点联通的最小代价

设 w[a, b] 表示 a 与 b 之间的边的权值。

  • 若 son[i] 不是关键点,Dp[u] = Dp[u] + min{Dp[son[i]],w[u][son[i]]}
  • 若 son[i] 是关键点,Dp[u] = Dp[u] + w[u][son[i]]

但这样复杂度很显然是不对的,所以我们考虑虚树

什么,你还不会虚树?那就去跟 zzq 学吧 https://www.cnblogs.com/zzqsblog/p/5560645.html

我们发现 k 的总和与 n 同级,所以用虚树优化这个 Dp,建出虚树,在虚树上 Dp 即可

#include <bits/stdc++.h>
#define X first
#define Y second
#define mp make_pair
using namespace std; typedef long long ll;
const int N = 250000 + 5, LG2 = 18; vector < pair <int, int> > G[N], G2[N];
int pre[N][LG2 + 1], dep[N], mx[N][LG2 + 1], id[N], dfn;
int n, m, k, h[N], sta[N], len, MX;
ll f[N];
bool book[N]; void init(int u, int fa) {
pre[u][0] = fa; dep[u] = dep[fa] + 1; id[u] = ++dfn;
for(int i = 1; i <= LG2; i++) {
pre[u][i] = pre[pre[u][i - 1]][i - 1];
mx[u][i] = min(mx[u][i - 1], mx[pre[u][i - 1]][i - 1]);
}
for(vector < pair <int, int> > :: iterator it = G[u].begin(); it != G[u].end(); it++) {
int v = it -> X; if(v != fa) mx[v][0] = it -> Y, init(v, u);
}
} int LCA(int x, int y) {
MX = INT_MAX;
if(dep[x] > dep[y]) swap(x, y);
for(int i = LG2; i >= 0; i--)
if(dep[pre[y][i]] >= dep[x])
MX = min(MX, mx[y][i]), y = pre[y][i];
if(x == y) return x;
for(int i = LG2; i >= 0; i--)
if(pre[x][i] != pre[y][i]) {
MX = min(MX, min(mx[x][i], mx[y][i]));
x = pre[x][i], y = pre[y][i];
}
return pre[x][0];
} bool cmp(int x, int y) {return id[x] < id[y];} void DP(int u) {
f[u] = 0;
for(vector < pair <int, int> > :: iterator it = G2[u].begin(); it != G2[u].end(); it++) {
int v = it -> X; DP(v);
if(book[v]) f[u] += it -> Y;
else f[u] += min(f[v], (ll)it -> Y);
}
} int main() {
cin >> n;
for(int i = 1; i < n; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
G[a].push_back( mp(b, c) );
G[b].push_back( mp(a, c) );
}
init(1, 0); cin >> m;
while(m--) {
scanf("%d", &k);
for(int i = 1; i <= k; i++) {
scanf("%d", &h[i]);
book[h[i]] = 1;
}
sort(h + 1, h + k + 1, cmp);
sta[len = 1] = 1; G2[1].clear();
for(int i = 1; i <= k; i++) {
if(h[i] == 1) continue;
int lca = LCA(h[i], sta[len]);
if(lca != sta[len]) {
while(id[lca] < id[sta[len - 1]]) {
LCA(sta[len - 1], sta[len]);
G2[sta[len - 1]].push_back( mp(sta[len], MX) );
len--;
}
if(id[lca] > id[sta[len - 1]]) {
G2[lca].clear();
LCA(lca, sta[len]);
G2[lca].push_back( mp(sta[len], MX) );
sta[len] = lca;
} else LCA(lca, sta[len]), G2[lca].push_back( mp(sta[len], MX) ), len--;
}
G2[h[i]].clear(); sta[++len] = h[i];
}
for(int i = 1; i < len; i++) LCA(sta[i], sta[i + 1]), G2[sta[i]].push_back( mp(sta[i + 1], MX) );
DP(1); printf("%lld\n", f[1]);
for(int i = 1; i <= k; i++) book[h[i]] = 0;
}
return 0;
}

luoguP2495 [SDOI2011]消耗战的更多相关文章

  1. [luoguP2495] [SDOI2011]消耗战(DP + 虚树)

    传送门 明显虚树. 别的题解里都是这样说的. 先不考虑虚树,假设只有一组询问,该如何dp? f[u]表示把子树u中所有的有资源的节点都切掉的最优解 如果节点u需要切掉了话,$f[u]=val[u]$ ...

  2. BZOJ 2286: [Sdoi2011]消耗战

    2286: [Sdoi2011消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2082  Solved: 736[Submit][Status] ...

  3. bzoj 2286: [Sdoi2011]消耗战 虚树+树dp

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...

  4. bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战

    http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...

  5. 【LG2495】[SDOI2011]消耗战

    [LG2495][SDOI2011]消耗战 题面 洛谷 题解 参考博客 题意 给你\(n\)个点的一棵树 \(m\)个询问,每个询问给出\(k\)个点 求将这\(k\)个点与\(1\)号点断掉的最小代 ...

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

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

  7. [BZOJ2286][SDOI2011]消耗战(虚树DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4998  Solved: 1867[Submit][Statu ...

  8. BZOJ2286 [Sdoi2011]消耗战 【虚树 + 树形Dp】

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 4261  Solved: 1552 [Submit][Sta ...

  9. 【BZOJ2286】[Sdoi2011]消耗战 虚树

    [BZOJ2286][Sdoi2011]消耗战 Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的 ...

随机推荐

  1. c#6.0特性

    (1)可以直接对自动属性初始化,而且允许非标准属性使用 例如: public string Gender{get;}="男";//非标准属性 public DateTime Bir ...

  2. javascript中的类方法、构造方法、原型方法的对比

    如果你已经接触js很久了,那么应该可以看看我总结的是否正确,如果你刚开始学习,那么通过我的总结,你可以更快的区别他们,记得我刚接触js时,这一块反正是模糊了很久! 1,长相的区别: function ...

  3. maven项目引入外部jar包的三种方式

    方式1:dependency 本地jar包 <dependency> <groupId>com.hope.cloud</groupId> <!--自定义--& ...

  4. LeetCode题解 #155 Min Stack

    写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...

  5. git用法小结(1)--建立远程仓库

    最近一直在学习使用git来管理自己的程序,总是今天东学一点,明天西凑一点,到用的时候,总是有些茫然不知所措. 在博客园里看见一篇老好的文章,教我们做笔记啦,但是做完笔记还是要记得总结哦! 来吧,让我们 ...

  6. linux多线程默认栈大小和最大线程数

    linux的线程栈大小可以使用ulimit -s查看,对于ubunt 2.6的内核线程栈的默认大小为8M,如下: shine@shine-bupt:~/Program/C$ ulimit -s 819 ...

  7. Maven 国内源

    maven的仓库好慢的说,还是配置一个国内的源吧.推荐aliyun 在maven/conf/settings.xml 文件里配置mirrors的子节点,添加如下mirror <mirror> ...

  8. Windows下Git中正确显示中文的设置方法

    Windows下Git中正确显示中文的设置方法 具体设置方法如下: 进入目录etc:$ cd /etc 1. 编辑 gitconfig 文件:$ vi gitconfig.在其中增加如下内容: [gu ...

  9. phpmailer邮件类

    <?php/** * 邮件类 * Enter description here ... * @author df * Mail::getMail()->sendMail(); * */cl ...

  10. IP定位,天气接口

    首先获取IP ////获得本地真实IP function get_onlineip() { $ip_json = @file_get_contents("http://ip.taobao.c ...