2286: [Sdoi2011]消耗战
2286: [Sdoi2011]消耗战
分析
虚树练习题。
构建虚树,在虚树上DP。
跟着gxb学虚-tree。。。
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype> using namespace std; const int N = ;
const int INF = 1e9; int head[N<<],nxt[N<<],to[N<<],w[N<<],TotE;
int deth[N],val[N],siz[N],dfn[N],fa[N],son[N],bel[N],sk[N<<],top,Time_index;
int A[N]; inline int read() {
int x = ,f = ;char ch =getchar();
for (; !isdigit(ch); ch=getchar()) if (ch=='-') f=-;
for (; isdigit(ch); ch=getchar()) x = x*+ch-'';
return x * f;
}
bool cmp(const int &a,const int &b) {
return dfn[a] < dfn[b];
}
void Add_edge(int u,int v,int c) {
++TotE;to[TotE] = v;w[TotE] = c;nxt[TotE] = head[u];head[u] = TotE;
++TotE;to[TotE] = u;w[TotE] = c;nxt[TotE] = head[v];head[v] = TotE;
}
void add_edge(int u,int v) {
++TotE;to[TotE] = v;nxt[TotE] = head[u];head[u] = TotE;
}
void dfs1(int u,int mn) {
val[u] = mn;
siz[u] = ;
for (int i=head[u]; i; i=nxt[i]) {
int v = to[i];
if (v==fa[u]) continue;
fa[v] = u;
deth[v] = deth[u] + ;
dfs1(v,min(w[i],mn));
siz[u] += siz[v];
if (!son[u] || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u,int top) {
bel[u] = top;
dfn[u] = ++Time_index;
if (!son[u]) return;
dfs2(son[u],top);
for (int i=head[u]; i; i=nxt[i]) {
int v = to[i];
if (v==fa[u] || v==son[u]) continue;
dfs2(v,v);
}
}
int Lca(int u,int v) {
while (bel[u] != bel[v]) {
if (deth[bel[u]] < deth[bel[v]]) swap(u,v);
u = fa[bel[u]];
}
if (deth[u] < deth[v]) return u;
return v;
}
void Insert(int p) {
int x = sk[top];
if (x==) {sk[++top] = p;return ;}
int lca = Lca(p,x);
if (lca == x) return; // 此题构建虚树不同的是,若x==lca,不加入p。
while (lca != x) {
int y = sk[--top];
if (dfn[y] < dfn[lca]) {
add_edge(lca,x);
sk[++top] = lca;
break;
}
add_edge(y,x);
x = sk[top];
}
sk[++top] = p;
}
long long DP(int u) {
if (!head[u]) return val[u];
long long ans = ;
for (int i=head[u]; i; i=nxt[i]) ans += DP(to[i]);
head[u] = ;
return min(ans,(long long)val[u]);
}
int main() {
int n = read();
for (int i=; i<n; ++i) {
int u = read(),v = read(),w = read();
Add_edge(u,v,w);
}
deth[] = ;
dfs1(,INF);
dfs2(,);
TotE = ;
memset(head,,sizeof(head)); int m = read();
while (m--) {
TotE = ;sk[top = ] = ; int k = read();
for (int i=; i<=k; ++i) A[i] = read();
sort(A+,A+k+,cmp);
for (int i=; i<=k; ++i) Insert(A[i]);
while (--top) add_edge(sk[top],sk[top+]); long long ans = ;
for (int i=head[]; i; i=nxt[i]) ans += DP(to[i]); // 单独处理1的情况,否则val[1]设为很大的longlong数,INF不够。
printf("%lld\n",ans);
head[] = ;
}
return ;
}
2286: [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】2286: [Sdoi2011消耗战
http://www.lydsy.com/JudgeOnline/problem.php?id=2286 题意:n个点的边加权树,m个询问,每次询问给出的k个点与结点1分离的最小代价.(n<=2 ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- BZOJ 2286 [Sdoi2011]消耗战(虚树+树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题目大意] 出一棵边权树,每次给出一些关键点,求最小边割集, 使得1点与各个关 ...
- bzoj 2286: [Sdoi2011消耗战
#include<cstdio> #include<iostream> #define M 1000009 #define N 250009 #define ll long l ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- BZOJ 2286: [Sdoi2011消耗战 [DP 虚树]
传送门 题意: 删除价值和最小的边使得$1$号点与$k$个关键点不连通 一个树形DP...但是询问多次,保证总的关键点数为$O(n)$ 先说一下这个$DP$ $f[i]$表示子树$i$中的关键点与$1 ...
随机推荐
- ModuleNotFoundError: No module named 'yaml'
ModuleNotFoundError: No module named 'yaml' 需要安装 pyyaml 包
- 【转载】#344 - Hidden Base Class Member Is Invoked Based on Declared Type of Object
When you use the new modifier to hide a base class method, it will still be called by objects whose ...
- 【转】Druid连接池一个设置引发的血案
https://my.oschina.net/haogrgr/blog/224010 今天在一台配置很低的机器上运行批量更新的程序~~~ 大概跑了三十分钟~~~这配置~~~这程序~~~ 然后华丽丽的报 ...
- 第30章 ADC—电压采集—零死角玩转STM32-F429系列
第30章 ADC—电压采集 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fireg ...
- @NotEmpty、@NotNull、@NotBlank 的区别
@NotEmpty 用在集合上面(不能注释枚举) @NotBlank用在String上面 @NotNull用在所有类型上面 1. @NotEmpty Asserts that the annotate ...
- 使用C#的新特性:可空类型
随着C#语言最新标准的出炉,现在它也提供了对可空类型的支持.这个小变化将会在处理那些包括可选项的数据库记录时非常有用.当然在其他地方,它也是非常有用的. 简单说来,可空数据类型就是包含了所定义的数据类 ...
- udp重发java实现
最近在处理框架通讯方面的问题,通过积累的开发经验,其实在很多情况(尤其是实时大数据量),udp是占有很多优势的:不需要连接,只管发送,理论上要快很多; 另外在穿墙上占有很大优势: 但是最大的一个问题就 ...
- C++ 编写的解码器小程序 map
c++ prime 5 ex11_4 代码如下 // ex11_4_word_transform.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h&quo ...
- BZOJ2005: [Noi2010]能量采集(容斥原理 莫比乌斯反演)
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 4727 Solved: 2877[Submit][Status][Discuss] Descript ...
- Shell学习——列出当前路径下所有目录
1.ls -d */[root@client02 ~]# ls -d */Desktop/ Documents/ Downloads/ jq-1.5/ Music/ Pictures/ Public/ ...