Luogu-2495 [SDOI2011]消耗战
虚树第一题
对于每次询问的点建立一棵虚树,然后在树上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]消耗战的更多相关文章
- bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286 https://www.luogu.org/problemnew/show/P2495 ...
- luogu P2495 [SDOI2011]消耗战 |虚树+LCA+dp
题目描述 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知 ...
- Luogu P2495 [SDOI2011]消耗战
题目 我们可以很快的想到一个单次\(O(n)\)的dp. 然后我们注意到这个dp有很多无用的操作,比如一条没有关键点的链可以直接去掉. 所以我们可以尝试一次dp中只管那些有用的点. 题目给的关键点显然 ...
- BZOJ 2286: [Sdoi2011]消耗战
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2082 Solved: 736[Submit][Status] ...
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战
http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...
- [Luogu 2486] SDOI2011 染色
[Luogu 2486] SDOI2011 染色 树剖水题,线段树维护. 详细题解不写了. 我只想说我写的线段树又变漂亮了qwq #include <algorithm> #include ...
- 【LG2495】[SDOI2011]消耗战
[LG2495][SDOI2011]消耗战 题面 洛谷 题解 参考博客 题意 给你\(n\)个点的一棵树 \(m\)个询问,每个询问给出\(k\)个点 求将这\(k\)个点与\(1\)号点断掉的最小代 ...
- AC日记——[SDOI2011]消耗战 洛谷 P2495
[SDOI2011]消耗战 思路: 建虚树走树形dp: 代码: #include <bits/stdc++.h> using namespace std; #define INF 1e17 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
随机推荐
- [Docker]学习笔记--搭建gitlab
Gitlab 是一个用于仓库管理系统的开源项目.使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 详细介绍可以参照官网,https://about.gitlab.com/ 今天主要是通过 ...
- 《从零开始学Swift》学习笔记(Day 51)——扩展构造函数
原创文章,欢迎转载.转载请注明:关东升的博客 扩展类型的时候,也可以添加新的构造函数.值类型与引用类型扩展有所区别.值类型包括了除类以外的其他类型,主要是枚举类型和结构体类型. 值类型扩展构造函数 扩 ...
- after
.pay-type_select-after, .amount-item_select-after { padding: 0; border: @wx-width-one_unit solid @co ...
- The C Programming Language Second Edition
%12d at least #include <stdio.h> main() { ,sum=,w=; ; ; w<=end; w++ ) { sum+=w; // for(wb= ...
- String、StringBuffer、StringBuiler区别
1.String与StringBuiler的相同点都是线程不安全的.StringBuffer是线程安全的. 2.String长度不可变,StringBuiler长度可变.当String 使用(+)连接 ...
- Echarts-雷达图
// 显示能力雷达图 $(".company .grade").hover(function () { $(".powerChart").show(); var ...
- js验证表单大全3
2 >表单提交验证类 2.1 表单项不能为空 <scriptlanguage="javascript"> <!-- function CheckForm( ...
- Chrome 正在受到自动化软件控制 – 解决办法
1.在这个文件下修改一行代码就可以了,文件路径E:\Miniconda3\envs\env3\Lib\site-packages\selenium\webdriver\chrome\options.p ...
- Pycharm中SQL语句提示SQL Dialect is Not Configured
解决办法: 在File---->Setting--->Languages & Frameworks--->SQL Dialects中,选择对应的数据库,如MySQL,之后点击 ...
- 001infor record 计划时间取值增强-20150622
ZMD_MRP_PARAMETERS 3000公司下工厂跑MRP时,如果为外购则通过外挂表取infor record计划交期. METHOD if_ex_md_mrp_parameters~adjus ...