bzoj2286 (sdoi2011)消耗战(虚树)
[Sdoi2011]消耗战
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 4052 Solved: 1463
[Submit][Status][Discuss]
Description
Input
第一行一个整数n,代表岛屿数量。
接下来n-1行,每行三个整数u,v,w,代表u号岛屿和v号岛屿由一条代价为c的桥梁直接相连,保证1<=u,v<=n且1<=c<=100000。
第n+1行,一个整数m,代表敌方机器能使用的次数。
接下来m行,每行一个整数ki,代表第i次后,有ki个岛屿资源丰富,接下来k个整数h1,h2,…hk,表示资源丰富岛屿的编号。
Output
输出有m行,分别代表每次任务的最小代价。
Sample Input
1 5 13
1 9 6
2 1 19
2 4 8
2 3 91
5 6 8
7 5 4
7 8 31
10 7 9
3
2 10 6
4 5 7 8 3
3 9 4 6
Sample Output
32
22
HINT
对于100%的数据,2<=n<=250000,m>=1,sigma(ki)<=500000,1<=ki<=n-1
Source

这是hzw的题解,难以理解,十分正常。
我还觉得这篇题解写的不错
那么就需要用到虚树的技巧了,虚树就是通过维护一个单调栈把树的关键点和它们之间的lca按照dfs序遍历一遍,遍历的过程中通过单调栈的调整来理清树的父亲和儿子之间的关系。
首先,对树节点进行dfs。在期间对节点进行标号dfn。
然后,维护一个单调栈。这个单调栈的节点都在一条链上。
对于栈顶元素 p,栈次顶元素 q, 即将插入节点x 有如下关系:
1.lca是p.此时dfn(x)>dfn(p)=dfn(lca)
这说明 x在p的下面,直接把x入栈即可
2.p和x分立在lca的两棵子树下.此时 dfn(x)>dfn(p)>dfn(ilca)
这时候就有三种讨论了
针对这道题的连边就是树型dp处理
(1)如果dfn(q)>dfn(lca),可以直接连边q->p,然后退一次栈.
(2)如果dfn(q)=dfn(lca),说明q=lca,直接连边lca->p,把p退栈,此时子树已经构建完毕.
(3)如果dfn(q)<dfn(lca),说明lca被p与q夹在中间,此时连边lca->q,把p退栈,再把lca压入栈.此时子树构建完毕.
- #include<iostream>
- #include<set>
- #include<map>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<ctime>
- #include<vector>
- #include<queue>
- #include<algorithm>
- #include<cmath>
- #include<bitset>
- #include<stack>
- #define inf 1e60
- #define pa pair<int,int>
- #define ll long long
- using namespace std;
- int read()
- {
- int x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int bin[];
- int n,m,cnt,ind,top;
- int last[],last2[],fa[][];
- ll mn[],f[];
- int h[],mark[],deep[];
- int st[];
- struct edge{
- int to,next,v;
- }e[],ed[];
- void insert(int u,int v,int w)//普通增边吧
- {
- e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w;
- e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;e[cnt].v=w;
- }
- void insert2(int u,int v)//这是虚树增边
- {
- if(u==v)return;
- ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;
- }
- bool cmp(int a,int b)
- {
- return mark[a]<mark[b];
- }
- void pre(int x)//预处理深度,以及父亲
- {
- mark[x]=++ind;
- for(int i=;bin[i]<=deep[x];i++)
- fa[x][i]=fa[fa[x][i-]][i-];
- for(int i=last[x];i;i=e[i].next)
- if(e[i].to!=fa[x][])
- {
- mn[e[i].to]=min(mn[x],(ll)e[i].v);//到路径的最小值。
- deep[e[i].to]=deep[x]+;
- fa[e[i].to][]=x;
- pre(e[i].to);
- }
- }
- int lca(int x,int y)//倍增求lca
- {
- if(deep[x]<deep[y])swap(x,y);
- int t=deep[x]-deep[y];
- for(int i=;bin[i]<=t;i++)
- if(t&bin[i])x=fa[x][i];
- for(int i=;i>=;i--)
- if(fa[x][i]!=fa[y][i])
- x=fa[x][i],y=fa[y][i];
- if(x==y)return x;
- return fa[x][];
- }
- void dp(int x)
- {
- f[x]=mn[x];
- ll tmp=;
- for(int i=last2[x];i;i=ed[i].next)
- {
- dp(ed[i].to);
- tmp+=f[ed[i].to];
- }
- last2[x]=;
- if(tmp==)f[x]=mn[x];
- else if(tmp<=f[x])f[x]=tmp;
- }
- void solve()
- {
- cnt=;
- int K=read();
- for(int i=;i<=K;i++)
- h[i]=read();
- sort(h+,h+K+,cmp);//输入是无序的
- int tot=;
- h[++tot]=h[];
- for(int i=;i<=K;i++)
- if(lca(h[tot],h[i])!=h[tot]) h[++tot]=h[i];
- st[++top]=;//根节点是总根
- for(int i=;i<=tot;i++)
- {
- int now=h[i],f=lca(now,st[top]);
- while()
- {
- if(deep[f]>=deep[st[top-]])
- {
- insert2(f,st[top--]);
- if(st[top]!=f)st[++top]=f;
- break;
- }
- insert2(st[top-],st[top]);top--;
- }
- if(st[top]!=now)st[++top]=now;
- }
- while(--top)insert2(st[top],st[top+]);
- dp();
- printf("%lld\n",f[]);
- }
- int main()
- {
- bin[]=;for(int i=;i<;i++)bin[i]=bin[i-]<<;
- n=read();
- for(int i=;i<n;i++)
- {
- int u=read(),v=read(),w=read();
- insert(u,v,w);
- }
- mn[]=inf;pre();
- m=read();
- for(int i=;i<=m;i++)
- solve();
- return ;
- }
重复判断上述过程
bzoj2286 (sdoi2011)消耗战(虚树)的更多相关文章
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- bzoj2286: [Sdoi2011]消耗战 虚树
在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知在其他k个 ...
- BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5246 Solved: 1978[Submit][Status][Discuss] Descript ...
- 【BZOJ2286】[Sdoi2011]消耗战 虚树
[BZOJ2286][Sdoi2011]消耗战 Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的 ...
- 【BZOJ-2286】消耗战 虚树 + 树形DP
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2120 Solved: 752[Submit][Status] ...
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
- [SDOI2011]消耗战(虚树+树形动规)
虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...
- bzoj 2286(洛谷 2495) [Sdoi2011]消耗战——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2286 https://www.luogu.org/problemnew/show/P2495 ...
随机推荐
- SQL中的动态语句执行--exec(@sqlstr)
begin drop table #tmptable declare @money ut_money set @money=1.2345 create table #tmptable ( je ut_ ...
- MFC U盘检测
WM_DEVICECHANGE消息 查阅MSDN得知: The framework calls this member function to notify an application or dev ...
- mongodb主从配置信息查看与确认
在local库中不仅有主从日志 oplog集合,还有一个集合用于记录主从配置信息 system.replset: > use local > show collections > d ...
- vijos 1190 繁忙的都市
描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路 ...
- UVA 10735 Euler Circuit (最大流)
题意:求混合图的欧拉路径. 一句话总结:网络流,最主要在于建图,此题是将出度则是和流量联系在了一起,用最大流来调整边的指向. 分析: 这题的困难之处在于无向边只能用一次,相当于一个方向未定的有向边. ...
- 转向ARC的说明
转自hherima的博客原文:Transitioning to ARC Release Notes(苹果官方文档) ARC是一个编译器特征,它提供了对OC对象自动管理内存.ARC让开发者专注于感兴趣的 ...
- Django ORM 查询操作
queryset中支持链式操作 book=Book.objects.all().order_by('-nid').first() 只要返回的是queryset对象就可以调用其他的方法,直到返回的是对象 ...
- pytest生成测试报告
生成JunitXML格式的测试报告 --junitxml=report\h.xml 生成result log 格式的测试报告 --resultlog=report\h.log 生成htm ...
- c++中的结构体:声明 定义 初始化
什么是结构体? 之前的学习中我们知道了数组是一个容器,而且是存放固定大小数据的容器,而且存放的元素的数据类型必须要一致. 比如数据库中有这样的一条记录学号 性别 年龄 成绩 地址应该怎样存放 结构体: ...
- 第六次作业 :使用Excel制作成绩单