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号点与查询 ...
随机推荐
- Lucene-安装和运行Demo程序
Lucene是什么 Lucene是一款高性能.可扩展的信息检索工具库.- Lucene In Action Lucene版本:7.1 一.下载安装包 https://lucene.apache.org ...
- uvm_pkg——老板,打包带走
Thus spake the master programmer: “After three day without programming, life becomes meaningless.” 编 ...
- Hadoop集群_VSFTP安装配置
原作者写的太好了,我这个菜鸟不自觉就转载了,原文链接:http://www.cnblogs.com/xia520pi/archive/2012/05/16/2503864.html 如果,您认为阅读这 ...
- Python安装第三方库文件工具——pip
Python安装第三方库文件一般使用pip. 1.pip的安装 (1)下载pip 进入https://pypi.python.org/pypi/pip#downloads
- sql中保留2位小数
问题: 数据库里的 float momey 类型,都会精确到多位小数.但有时候 我们不需要那么精确,例如,只精确到两位有效数字. 解决: 1. 使用 Round() 函数,如 Round(@num,2 ...
- siege4压测脚本示例
agent="Siege 1.0"rcconfig="/opt/siege4.0/etc/siegerc"concurrent=$1repet=$2url=&q ...
- codevs 2277 爱吃皮蛋的小明(水题日常)
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 小明特别爱吃蛋,特别是皮蛋.他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他 ...
- 通过StringBuilder的reverse()实现倒序
import java.util.Scanner; public class ReverseString { public static void main(String[] args) { Scan ...
- Error:(3, 32) java: 程序包org.springframework.boot不存在
解决方案一: 找同事传一份D:\maven_repository\org\springframework\boot ,如图所示的位置,添加进去立刻就不报红.我也可以给你发.... 解决方案二: ...
- Asp.Net Core 进阶(一) —— 读取appsettings.json
我们以前在Asp.Net MVC中使用 System.Configuration.ConfigurationManager 来读取web.config文件.但是Asp.Net Core MVC已经没有 ...