BZOJ 2286 [Sdoi2011]消耗战 ——虚树
虚树第一题。
大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP
#include <map>
#include <ctime>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define ll long long
#define inf 1e17
#define maxn 500005 int n,m,k,a[maxn];
int h[maxn],to[maxn],ne[maxn],w[maxn],en=0;
int dep[maxn],st[maxn][21],_log[maxn],in[maxn],out[maxn],tot,ord[maxn];
ll mn[maxn];int sta[maxn],top=0,last[maxn],idx=0; void add(int a,int b,int c)
{to[en]=b;ne[en]=h[a];w[en]=c;h[a]=en++;} void dfs(int o,int fa)
{
in[o]=++tot;ord[tot]=o;
for (int i=h[o];i>=0;i=ne[i])
if (to[i]!=fa){
dep[to[i]]=dep[o]+1;
mn[to[i]]=min(mn[o],(ll)w[i]);
dfs(to[i],o);
ord[++tot]=o;
}
out[o]=tot;
} void initst()
{
F(i,2,maxn-1) _log[i]=_log[i>>1]+1;
F(i,1,20) F(j,1,tot-(1<<i)+1)
{
if (dep[st[j][i-1]]<dep[st[j+(1<<i-1)][i-1]]) st[j][i]=st[j][i-1];
else st[j][i]=st[j+(1<<i-1)][i-1];
}
} int lca(int l,int r)
{
if (l==r) return l;
l=in[l],r=in[r];
if (l>r) swap(l,r);
int tmp=_log[r-l+1];// printf("tmp is %d\n",tmp);
if (dep[st[l][tmp]]<dep[st[r-(1<<tmp)+1][tmp]]) return st[l][tmp];
else return st[r-(1<<tmp)+1][tmp];
} bool cmp(int a,int b)
{return in[a]<in[b];} int hd[maxn],tl[maxn],nxt[maxn],ed,se[maxn]; void add_edge(int a,int b)
{
if (last[a]!=idx) last[a]=idx,hd[a]=-1;
tl[ed]=b;
nxt[ed]=hd[a];
hd[a]=ed++;
} ll dfs2(int o,int fa)
{
ll ret=0;
for (int i=hd[o];i>=0;i=nxt[i])
if (tl[i]!=fa)
ret+=dfs2(tl[i],o);
if(se[o]==idx) return mn[o];
return min(ret,(ll)mn[o]);
} int main()
{
memset(h,-1,sizeof h);
scanf("%d",&n); mn[1]=inf;
F(i,1,n-1)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
dfs(1,0);
F(i,1,tot)st[i][0]=ord[i];initst();
scanf("%d",&m);
while (m--)
{
ed=0; idx++;
scanf("%d",&k);
F(i,1,k) scanf("%d",&a[i]),se[a[i]]=idx;
sort(a+1,a+k+1,cmp);
sta[top=1]=1;
F(i,1,k)
{
int l=in[sta[top]],r=in[a[i]],x=lca(sta[top],a[i]);
while (dep[sta[top]]>dep[x])
{
int tmp;
if (dep[x]>dep[sta[top-1]]) tmp=x;
else tmp=sta[top-1];
add_edge(sta[top],tmp);
add_edge(tmp,sta[top]);
top--;
}
if (x!=sta[top]) sta[++top]=x;
sta[++top]=a[i];
}
while (top>1) add_edge(sta[top],sta[top-1]),add_edge(sta[top-1],sta[top]),top--;
printf("%lld\n",dfs2(1,0));
}
}
BZOJ 2286 [Sdoi2011]消耗战 ——虚树的更多相关文章
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树
Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军 ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
- BZOJ 2286: [Sdoi2011]消耗战
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2082 Solved: 736[Submit][Status] ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...
随机推荐
- 洛谷 P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- UVA1001 Say Cheese (dijkstra)
如果没有洞,那么任意两点的最短距离就是直线距离,洞里是瞬间的,所以看成一个点就行了(其实点也可以当作半径为0的洞来处理),洞到洞的最短距离都是圆心距离减去半径.剩下的就是求单源最短路径,是完全图,用不 ...
- 讲课笔记3——浮动、margin失效的问题、默认样式重置
EO:搜索引擎优化,一般在网页里面只写一个h1标签,搜索引擎可以通过该h1标签里面的内容搜索你所写的网页(a标签和img标签最好写上title属性)标准写法: .logo { text-decorat ...
- CAD交互绘制批注(网页版)
js中实现代码说明: 动态拖放时的绘制事件: function DynWorldDrawComment( pCustomEntity, pWorldDraw, curPt) { // 得到绘制参数. ...
- please upgrade your plan to create a new private reposiory
请升级你的计划来创建一个新的私人仓库 提交仓库到github,要公开,除非买他们服务,所以把勾去掉就好了keep this code private
- javase(5)_面向对象
一.概述 1.面向对象是一种思想,让我们由执行者变成指挥者,执行者是面向过程,指挥者是面向对象.例如人开冰箱门,开冰箱门这个动作应该属于门而不是人,冰箱自己最清楚门应该怎么开,人只是调用了冰箱的这个动 ...
- touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
docker 运行后, 执行docker logs -f myjenkins时报错: touch: cannot touch ‘/var/jenkins_home/copy_reference_fil ...
- 《C/C++工程师综合练习卷》之小试牛刀
第一套练习之感受 刚刚注册了牛客网的账号,准备在此衡量一下水平,好好磨练一番.看到推荐练习<C/C++工程师综合练习卷>,oh,20道题,2个小时.由于木有经验,好一番紧张~ 结果用了20 ...
- ACM训练联盟周赛 Teemo's formula
Teemo has a formula and he want to calculate it quickly. The formula is . As the result may be very ...
- tomcat 修改默认端口8080 为 80端口
首先,找到你的安装目录,如图: 打开server.xml文件,找到8080,如图: 将 8080 改成你想要的端口,如 80 即可.改完后,记得要重启tomcat! 将端口改成 80 后,访问就不需 ...