luoguP2495 [SDOI2011]消耗战
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]消耗战的更多相关文章
- [luoguP2495] [SDOI2011]消耗战(DP + 虚树)
传送门 明显虚树. 别的题解里都是这样说的. 先不考虑虚树,假设只有一组询问,该如何dp? f[u]表示把子树u中所有的有资源的节点都切掉的最优解 如果节点u需要切掉了话,$f[u]=val[u]$ ...
- BZOJ 2286: [Sdoi2011]消耗战
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2082 Solved: 736[Submit][Status] ...
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战
http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...
- 【LG2495】[SDOI2011]消耗战
[LG2495][SDOI2011]消耗战 题面 洛谷 题解 参考博客 题意 给你\(n\)个点的一棵树 \(m\)个询问,每个询问给出\(k\)个点 求将这\(k\)个点与\(1\)号点断掉的最小代 ...
- AC日记——[SDOI2011]消耗战 洛谷 P2495
[SDOI2011]消耗战 思路: 建虚树走树形dp: 代码: #include <bits/stdc++.h> using namespace std; #define INF 1e17 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- BZOJ2286 [Sdoi2011]消耗战 【虚树 + 树形Dp】
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 4261 Solved: 1552 [Submit][Sta ...
- 【BZOJ2286】[Sdoi2011]消耗战 虚树
[BZOJ2286][Sdoi2011]消耗战 Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的 ...
随机推荐
- WARNING: cell0 mapping not found - not syncing cell0
WARNING: cell0 mapping not found - not syncing cell0
- 泛型集合List<T>
- angularjs 简易模态框
angularjs 简易模态框 angularjs 中的模态框一般使用插件angular-ui-bootstrap书写. 这里记录一种简易的模态框写法: 1.警告消息框alert: 原理: 在html ...
- 【290】Python 常用说明
1. 双击直接运行 python 代码暂停显示的方法:python学习笔记(3)--IDLE双击运行后暂停 需要添加如下代码: import os os.system("pause" ...
- jdk环境变量一键设置 管理員运行
退出360等杀毒软件 本机win10 其他环境自测.参考了网上代码修改. @echo off rem dss color 02 mode con cols=70 lines=30 title JDK ...
- T-SQL 理解SQL SERVER中的分区表(转)
转载来源一定要明显: http://www.cnblogs.com/CareySon/archive/2011/12/30/2307766.html 而且这个大神对于数据库方面的文章非常棒 强烈推荐 ...
- codeforce469DIV2——D. A Leapfrog in the Array
题意: 给出1<=n<=10^18和1<=q<=200000,有一个长度为2*n-1的数组,初始时单数位置存(i+1)/2,双数位置是空的.每次找出最右边的一个数将它跳到离它最 ...
- 搭建大数据hadoop完全分布式环境遇到的坑
搭建大数据hadoop完全分布式环境,遇到很多问题,这里记录一部分,以备以后查看. 1.在安装配置完hadoop以后,需要格式化namenode,输入指令:hadoop namenode -forma ...
- Docker学习之路(三)Docker网络详解
1. Docker的4种网络模式 我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: host模式,使用--net=host ...
- ROS naviagtion analysis: costmap_2d--ObstacleLayer
博客转载自:https://blog.csdn.net/u013158492/article/details/50493676 构造函数 ObstacleLayer() { costmap_ = NU ...