洛谷 P2495 [SDOI2011]消耗战(虚树,dp)
题面
题解
虚树+dp
关于虚树
了解一下
- 具体实现
inline void insert(int x) {
if (top == 1) {s[++top] = x; return ;}
int lca = query(x, s[top]);
while (top > 1 && dfn[s[top-1]] >= dfn[lca]) t[s[top-1]].push_back(s[top]), top--;
if (lca != s[top]) t[lca].push_back(s[top]), s[top] = lca;
s[++top] = x;
return ;
}
bool cmp(int a, int b) {
return dfn[a] < dfn[b];
}//dfn为在dfs序上的位置
main() {//o为输入的关键点
sort(o+1, o+1+k, cmp);
s[top = 1] = 1;
for (int i = 1; i <= k; i++) insert(o[i]);
for (int i = 1; i < top; i++) t[s[i]].push_back(s[i+1]);
}
然后对于这个虚树\(dp\)
\(a\)数组表示当前点到根这条链路径最小值
Code
#include<bits/stdc++.h>
#define LL long long
#define RG register
using namespace std;
inline int gi() {
RG int x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-'0', c = getchar();
return f ? -x : x;
}
const int N = 250010, INF = 2147483647;
struct node {
int to, next, w;
}g[N<<1];
int last[N], gl;
inline void add(int x, int y, int z) {
g[++gl] = (node) {y, last[x], z};
last[x] = gl;
return ;
}
int anc[N][21], dfn[N], cnt, dep[N], a[N];
void init(int u, int fa) {
anc[u][0] = fa;
dfn[u] = ++cnt;
for (int i = 1; i <= 20; i++)
anc[u][i] = anc[anc[u][i-1]][i-1];
for (int i = last[u]; i; i = g[i].next) {
int v = g[i].to;
if (v == fa) continue;
dep[v] = dep[u]+1;
a[v] = min(g[i].w, a[u]);
init(v, u);
}
return ;
}
inline int query(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (dep[anc[x][i]] >= dep[y])
x = anc[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (anc[x][i] != anc[y][i])
x = anc[x][i], y = anc[y][i];
return anc[x][0];
}
vector<int> t[N];
int o[N], s[N], top;
bool cmp(int a, int b) {
return dfn[a] < dfn[b];
}
inline void insert(int x) {
if (top == 1) {s[++top] = x; return ;}
int lca = query(x, s[top]);
if (lca == s[top]) return ;//祖先都走不到,它肯定也走不到哈
while (top > 1 && dfn[s[top-1]] >= dfn[lca]) t[s[top-1]].push_back(s[top]), top--;
if (lca != s[top]) t[lca].push_back(s[top]), s[top] = lca;
s[++top] = x;
return ;
}
LL dp(int u) {
LL S = 0;
if (!t[u].size()) return a[u];
for (int i = 0; i < (int)t[u].size(); i++) {
int v = t[u][i];
S += dp(v);
}
t[u].clear();
if (u==1) return S;
return min(S, 1ll*a[u]);
}
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int n = gi();
for (int i = 1; i < n; i++) {
int x = gi(), y = gi(), z = gi();
add(x, y, z); add(y, x, z);
}
a[1] = INF;
init(1, 0);
int m = gi();
while (m--) {
int k = gi();
for (int i = 1; i <= k; i++)
o[i] = gi();
sort(o+1, o+1+k, cmp);
s[top = 1] = 1;
for (int i = 1; i <= k; i++) insert(o[i]);
for (int i = 1; i < top; i++)
t[s[i]].push_back(s[i+1]);
printf("%lld\n", dp(1));
}
return 0;
}
洛谷 P2495 [SDOI2011]消耗战(虚树,dp)的更多相关文章
- bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286 https://www.luogu.org/problemnew/show/P2495 ...
- 洛谷P2495 [SDOI2011]消耗战(虚树dp)
P2495 [SDOI2011]消耗战 题目链接 题解: 虚树\(dp\)入门题吧.虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- ●洛谷P2495 [SDOI2011]消耗战
题链: https://www.luogu.org/problemnew/show/P2495题解: 虚树入门,树形dp 推荐博客:http://blog.csdn.net/lych_cys/arti ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
- 洛谷P2495 [SDOI2011]消耗战(虚树)
题面 传送门 题解 为啥一直莫名其妙\(90\)分啊--重构了一下代码才\(A\)掉-- 先考虑直接\(dp\)怎么做 树形\(dp\)的时候,记一下断开某个节点的最小值,就是从根节点到它的路径上最短 ...
- [洛谷P2495][SDOI2011]消耗战
题目大意:有一棵$n(n\leqslant2.5\times10^5)$个节点的带边权的树,$m$个询问,每次询问给出$k(\sum\limits_{i=1}^mk_i\leqslant5\times ...
- 洛谷 P3233 [HNOI2014]世界树(虚树+dp)
题面 luogu 题解 数据范围已经告诉我们是虚树了,考虑如何在虚树上面\(dp\) 以下摘自hzwer博客: 构建虚树以后两遍dp处理出虚树上每个点最近的议事处 然后枚举虚树上每一条边,考虑其对两端 ...
随机推荐
- Java-Decimal
import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; public c ...
- HightCharts开发总结
1. 简介:Highcharts是一款纯javascript编写的图表库,能够在Web网站或Web应用中添加交互性的图表,现在官方的最新版本为Highcharts-4.2.3. 2. 兼容性: ...
- [GO]猜数字的小游戏
随机生成四位数字,然后用户输入四位数字,然后根据提示一步步猜到随机数 package main import ( "math/rand" "time" &quo ...
- jstl c
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 例子:list中有两 ...
- (转)第一次发博客-说说我的B/S开发框架(asp.net mvc + web api + easyui)
原文地址:http://www.cnblogs.com/xqin/archive/2013/05/29/3105291.html 前言 这些年一直在.net下做企业web系统开发,前前后后经历的不同的 ...
- FractalNet(分形网络)
-Argues that key is transitioning effectively from shallow to deep and residual representations are ...
- 关于在审查元素中看到的::before与::after
审查元素中看到的这两个标签,表示内容并不在元素中,而是在css中,可以查看style看到具体内容. 一般来说这样做是为了清除浮动(clearfix)的代码,防止后边的容器因为浮动出现布局的混乱. 添加 ...
- vs调试的时候debug和release的区别
今天在VS项目中调式遇到一个问题,断点快速查询变量的发现变两竟然不存在 花了一个小时到处百度也查不出一个所以然,后来请教了大神才知道VS调试有debug和release两个模式, vs中的程序有deb ...
- Expression表单式树
余于项目中逢Expression(表达式树),然今未明其用途也,记之以温. using System; using System.Collections.Generic; using System.L ...
- 苹果微信内置浏览器cookie
苹果微信内置浏览器cookie会被自动清掉,但safari不会清除,原因还未找到,解决方法是把前端把数据通过header传到后台