虚树第一题

对于每次询问的点建立一棵虚树,然后在树上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. ajax的适用场景

    1.适用:基本所有的网站都有涉及到. 2.典型使用场景: 动态加载数据,按照需要取数据 改善用户体验 电子商务应用 访问第三方服务 数据局部刷新

  2. linux以下C 利用openssl的AES库加密,解密

    OpenSSL提供了AES加解密算法的API const char *AES_options(void); AES算法状态,是所有支持或者是部分支持. 返回值:"aes(full)" ...

  3. 如何停止和扭转UIView的动画

    本文转载至  http://codego.net/576089/ 我有它收缩时碰到切换按钮UIView的动画跳和它扩展恢复到原来的大小当再次接触到按钮.密封式前大灯一切都工作得很好.问题是,动画师注意 ...

  4. TP分页

    ①在Home下设置Publics文件夹或在thinkPHP下library的vender 把page.class.php 考贝进入 ②通过new 实例化方式调用 $page=new \Home\Pub ...

  5. EasyDSS流媒体服务器软件(支持RTMP/HLS/HTTP-FLV/视频点播/视频直播)-正式环境安装部署攻略

    EasyDSS流媒体服务器软件,提供一站式的转码.点播.直播.时移回放服务,极大地简化了开发和集成的工作. 其中,点播功能主要包含:上传.转码.分发.直播功能,主要包含:直播.录像, 直播支持RTMP ...

  6. 160817、Java数据类型以及变量的定义

    Java 是一种强类型的语言,声明变量时必须指明数据类型.变量(variable)的值占据一定的内存空间.不同类型的变量占据不同的大小. Java中共有8种基本数据类型,包括4 种整型.2 种浮点型. ...

  7. mysql 5.7.18版本 sql_mode 问题

    only_full_group_by 模式的,但开启这个模式后,原先的 group by 语句就报错,然后又把它移除了. only_full_group_by ,感觉,group by 将变成和 di ...

  8. What are DESC and ASC Keywords?

    What are DESC and ASC Keywords? ASC is the short form for ascending DESC is the short form for desce ...

  9. matlab学习笔记之五种常见的图形绘制功能

    分类: 离散数据图形绘制 函数图形绘制 网格图形绘制 曲面图形绘制 特殊图形绘制 本文重点介绍matlab五种图形绘制方法的后三种. 一.网格图形绘制 以绘制函数z=f(x,y)三维网格图为例,下面为 ...

  10. 2、Python request、BeautifulSoup(download mm_pic)

    import requests from bs4 import BeautifulSoup import os class DownLoadImg(object): def __init__(self ...