[update]

好像有个东西叫笛卡尔树,好像是这样建的.....

inline void build_d() {
stk[top = ] = ;
for(int i = ; i <= n; i++) {
while(top && val[stk[top]] <= val[i]) {
ls[i] = stk[top];
top--;
}
if(ls[i]) {
fa[ls[i]] = i;
}
if(top) {
fa[i] = stk[top];
rs[stk[top]] = i;
}
stk[++top] = i;
}
RT = stk[];
return;
}

题意:给定树上k个点,求切断这些点到根路径的最小代价。∑k <= 5e5

解:虚树。

构建虚树大概是这样的:设加入点与栈顶的lca为y,比较y和栈中第二个元素的DFS序大小关系。

代码如下:

 inline bool cmp(const int &a, const int &b) {
return pos[a] < pos[b];
} inline void build_t() {
std::sort(imp + , imp + k + , cmp);
TP = top = ;
stk[++top] = imp[];
use[imp[]] = Time;
E[imp[]] = ;
for(int i = ; i <= k; i++) {
int x = imp[i], y = lca(x, stk[top]);
if(use[x] != Time) {
use[x] = Time;
E[x] = ;
}
if(use[y] != Time) {
use[y] = Time;
E[y] = ;
}
while(top > && pos[y] <= pos[stk[top - ]]) {
ADD(stk[top - ], stk[top]);
top--;
}
if(stk[top] != y) {
ADD(y, stk[top]);
stk[top] = y;
}
stk[++top] = x;
}
while(top > ) {
ADD(stk[top - ], stk[top]);
top--;
}
RT = stk[top];
return;
}

然后本题建虚树跑DP就行了。注意虚树根节点的DP初值和虚树的边权。

 #include <cstdio>
#include <algorithm> typedef long long LL;
const int N = ;
const LL INF = 1e18; struct Edge {
int nex, v;
LL len;
}edge[N << ], EDGE[N << ]; int tp, TP; int e[N], siz[N], stk[N], top, Time, n, fa[N][], k, RT, num, pos[N], pw[N], now[N], imp[N], E[N], d[N], use[N];
LL f[N], small[N][]; inline void ADD(int x, int y, LL z) {
TP++;
EDGE[TP].v = y;
EDGE[TP].len = z;
EDGE[TP].nex = E[x];
E[x] = TP;
return;
} inline void add(int x, int y, LL z) {
top++;
edge[top].v = y;
edge[top].len = z;
edge[top].nex = e[x];
e[x] = top;
return;
} void DFS_1(int x, int father) { // get fa small
fa[x][] = father;
d[x] = d[father] + ;
pos[x] = ++num;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(y == father) {
continue;
}
small[y][] = edge[i].len;
DFS_1(y, x);
}
return;
} inline int lca(int x, int y) {
if(d[x] > d[y]) {
std::swap(x, y);
}
int t = pw[n];
while(t >= && d[x] < d[y]) {
if(d[fa[y][t]] >= d[x]) {
y = fa[y][t];
}
t--;
}
if(x == y) {
return x;
}
t = pw[n];
while(t >= && fa[x][] != fa[y][]) {
if(fa[x][t] != fa[y][t]) {
x = fa[x][t];
y = fa[y][t];
}
t--;
}
return fa[x][];
} inline bool cmp(const int &a, const int &b) {
return pos[a] < pos[b];
} inline LL getMin(int x, int y) {
LL ans = INF;
int t = pw[d[y] - d[x]];
while(t >= && y != x) {
if(d[fa[y][t]] >= d[x]) {
ans = std::min(ans, small[y][t]);
y = fa[y][t];
}
t--;
}
return ans;
} inline void build_t() {
std::sort(imp + , imp + k + , cmp);
TP = top = ;
stk[++top] = imp[];
use[imp[]] = Time;
E[imp[]] = ;
for(int i = ; i <= k; i++) {
int x = imp[i], y = lca(x, stk[top]);
if(use[x] != Time) {
use[x] = Time;
E[x] = ;
}
if(use[y] != Time) {
use[y] = Time;
E[y] = ;
}
while(top > && pos[y] <= pos[stk[top - ]]) {
ADD(stk[top - ], stk[top], getMin(stk[top - ], stk[top]));
top--;
}
if(stk[top] != y) {
ADD(y, stk[top], getMin(y, stk[top]));
stk[top] = y;
}
stk[++top] = x;
}
while(top > ) {
ADD(stk[top - ], stk[top], getMin(stk[top - ], stk[top]));
top--;
}
RT = stk[top];
return;
} void DFS(int x) {
siz[x] = (now[x] == Time);
LL temp = ;
for(int i = E[x]; i; i = EDGE[i].nex) {
int y = EDGE[i].v;
f[y] = EDGE[i].len;
DFS(y);
siz[x] += siz[y];
if(siz[y]) {
temp += f[y];
}
}
if(now[x] != Time) {
f[x] = std::min(f[x], temp);
}
return;
} void out(int x) {
return;
} int main() {
scanf("%d", &n);
/*if(n > 100) {
return -1;
}*/
int x, y; LL z;
for(int i = ; i < n; i++) {
scanf("%d%d%lld", &x, &y, &z);
add(x, y, z);
add(y, x, z);
}
// get lca min_edge
DFS_1(, );
for(int i = ; i <= n; i++) {
pw[i] = pw[i >> ] + ;
}
for(int j = ; j <= pw[n]; j++) {
for(int i = ; i <= n; i++) {
fa[i][j] = fa[fa[i][j - ]][j - ];
small[i][j] = std::min(small[i][j - ], small[fa[i][j - ]][j - ]);
}
} int m;
scanf("%d", &m);
for(Time = ; Time <= m; Time++) {
scanf("%d", &k);
//printf("\n k = %d \n", k);
for(int i = ; i <= k; i++) {
scanf("%d", &imp[i]);
now[imp[i]] = Time;
}
//printf("input over \n");
build_t();
//out(RT);
f[RT] = getMin(, RT);
DFS(RT);
printf("%lld\n", f[RT]);
} return ;
}

AC代码

[虚树模板] 洛谷P2495 消耗战的更多相关文章

  1. 【数论】卢卡斯定理模板 洛谷P3807

    [数论]卢卡斯定理模板 洛谷P3807 >>>>题目 [题目] https://www.luogu.org/problemnew/show/P3807 [输入格式] 第一行一个 ...

  2. KMP字符串匹配 模板 洛谷 P3375

    KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...

  3. 洛谷 P2495 [SDOI2011]消耗战(虚树,dp)

    题面 洛谷 题解 虚树+dp 关于虚树 了解一下 具体实现 inline void insert(int x) { if (top == 1) {s[++top] = x; return ;} int ...

  4. 洛谷P2495 [SDOI2011]消耗战(虚树dp)

    P2495 [SDOI2011]消耗战 题目链接 题解: 虚树\(dp\)入门题吧.虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是 ...

  5. 洛谷P2495 [SDOI2011]消耗战(虚树)

    题面 传送门 题解 为啥一直莫名其妙\(90\)分啊--重构了一下代码才\(A\)掉-- 先考虑直接\(dp\)怎么做 树形\(dp\)的时候,记一下断开某个节点的最小值,就是从根节点到它的路径上最短 ...

  6. ●洛谷P2495 [SDOI2011]消耗战

    题链: https://www.luogu.org/problemnew/show/P2495题解: 虚树入门,树形dp 推荐博客:http://blog.csdn.net/lych_cys/arti ...

  7. 树链剖分模板(洛谷P3384)

    洛谷P3384 #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " < ...

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

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

  9. [Bzoj2286][Sdoi2011]消耗战(虚树模板题附讲解)

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

随机推荐

  1. 容斥原理I

    普利斯记号 以下以"人"代指受条件约束的元素. \(K(x)\)表示刚好\(x\)人满足条件的方案数. \(S(x)\)表示至少\(x\)人满足条件的方案数. \(C(x)\)表示 ...

  2. pair work结对编程(张艺 杨伊)

    一.结对编程人员: 张艺(学号后三位:185) 杨伊(学号后三位:151) 二.这是我们工作的样子:(图片) 三.结对编程优缺点:  优点:  1.结对编程时间紧密,在一定程度上可以督促双方学习,提高 ...

  3. 对spring cloud config的一点理解

    以下部分纯属个人理解,但是结果都是经过demo验证. 一.spring cloud config介绍 spring cloud是spring家族中的一个微服务工具包,其中包含了很多微服务的工具.偏向于 ...

  4. Linux 第七章学习笔记

    1:链接概述 链接(linking)是将各种代码和数据部分收集起来并组合成为一个单一文件的过程,这个文件可被加载(或被拷贝)到存储并执行. 编译系统提供的调用预处理器.编译器.汇编器和链接器来构造目标 ...

  5. BF算法和KMP算法 python实现

    BF算法 def Index(s1,s2,pos = 0): """ BF算法 """ i = pos j = 0 while(i < ...

  6. 第三个sprint冲刺第三阶段

    公测版:

  7. vue element-ui upload 实现带token上传

    <el-upload class="upload-demo" :data ="uploadData" :headers = "headers&q ...

  8. org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unexpected failure during bean definition parsing Offending resource: class path resource [applicationC

    这个错误是 org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration proble ...

  9. 归并排序详解(python实现)

    因为上个星期leetcode的一道题(Median of Two Sorted Arrays)所以想仔细了解一下归并排序的实现. 还是先阐述一下排序思路: 首先归并排序使用了二分法,归根到底的思想还是 ...

  10. Log4J日志信息配置文件详解

    原文地址: http://blog.csdn.net/wuxintdrh/article/details/78282097 使用log4j 记录日志甚是方便,其提供了两种日志配置方式,log4j.pr ...