题面

虚树的模板题:

虚树的思想是只保留有用的点(在这道题目里面显然是标记点和lca),然后重新构建一棵树,从而使节点大大减少,优化复杂度

我们维护一条链(以1号点为根),这条链左边的所有在虚树上的位置都已经处理完毕;而这条链右边的和下面的都未处理;

这条链我们用栈来维护;

对于要新加的询问点now,对于虚树的影响有四种情况:(lc表示x与st[top]的LCA)

1.lc==st[top]      :  在虚树上连接st[top]与now

.

2.lc在st[top]与st[top-1]之间;在虚树上连接lc与st[top],--top,然后now进栈;

3.lc==st[top-1]    :--top,然后now进栈;

4.lc在st[top-1]之上 :在虚树上连接st[top-1]与st[top],然后退栈,重复以上步骤知道出现情况1、2、3;

在最后,我们把这条链加入到虚树中,这样一颗完美的虚树就建成了;

在每次建立虚树的时候,我们要实时清空虚树,否则时间复杂度会退化成O(n^2);

然后在这个虚树上跑dp,就可以了;

#include <bits/stdc++.h>
#define int long long
#define inc(i,a,b) for(register int i=a;i<=b;i++)
#define dec(i,a,b) for(register int i=a;i>=b;i--)
using namespace std;
int head1[500010],cnt1,head2[500010],cnt2;
class littlestar{
public:
int to,nxt;
long long w;
void add1(int u,int v,long long gg){
to=v; nxt=head1[u];
head1[u]=cnt1; w=gg;
}
void add2(int u,int v){
to=v; nxt=head2[u];
head2[u]=cnt2;
}
}star1[500010*2],star2[500010*2];
int n,f[500010][25],dfn[500010],cur,dep[500010];
long long minv[500010];
void dfs(int u)
{
inc(i,0,19){
f[u][i+1]=f[f[u][i]][i];
}
dfn[u]=++cur;
for(int i=head1[u];i;i=star1[i].nxt){
int v=star1[i].to;
if(!dfn[v]){
f[v][0]=u;
dep[v]=dep[u]+1;
minv[v]=min(minv[u],star1[i].w);
dfs(v);
}
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
dec(i,20,0){
if(dep[f[x][i]]>=dep[y]) x=f[x][i];
}
if(x==y) return x;
dec(i,20,0){
if(f[x][i]!=f[y][i]){
x=f[x][i]; y=f[y][i];
}
}
return f[x][0];
}
bool cmp(int x,int y){return dfn[x]<dfn[y];}
int num;
int judge[500001],h[500010];
int st[500010],top;
int dp(int u)
{
long long summ=0;
for(int i=head2[u];i;i=star2[i].nxt){
int v=star2[i].to;
summ+=dp(v);
}
long long ans=0;
if(judge[u]) ans=minv[u];
else ans=min(minv[u],summ);
judge[u]=0;
head2[u]=0;
return ans;
}
signed main()
{
cin>>n;
inc(i,1,n-1){
int u,v; long long w;
scanf("%lld%lld%lld",&u,&v,&w);
star1[++cnt1].add1(u,v,w);
star1[++cnt1].add1(v,u,w);
}
minv[1]=1e17+21;
dfs(1);
int q; scanf("%lld",&q);
while(q--){
scanf("%lld",&num);
inc(i,1,num){
scanf("%lld",&h[i]);
judge[h[i]]=1;
}
sort(h+1,h+1+num,cmp);
top=1;
st[top]=h[1];
inc(i,2,num){
int LCA=lca(h[i],st[top]);
while(true){
if(dep[LCA]>=dep[st[top-1]]){
if(LCA!=st[top]){
star2[++cnt2].add2(LCA,st[top]);
if(LCA!=st[top-1]){
st[top]=LCA;
}
else{
--top;
}
}
else{
break;
}
}
else{
star2[++cnt2].add2(st[top-1],st[top]);
--top;
}
}
st[++top]=h[i];
}
while(--top){
star2[++cnt2].add2(st[top],st[top+1]);
}
cout<<dp(st[1])<<endl;
cnt2=0;
}
}

[SDOI2011]消耗战 题解的更多相关文章

  1. BZOJ2286:[SDOI2011]消耗战——题解

    +++++++++++++++++++++++++++++++++++++++++++ +本文作者:luyouqi233. + +欢迎访问我的博客:http://www.cnblogs.com/luy ...

  2. 【LG2495】[SDOI2011]消耗战

    [LG2495][SDOI2011]消耗战 题面 洛谷 题解 参考博客 题意 给你\(n\)个点的一棵树 \(m\)个询问,每个询问给出\(k\)个点 求将这\(k\)个点与\(1\)号点断掉的最小代 ...

  3. 【BZOJ2286】[Sdoi2011]消耗战 虚树

    [BZOJ2286][Sdoi2011]消耗战 Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的 ...

  4. BZOJ2286 [Sdoi2011]消耗战 和 BZOJ3611 [Heoi2014]大工程

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

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

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

  6. BZOJ 2286: [Sdoi2011]消耗战

    2286: [Sdoi2011消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2082  Solved: 736[Submit][Status] ...

  7. bzoj 2286: [Sdoi2011]消耗战 虚树+树dp

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...

  8. bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战

    http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...

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

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

随机推荐

  1. 捣乱Linux环境下的C语言

    #include <stdlib.h> 头文件作用.CSDN C 标准库 – <stdlib.h> | 菜鸟教程  https://www.runoob.com/cprogra ...

  2. 前端工程师需要掌握的 Babel 知识

    在前端圈子里,对于 Babel,大家肯定都比较熟悉了.如果哪天少了它,对于前端工程师来说肯定是个噩梦.Babel 的工作原理是怎样的可能了解的人就不太多了.本文将主要介绍 Babel 的工作原理以及怎 ...

  3. elasticsearch-head后台运行

    运行插件 # npm run start > elasticsearch-head@0.0.0 start /usr/local/elasticsearch-head-master > g ...

  4. Laravel 配置

    首页 问答社区 中文文档 API Composer Github 配置说明 框架下载好了,但是想要很好的使用,可能我们还有一些东西需要知道,这就是配置.和项目有关的配置是在 app/config 文件 ...

  5. 03 HTTP协议与HTTPS协议

    一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...

  6. flask 第十篇 after_request before_request

    Flask我们已经学习很多基础知识了,现在有一个问题 我们现在有一个 Flask 程序其中有3个路由和视图函数,如下: from flask import Flask app = Flask(__na ...

  7. 在windows系统搭建并运行一个Flutter项目

    搭建Flutter之前需要已经安装好相应的Flutter开发环境,如果没安装好相应环境的可以查看在windows系统搭建Flutter开发环境 搭建Flutter项目可以通过命令行搭建,或者通过and ...

  8. linux 搜索文件夹下的所有文件内容

    find . -type f -name "*.*" | xargs grep "博客园"

  9. MAC为Apache2服务器配置多个虚拟主机

    Mac 下自带的 Apache 配置 2016年01月25日 00:25:03 阅读数:1292 参考: http://www.cnblogs.com/snandy/archive/2012/11/1 ...

  10. sqlserver2008 job设定

    数据同期: update owk ),td.) ,owk.RecordEndTime),td.) from openrowset( 'SQLOLEDB ', '172.17.1.14'; 'read' ...