【LG2495】[SDOI2011]消耗战
【LG2495】[SDOI2011]消耗战
题面
题解
题意
给你\(n\)个点的一棵树
\(m\)个询问,每个询问给出\(k\)个点
求将这\(k\)个点与\(1\)号点断掉的最小代价
其中\(n\leq250000\) \(m\geq1\) \(\Sigma k_i\leq500000\)
暴力
考虑直接暴力\(dp\)
设\(dp[i]\)表示处理完\(i\)的子树的最小代价
则\(dp[i]\)\(=\)\(min\Sigma dp[son_i],mn[i]\)其中\(mn[i]\)代表根节点到\(i\)节点路径上的最小边权
这样的话复杂度\(O(nm)\)无法通过此题
但观察数据范围
发现利用\(\Sigma k_i\)比较小的特点切入
虚树
思想
就是只将树上有用的点提取出来进行\(dp\),重构一棵树
这里指询问点和它们的\(lca\)
构建
考虑如何建一颗虚树。
首先我们要先对整棵树dfs一遍,求出他们的dfs序,然后对每个节点以dfs序为关键字从小到大排序
同时维护一个栈,表示从根到栈顶元素这条链。
设当前要加入的节点为\(p\),栈顶元素为\(x=s[top],lca\)为它们的最近公共祖先
因为我们按照\(dfs\)序遍历,所以\(p\)不可能是\(lca\)
那么现在会有两种情况
1.\(lca\)为\(x\),直接将\(p\)入栈
2.\(x\),\(p\)位于不同的子树中,则此时\(x\)所在子树已经遍历完了,我们需对其进行构造
设栈顶元素为\(x\),第二个为\(y\)
若\(dfn[y]>dfn[lca]\),可连边\(y->x\),将\(x\)出栈。
若\(dfn[y]=dfn[lca]\),\(y\)=\(lca\),连边\(lca->x\),子树构建完毕。
若\(dfn[y]<dfn[lca]\),即\(lca\)在\(x\)、\(y\)之间,连边\(lca->x\),\(x\)出栈,再将\(lca\)入栈,子树构建完毕。
然后重复这个过程就可以了,不理解的话可以自己手玩一下。。。
复杂度
大约是\(O(\Sigma k_i的)\),可能会带一些小常数
然后这题的代码贴在这里了:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = getchar();
if (ch == '-') w = -1 , ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return w * data;
}
typedef long long ll;
#define int ll
const int MAX_N = 250005;
const int MAX_LOG_N = 19;
struct Graph { int to, cost, next; } e[MAX_N << 1]; int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int u, int v, int w) { e[e_cnt] = (Graph){v, w, fir[u]}; fir[u] = e_cnt++; }
int N, M, s[MAX_N], _top, dfn[MAX_N], mn[MAX_N];
namespace cpp1 {
int dep[MAX_N], top[MAX_N], fa[MAX_N], size[MAX_N], son[MAX_N], tim;
void dfs1(int x) {
dep[x] = dep[fa[x]] + 1; size[x] = 1;
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa[x]) continue;
fa[v] = x; mn[v] = min(mn[x], e[i].cost);
dfs1(v);
size[x] += size[v];
if (size[v] > size[son[x]]) son[x] = v;
}
}
void dfs2(int x, int tp) {
top[x] = tp; dfn[x] = ++tim;
if (son[x]) dfs2(son[x], tp);
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to;
if (v == fa[x] || v == son[x]) continue;
dfs2(v, v);
}
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] > dep[y] ? y : x;
}
}
namespace cpp2 {
vector<int> G[MAX_N];
void add(int x, int y) { G[x].push_back(y); }
void ins(int x) {
if (_top == 1) { s[++_top] = x; return ; }
int lca = cpp1::LCA(x, s[_top]);
if (lca == s[_top]) return ;
while (_top > 1 && dfn[s[_top - 1]] >= dfn[lca]) add(s[_top - 1], s[_top]), _top--;
if (lca != s[_top]) add(lca, s[_top]), s[_top] = lca;
s[++_top] = x;
}
ll dfs(int x) {
if (G[x].size() == 0) return mn[x];
ll res = 0;
for (int i = 0; i < (int)G[x].size(); i++) res += dfs(G[x][i]);
G[x].clear();
return min(res, 1ll * mn[x]);
}
bool cmp(int a, int b) { return dfn[a] < dfn[b]; }
}
int a[MAX_N];
#undef int
int main () {
#define int ll
clearGraph();
N = gi();
for (int i = 1; i < N; i++) {
int u = gi(), v = gi(), w = gi();
Add_Edge(u, v, w);
Add_Edge(v, u, w);
}
fill(&mn[1], &mn[N + 1], LLONG_MAX / 2);
cpp1::dfs1(1); cpp1::dfs2(1, 1);
M = gi();
while (M--) {
int K = gi(); for (int i = 1; i <= K; i++) a[i] = gi();
sort(&a[1], &a[K + 1], cpp2::cmp);
s[_top = 1] = 1;
for (int i = 1; i <= K; i++) cpp2::ins(a[i]);
while (_top > 0) cpp2::add(s[_top - 1], s[_top]), _top--;
printf("%lld\n", cpp2::dfs(1));
}
return 0;
}
【LG2495】[SDOI2011]消耗战的更多相关文章
- 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 ...
- 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的 ...
- 2286: [Sdoi2011]消耗战
2286: [Sdoi2011]消耗战 链接 分析 虚树练习题. 构建虚树,在虚树上DP. 跟着gxb学虚-tree... 代码 #include <cstdio> #include &l ...
- BZOJ2286 [Sdoi2011]消耗战 和 BZOJ3611 [Heoi2014]大工程
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6371 Solved: 2496[Submit][Statu ...
随机推荐
- [USACO07DEC]Sightseeing Cows
嘟嘟嘟 这题好像属于01分数规划问题,叫什么最优比率生成环. 题目概括一下,就是求一个环,满足∑v[i] / ∑c[i]最大. 我们可以堆上面的式子变个型:令 x = ∑v[i] / ∑c[i],则x ...
- JSON数据转换之net.sf.json包的使用
转载 解析json之net.sf.json https://blog.csdn.net/itlwc/article/details/38442667 一.介绍 使用之前需要导入的jar包: json- ...
- java使用jsp建立项目+视频
项目下载:https://pan.baidu.com/s/1rfwrihd82zAmpt3r-mxIMw 视频+项目下载:https://pan.baidu.com/s/17Cm0wW-S9xiUy4 ...
- Node环境下实现less编译
今天在学习less的时候发现了在node中是可以渲染的,通过调用less的render方法渲染来生成css,所以写了个小Demo. var less = require('less'); var ht ...
- 【题解】洛谷P4391 [BOI2009] Radio Transmission(KMP)
洛谷P4391:https://www.luogu.org/problemnew/show/P4391 思路 对于给定的字符串 运用KMP思想 设P[x]为前x个字符前缀和后缀相同的最长长度 则对于题 ...
- Flask—07-建立自己的博客(01)
博客项目 一局王者的时间轻松学会用Flask建立一个属于自己的博客. 需求分析 用户注册登录 用户信息管理 博客发表回复 博客列表展示 博客分页展示 博客收藏点赞 搜索.统计.排序.… 目录结构 bl ...
- C++笔记005:用面向过程和面向对象方法求解圆形面积
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 结束了第一个hello world程序后,我们来用面向过程和面向对象两个方法来求解圆的面积这个问题,以能够更清晰的体会面向对象和面向过程. ...
- HTML学习日记之元信息meta标记
所谓meta标记就是用来描述一个HTML网页文档的属性,也称为元信息,这些信息并不会显示在浏览器的页面中,例如作者.日期和时间.网页描述.页面刷新等. 基本语法: <meta name = &q ...
- 利用GoAccess分析Nginx访问日志
原文链接:https://blog.csdn.net/yown/article/details/56027112 需求:及时得到线上用户访问日志分析统计结果,以便给开发.测试.运维.运营人员提供决策! ...
- 可以提高php编程效率的20个要点
整理了可以提高php编程效率的20个要点,发博客记录一下,需要的朋友可以参考. 1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id' ...