bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286
https://www.luogu.org/problemnew/show/P2495
学习(抄)了 hzwer 的代码,觉得写得很好。http://hzwer.com/6188.html
有一个 “如果排序后第 i 个关键点和第 i-1 个关键点的 lca 是第 i-1 个关键点,就舍弃第 i 个关键点” 的操作,觉得很好。
把 hd[ ] 数组清空写在了 dfs 里,觉得很好。
自己一开始写了一个倍增找链上边权最小值,用来给虚树的边赋值,参考之后发现只要记录一个 “到根的路径上的最小边权” 就行了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int rdn()
{
int ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return fx?ret:-ret;
}
ll Mx(ll a,ll b){return a>b?a:b;}
ll Mn(ll a,ll b){return a<b?a:b;} const int N=,K=;const ll INF=3e10+;//for dp
int n,hd[N],xnt,to[N<<],nxt[N<<],w[N<<];
int dep[N],pre[N][K],bin[K],dfn[N],tim; ll mn[N];
bool cmp(int a,int b){return dfn[a]<dfn[b];}
void add(int x,int y,int z){to[++xnt]=y;nxt[xnt]=hd[x];hd[x]=xnt;w[xnt]=z;}
void dfs(int cr,int fa)
{
dfn[cr]=++tim; dep[cr]=dep[fa]+;
pre[cr][]=fa;
for(int t=;bin[t]<=dep[cr];t++)
pre[cr][t]=pre[pre[cr][t-]][t-];
for(int i=hd[cr],v;i;i=nxt[i])
if((v=to[i])!=fa)
{
mn[v]=Mn(mn[cr],w[i]);
dfs(v,cr);
}
}
int get_lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
int d=dep[x]-dep[y];
for(int t=;bin[t]<=d;t++)
if(d&bin[t])x=pre[x][t]; if(x==y)return x;
for(int t=;t>=;t--)
if(pre[x][t]!=pre[y][t])
x=pre[x][t],y=pre[y][t];
return pre[x][];
}
namespace Tr{
int hd[N],xnt,to[N],nxt[N];
int p[N],tot,sta[N],top; ll dp[N];
void add(int x,int y){to[++xnt]=y;nxt[xnt]=hd[x];hd[x]=xnt;}
void get_tr()
{
xnt=;
sort(p+,p+tot+,cmp);
int lm=tot; p[tot=]=p[];
for(int i=;i<=lm;i++)
if(get_lca(p[i],p[tot])!=p[tot])p[++tot]=p[i];
sta[top=]=;
for(int i=;i<=tot;i++)
{
int u=p[i], lca=get_lca(u,sta[top]);
while(top&&dfn[lca]<dfn[sta[top]])
{
if(dfn[sta[top-]]<dfn[lca])
add(lca,sta[top]);
else add(sta[top-],sta[top]);
top--;
}
if(sta[top]!=lca)sta[++top]=lca;
sta[++top]=u;
}
for(int i=;i<top;i++)add(sta[i],sta[i+]);
}
void dfs(int cr)
{
if(!hd[cr]){dp[cr]=mn[cr];return;}
dp[cr]=;
for(int i=hd[cr],v;i;i=nxt[i])
{
dfs(v=to[i]); dp[cr]+=dp[v];
}
hd[cr]=;//////
dp[cr]=Mn(dp[cr],mn[cr]);
}
void solve()
{
int k=rdn(); tot=;
for(int i=,d;i<=k;i++)
d=rdn(),p[++tot]=d;
get_tr(); dfs(); printf("%lld\n",dp[]);
}
}
int main()
{
bin[]=;for(int i=;i<=;i++)bin[i]=bin[i-]<<;
n=rdn();
for(int i=,u,v,z;i<n;i++)
u=rdn(),v=rdn(),z=rdn(),add(u,v,z),add(v,u,z);
mn[]=INF; dfs(,);
int Q=rdn(); while(Q--)Tr::solve();
return ;
}
bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树的更多相关文章
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...
- 洛谷 P2495 [SDOI2011]消耗战(虚树,dp)
题面 洛谷 题解 虚树+dp 关于虚树 了解一下 具体实现 inline void insert(int x) { if (top == 1) {s[++top] = x; return ;} int ...
- 洛谷P2495 [SDOI2011]消耗战(虚树dp)
P2495 [SDOI2011]消耗战 题目链接 题解: 虚树\(dp\)入门题吧.虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- 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\)的时候,记一下断开某个节点的最小值,就是从根节点到它的路径上最短 ...
- BZOJ 2286 [Sdoi2011]消耗战 ——虚树
虚树第一题. 大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP #include <map> #include <ctime> #include <cmath&g ...
随机推荐
- shell 命令参数
$# 是传给脚本的参数个数$0 是脚本本身的名字$1 是传递给该shell脚本的第一个参数$2 是传递给该shell脚本的第二个参数$@ 是传给脚本的所有参数的列表$* 是以一个单字符串显示所有向脚本 ...
- elasticsearch 路由文档到分片
路由文档到分片 当你索引一个文档,它被存储在单独一个主分片上.Elasticsearch是如何知道文档属于哪个分片的呢?当你创建一个新文档,它是如何知道是应该存储在分片1还是分片2上的呢? 进程不能是 ...
- BZOJ1461 字符串的匹配
什么字符串...明明是两个数列... 分类上来讲,还是一道很好的noip题...(雾) 首先,kmp会不会?(答:会!) 其次,树状数组求顺序对会不会?(再答:会!) 讲完了!>.< 进入 ...
- SQL Server 调优系列基础篇 - 并行运算总结(二)
前言 上一篇文章我们介绍了查看查询计划的并行运行方式. 本篇我们接着分析SQL Server的并行运算. 闲言少叙,直接进入本篇的正题. 技术准备 同前几篇一样,基于SQL Server2008R2版 ...
- C# 接收form表单中多个相同name值的问题
以前接收form表单的值直接用FormCollection或自定义类来接收,当有多个相同Name的值时会自动用“,”隔开,这样就有了一个问题,当值中本身就含有“,”时就比较难处理了. 所以解决方法就是 ...
- windows 环境下python 安装 pypcap 并用pyinstaller打包到exe,解决DLL 加载失败。
安装 PYQT5 pypcap 环境: windows10_x64 python3.6.3 pycharm2017.2.4 备注: 需要安装 Visual C++ Build Tools 2015 可 ...
- pandas 常用语句
pandas的功能非常强大,支持类似与sql的数据增.删.查.改,并且带有丰富的数据处理函数: 支持时间序列分析功能:支持灵活处理缺失数据等. pandas的基本数据结构是Series和DataFra ...
- delete p和delete [] p的区别(转)
operator new 和 operator delete函数有两个重载版本,每个版本支持相关的new表达式和delete表达式: void* operator new (size_t); // a ...
- windowsphone开发页面跳转到另一个dll中的页面
WP的页面跳转一般是只能跳转到本DLL的页面, 如果要跳转到其他DLL的页面则需要这样写 (Application.Current.RootVisual as PhoneApplicationFram ...
- MyEclipse 2017 CI 9 发布(附下载)
挑战全年最低价!MyEclipse线上狂欢继续!火热开启中>> 在进入年底之时,2017 CI 9是我们最大的版本发布之一.在新版本中,我们添加了对Angular 5和TypeScript ...