【bzoj2286】[Sdoi2011]消耗战
虚树入门题;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
#include<cmath>
#include<iostream>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define up(i,j,n) for(int i=(j);i<=(n);i++)
#define FILE "dealing"
int read(){
int f=1,x=0,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return f*x;
}
const int maxn=505000,inf=1000000000;
int n,m,lim,k;
struct node{int y,next,v;}e[maxn],E[maxn];
LL linkk[maxn],len=0,linker[maxn],pre[maxn],vis[maxn],Len,d[maxn][31],fa[maxn][31],dfs_clock,dep[maxn],q[maxn],top=0,f[maxn];
pii a[maxn];
void insert(int x,int y,int v){e[++len].y=y;e[len].next=linkk[x];linkk[x]=len;e[len].v=v;}
void insert2(int x,int y,int v){E[++Len].y=y;E[Len].next=linker[x];linker[x]=Len;E[Len].v=v;}
void dfs(int x){
pre[x]=++dfs_clock;
for(int i=linker[x];i;i=E[i].next){
if(E[i].y==fa[x][0])continue;
fa[E[i].y][0]=x;
d[E[i].y][0]=E[i].v;
dep[E[i].y]=dep[x]+1;
dfs(E[i].y);
}
}
int lca(int x,int y){
if(dep[x]>dep[y])swap(x,y);
for(int i=lim;i>=0;i--)if(dep[y]-dep[x]>=(1<<i))y=fa[y][i];
if(x==y)return x;
for(int i=lim;i>=0;i--)if(fa[y][i]!=fa[x][i])x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
LL fmin(LL x,LL y){
if(dep[x]>dep[y])swap(x,y);
LL Min=inf;
for(int i=lim;i>=0;i--)if(dep[y]-dep[x]>=(1<<i))Min=min((LL)Min,d[y][i]),y=fa[y][i];
return Min;
}
void dp(int x,int fa,int w){
if(vis[x]&&x!=1){f[x]=w;return;}
else {
f[x]=0;
for(int i=linkk[x];i;i=e[i].next){
if(e[i].y==fa)continue;
dp(e[i].y,x,e[i].v);
f[x]+=f[e[i].y];
}
if(x!=1)f[x]=min(f[x],(LL)w);
}
}
void clear(int x,int fa){
for(int i=linkk[x];i;i=e[i].next){
if(e[i].y==fa)continue;
clear(e[i].y,x);
}
f[x]=0;
linkk[x]=0;
}
int main(){
//freopen("dealing.in","r",stdin);
//freopen("dealing.out","w",stdout);
n=read();lim=(int)log2(n*1.0+1)+1;
up(i,1,n-1){
int x=read(),y=read(),v=read();
insert2(x,y,v);insert2(y,x,v);
}
dfs(1);
up(j,1,lim)up(i,1,n)fa[i][j]=fa[fa[i][j-1]][j-1],d[i][j]=min(d[i][j-1],d[fa[i][j-1]][j-1]);
m=read();
while(m--){
k=read()+1;a[k].second=1;
up(i,1,k-1)a[i].second=read();
up(i,1,k)a[i].first=pre[a[i].second],vis[a[i].second]=1;vis[1]=0;
sort(a+1,a+k+1);
up(i,1,k){
int x=a[i].second;
if(!top){q[++top]=x;continue;}
int Lca=lca(x,q[top]);
if(Lca==q[top]){q[++top]=x;continue;}
while(dep[q[top-1]]>dep[Lca]){
LL v=fmin(q[top],q[top-1]);
insert(q[top],q[top-1],v);insert(q[top-1],q[top],v);
top--;
}
LL v=fmin(Lca,q[top]);
insert(Lca,q[top],v);insert(q[top],Lca,v);
top--;
if(q[top]!=Lca)q[++top]=Lca;q[++top]=x;
}
while(top!=1){
LL v=fmin(q[top],q[top-1]);
insert(q[top],q[top-1],v);
insert(q[top-1],q[top],v);
top--;
}
dp(1,0,0);top=0;
printf("%lld\n",f[1]);
clear(1,0);
up(i,1,k)vis[a[i].second]=0;len=0;
}
return 0;
}
【bzoj2286】[Sdoi2011]消耗战的更多相关文章
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- [BZOJ2286][Sdoi2011]消耗战(虚树上DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6457 Solved: 2533[Submit][Statu ...
- bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战
http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...
- BZOJ2286 [Sdoi2011]消耗战 【虚树 + 树形Dp】
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 4261 Solved: 1552 [Submit][Sta ...
- BZOJ2286 [Sdoi2011]消耗战 和 BZOJ3611 [Heoi2014]大工程
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6371 Solved: 2496[Submit][Statu ...
- [Bzoj2286][Sdoi2011]消耗战(虚树模板题附讲解)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4896 Solved: 1824[Submit][Statu ...
- 【学习笔记】虚树复习记(BZOJ2286 SDOI2011 消耗战)
想写战略游戏却想不起来虚树T^T 所以就有了这篇复习记QwQ ——简介!—— 我们在处理树上问题的时候,dfs是一个常用手段,但是我们发现,如果一棵树上只有一部分关键点,每次dfs需要访问好多不是关键 ...
- [bzoj2286][Sdoi2011]消耗战(虚树上的DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2286 分析:对于普通的树形dp:f[x]=min(∑f[son],m[x]),其中f[ ...
- BZOJ2286: [Sdoi2011]消耗战
建出虚树dp. 把询问点按dfs序排序,用一个以dfs序为关键字的单调栈(以深度为关键字也是一样的),每次将一个询问点与栈顶的点的lca入栈,再将这个询问点入栈,在这个过程中建出一棵树就是虚树.具体看 ...
- [bzoj2286] [Sdoi2011消耗战
还是虚树恩..模板都能打挂QAQ 先在原树上预处理出mndis[i],表示根节点到节点i 路径上边权的最小值(就是断开i与根的联系的最小花费) 建完虚树在虚树上跑树形DP..f[i]表示断开 i 所 ...
随机推荐
- Codeforces Gym101502 H.Eyad and Math-换底公式
H. Eyad and Math time limit per test 2.0 s memory limit per test 256 MB input standard input outpu ...
- OceanBase分区表有什么不同?
概述 分区表是ORACLE从8.0开始引入的功能,也是第一个支持物理分区的数据库,随后其他数据库纷纷跟进.分区表是一种“分而治之”的思想,通过将大表.索引分成可以独立管理的.小的片段(Segment) ...
- redis基本类型和操作
基本类型:string hash list set sorted set 添加String 类型(最基本的key,value形式) set str1 s1 获取value get str1 添加has ...
- Light oj 1085 - All Possible Increasing Subsequences (简单dp + 离散化 + BIT)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1085 题意: 问你有多少个上升子序列. 思路: dp[i]表示以第i个数结尾的 ...
- 转 Windows串口过滤驱动程序的开发
在Windows系统上与安全软件相关的驱动开发过程中,“过滤(filter)”是极其重要的一个概念.过滤是在不影响上层和下层接口的情况下,在Windows系统内核中加入新的层,从而不需要修改上层的软件 ...
- Android-->状态栏高度,导航栏高度,Window高度,DecorView高度,heightPixels
1:DecorView的高度 DecorView的高度代表的是: 整个装饰窗口的高度, 这个高度包括:状态烂的高度和导航栏的高度.(状态栏和导航栏通常叫做装饰窗口, 而ActionBar不属于装饰窗口 ...
- .NET组件编程
链接 http://www.cnblogs.com/mapserver/archive/2006/03/06/343632.html
- 简单理解 ES7 Decorator(装饰器)
如何使用ES7 Decorator给你的游戏人物开挂? // 预告: 本文有点小难度,对js不太熟的人可能比较懵逼 // 本文的目的是让你们知其然 // ======================= ...
- POJ 1141 Brackets Sequence (区间DP)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- Android SQLite性能分析
作为Android预置的数据库模块,对SQLite的深入理解是很有必要的,能够从中找到一些优化的方向. 这里对SQLite的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...