BZOJ 2286 [Sdoi2011]消耗战 ——虚树
虚树第一题。
大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP
#include <map>
#include <ctime>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define ll long long
#define inf 1e17
#define maxn 500005 int n,m,k,a[maxn];
int h[maxn],to[maxn],ne[maxn],w[maxn],en=0;
int dep[maxn],st[maxn][21],_log[maxn],in[maxn],out[maxn],tot,ord[maxn];
ll mn[maxn];int sta[maxn],top=0,last[maxn],idx=0; void add(int a,int b,int c)
{to[en]=b;ne[en]=h[a];w[en]=c;h[a]=en++;} void dfs(int o,int fa)
{
in[o]=++tot;ord[tot]=o;
for (int i=h[o];i>=0;i=ne[i])
if (to[i]!=fa){
dep[to[i]]=dep[o]+1;
mn[to[i]]=min(mn[o],(ll)w[i]);
dfs(to[i],o);
ord[++tot]=o;
}
out[o]=tot;
} void initst()
{
F(i,2,maxn-1) _log[i]=_log[i>>1]+1;
F(i,1,20) F(j,1,tot-(1<<i)+1)
{
if (dep[st[j][i-1]]<dep[st[j+(1<<i-1)][i-1]]) st[j][i]=st[j][i-1];
else st[j][i]=st[j+(1<<i-1)][i-1];
}
} int lca(int l,int r)
{
if (l==r) return l;
l=in[l],r=in[r];
if (l>r) swap(l,r);
int tmp=_log[r-l+1];// printf("tmp is %d\n",tmp);
if (dep[st[l][tmp]]<dep[st[r-(1<<tmp)+1][tmp]]) return st[l][tmp];
else return st[r-(1<<tmp)+1][tmp];
} bool cmp(int a,int b)
{return in[a]<in[b];} int hd[maxn],tl[maxn],nxt[maxn],ed,se[maxn]; void add_edge(int a,int b)
{
if (last[a]!=idx) last[a]=idx,hd[a]=-1;
tl[ed]=b;
nxt[ed]=hd[a];
hd[a]=ed++;
} ll dfs2(int o,int fa)
{
ll ret=0;
for (int i=hd[o];i>=0;i=nxt[i])
if (tl[i]!=fa)
ret+=dfs2(tl[i],o);
if(se[o]==idx) return mn[o];
return min(ret,(ll)mn[o]);
} int main()
{
memset(h,-1,sizeof h);
scanf("%d",&n); mn[1]=inf;
F(i,1,n-1)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
dfs(1,0);
F(i,1,tot)st[i][0]=ord[i];initst();
scanf("%d",&m);
while (m--)
{
ed=0; idx++;
scanf("%d",&k);
F(i,1,k) scanf("%d",&a[i]),se[a[i]]=idx;
sort(a+1,a+k+1,cmp);
sta[top=1]=1;
F(i,1,k)
{
int l=in[sta[top]],r=in[a[i]],x=lca(sta[top],a[i]);
while (dep[sta[top]]>dep[x])
{
int tmp;
if (dep[x]>dep[sta[top-1]]) tmp=x;
else tmp=sta[top-1];
add_edge(sta[top],tmp);
add_edge(tmp,sta[top]);
top--;
}
if (x!=sta[top]) sta[++top]=x;
sta[++top]=a[i];
}
while (top>1) add_edge(sta[top],sta[top-1]),add_edge(sta[top-1],sta[top]),top--;
printf("%lld\n",dfs2(1,0));
}
}
BZOJ 2286 [Sdoi2011]消耗战 ——虚树的更多相关文章
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树
Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军 ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
- BZOJ 2286: [Sdoi2011]消耗战
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2082 Solved: 736[Submit][Status] ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
随机推荐
- Android GreenDao 深查询 n:m 的关系
在我的应用程序这样设计的关系:和我想选择至少一个用户作为一个朋友的所有聊天. 基本上,我想要执行以下查询:\ SELECT c.* FROM CHAT c, USER u, UserChats uc ...
- uvm_reg——寄存器模型(三)
uvm_reg 是uvm_reg_field , 包含所有uvm_reg_field 所有的函数.
- IOS之constraints
anyway, you can do this with auto layout. You can do it entirely in IB as of Xcode 5.1. Let's start ...
- KVC/KVO 本质
KVO 的实现原理 KVO是关于runtime机制实现的 当某个类的对象属性第一次被观察时,系统就会在运行期动态地创建该类的一个派生类,在这个派生类中重写基类中任何被观察属性的setter方法.派生类 ...
- 分析(ExtractTransformLoad)与挖掘(DataMine)有何区别 ?
首先,介绍一下ETL 和 DM: ETL/Extraction-Transformation-Loading——用于完成DB到DW的数据转存,它将DB中的某一个时间点的状态,“抽取”出来,根据 ...
- C-基础:表达式中存在有符号类型和无符号类型时,都自动转换为无符号类型
void foo(void) { unsigned ; ; (a+b > ) puts("> 6") : puts("<= 6"); } 答案 ...
- eclips配置
新建空workspace import... configMathod:main:project:eFT-Debug@eFTSlnC/C++ Aplication /media/B/testspa2. ...
- Mac屏幕亮度保存
关于保存屏幕亮度的方法,论坛上已有几种,搜索 NVRAM 会出来很多教程,在此不再详述,可以参考帖子http://www.idelta.info/archives/nvram_on_hackintos ...
- (59)zabbix拓扑图展示链路状况Link indicators
Link indicators介绍 上一篇已经了解了如何配置zabbix map,也提到了如何连接两个map元素,这节我们来讲两个map元素之间的链路指示配置. 我们需要在链路上配置trigger,如 ...
- mem之读操作调式总结(跟入栈出栈有关)
现象: 1.当case比较复杂的时候(含有for循环对mem进行读/写) 发现for循环时总是有汇编指令不执行跳过去了,(其实是汇编不熟和指令太多无法理智分析指令了). 事实是指令是对的,但执行错了( ...