虚树第一题

对于每次询问的点建立一棵虚树,然后在树上DP,一个点的答案就是这个点的父边切断的代价与所有儿子切断的代价去最小值,当然如果这个节点是资源点则必须切父边

注意在虚树上一条边的代价应该是中间所有边代价的最小值,在这道题里可以用到根节点边的最小值

建虚树的时候可以不去建那些在其他资源点下面的资源点,他们不会对答案造成影响,并且这样的话资源点都是叶节点,就不用给资源点打标记了23333

注意1点不能断,特判一下就好了。

#include<map>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=3e5+100,maxm=5e5+100;
struct node{
int fa,dep,top,son,siz;
}tre[maxn];
int head[maxn],nex[maxm],v[maxm],w[maxm],num=1,fee[maxn];
int n,t,tim,m,p[maxn],dfn[maxn],top,st[maxn],a,b,c;
vector<int>e[maxn];
void dfs1(int x,int fa,int dep){
tre[x].dep=dep;
tre[x].fa=fa;
tre[x].siz=1;
dfn[x]=++tim;
for(int i=head[x];i;i=nex[i])
if(v[i]!=fa){
fee[v[i]]=min(fee[x],w[i]);
dfs1(v[i],x,dep+1);
if(tre[v[i]].siz>tre[tre[x].son].siz)
tre[x].son=v[i];
tre[x].siz+=tre[v[i]].siz;
}
}
void dfs2(int x,int fa,int top){
tre[x].top=top;
if(tre[x].son) dfs2(tre[x].son,x,top);
for(int i=head[x];i;i=nex[i])
if(v[i]!=fa&&v[i]!=tre[x].son)
dfs2(v[i],x,v[i]);
}
int Lca(int x,int y){
int fx=tre[x].top,fy=tre[y].top;
while(fx!=fy){
if(tre[fx].dep<tre[fy].dep) swap(x,y),swap(fx,fy);
x=tre[fx].fa;
fx=tre[x].top;
}
return tre[x].dep>tre[y].dep?y:x;
}
void add(int x,int y,int z){
v[++num]=y;
w[num]=z;
nex[num]=head[x];
head[x]=num;
v[++num]=x;
w[num]=z;
nex[num]=head[y];
head[y]=num;
}
bool cmp(int x,int y){
return dfn[x]<dfn[y];
}
void insert(int x){
if(top==1){
st[++top]=x;
return;
}
int lca=Lca(st[top],x);
if(lca==st[top]) return; //DP方便
while(top>1&&dfn[st[top-1]]>=dfn[lca])
e[st[top-1]].push_back(st[top]),top--;
if(lca!=st[top]) e[lca].push_back(st[top]),st[top]=lca;
st[++top]=x;
}
ll dp(int x){
if(e[x].size()==0) return 1ll*fee[x];
ll tot=0;
for(int i=0;i<e[x].size();i++)
tot+=dp(e[x][i]);
e[x].clear();
if(x==1) return tot;
else return min(tot,1ll*fee[x]);
}
int main(){
scanf("%d",&n);
for(int i=1;i<n;i++)
scanf("%d%d%d",&a,&b,&c),add(a,b,c);
dfs1(1,1,1);
dfs2(1,1,1);
scanf("%d",&t);
while(t--){
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&p[i]);
sort(p+1,p+m+1,cmp);
st[top=1]=1;
for(int i=1;i<=m;i++) insert(p[i]);
while(top>1) e[st[top-1]].push_back(st[top]),top--;
printf("%lld\n",dp(1));
}
return 0;
}

Luogu-2495 [SDOI2011]消耗战的更多相关文章

  1. bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286 https://www.luogu.org/problemnew/show/P2495 ...

  2. luogu P2495 [SDOI2011]消耗战 |虚树+LCA+dp

    题目描述 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知 ...

  3. Luogu P2495 [SDOI2011]消耗战

    题目 我们可以很快的想到一个单次\(O(n)\)的dp. 然后我们注意到这个dp有很多无用的操作,比如一条没有关键点的链可以直接去掉. 所以我们可以尝试一次dp中只管那些有用的点. 题目给的关键点显然 ...

  4. BZOJ 2286: [Sdoi2011]消耗战

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

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

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

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

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

  7. [Luogu 2486] SDOI2011 染色

    [Luogu 2486] SDOI2011 染色 树剖水题,线段树维护. 详细题解不写了. 我只想说我写的线段树又变漂亮了qwq #include <algorithm> #include ...

  8. 【LG2495】[SDOI2011]消耗战

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

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

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

  10. [BZOJ2286][SDOI2011]消耗战(虚树DP)

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

随机推荐

  1. <转载> Jquery的性能优化-实用!

    我一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些.找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来 ========================= ...

  2. R-ArcGIS探秘(1)安装以及Sample执行

    在今年的全球用户大会上,Esri官方发布了R-ArcGIS的官方演示样例,在ArcMap和ArcGIS pro中,直接通过Toolbox能够调用R的分析工具包,将R的分析能力直接作用在ArcGIS上面 ...

  3. Partial Sum

    Partial Sum Accepted : 80   Submit : 353 Time Limit : 3000 MS   Memory Limit : 65536 KB  Partial Sum ...

  4. Asp.Net MVC anti-forgery token的问题:nameidentifier or identityprovider not present

    当使用ClaimsIdentity的时候,Asp.Net MVC在生成AntiForgeryToken的时候会默认使用User.Identity中两种ClaimsType的值:NameIdentifi ...

  5. nginx反向代理三台web

    1.首先我们需要在服务器中三个不同名字,并将他们赋值 2.切换到nginx—conf  把三台机器的nginx的配置文件分别命名为web1.conf.web2.conf.web3.conf vim的赋 ...

  6. 卷积神经网络(CNN)的训练及代码实现

    本文部分内容来自zouxy09的博客.谢谢.http://blog.csdn.net/zouxy09/article/details/9993371 以及斯坦福大学深度学习教程:http://ufld ...

  7. 022-Spring Boot 构建微服务实战

    一.概述 二. 2.1.微服务 将原来一个大的系统,拆分成小系统 每个小系统分别开发,测试,维护 2.2.调用方 服务提供方式:rest(http)[restTemplate,httpclient]. ...

  8. microsoft cl.exe 编译器

    cl.exe是visual stdio 内置的编译器,visual stdio包含各种功能,有些功能可能这辈子都用不到,体积庞大,如果是 开发比较大或者有图形的项目,vs是首选.更多情况时更喜欢使用文 ...

  9. FSR薄膜压力传感器使用教程

    FSR薄膜压力传感器教程 本店常用的外形有2种: 圆形: 长条形: 如果用单片机控制建议买带转换的,可以直接接单片机AD口或者数字IO去读取数值: 电压输出的AO接口是模拟量输出,可以接单片机的模拟口 ...

  10. EasyMock 常见异常

    1. java.lang.IllegalStateException: calling verify is not allowed in record state 含义:不允许在记录状态(record ...