虚树入门题;

#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]消耗战的更多相关文章

  1. [BZOJ2286][SDOI2011]消耗战(虚树DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4998  Solved: 1867[Submit][Statu ...

  2. [BZOJ2286][Sdoi2011]消耗战(虚树上DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6457  Solved: 2533[Submit][Statu ...

  3. bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战

    http://www.lydsy.com/JudgeOnline/problem.php?id=2286 虚树上树形DP #include<cmath> #include<cstdi ...

  4. BZOJ2286 [Sdoi2011]消耗战 【虚树 + 树形Dp】

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 4261  Solved: 1552 [Submit][Sta ...

  5. BZOJ2286 [Sdoi2011]消耗战 和 BZOJ3611 [Heoi2014]大工程

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6371  Solved: 2496[Submit][Statu ...

  6. [Bzoj2286][Sdoi2011]消耗战(虚树模板题附讲解)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4896  Solved: 1824[Submit][Statu ...

  7. 【学习笔记】虚树复习记(BZOJ2286 SDOI2011 消耗战)

    想写战略游戏却想不起来虚树T^T 所以就有了这篇复习记QwQ ——简介!—— 我们在处理树上问题的时候,dfs是一个常用手段,但是我们发现,如果一棵树上只有一部分关键点,每次dfs需要访问好多不是关键 ...

  8. [bzoj2286][Sdoi2011]消耗战(虚树上的DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2286 分析:对于普通的树形dp:f[x]=min(∑f[son],m[x]),其中f[ ...

  9. BZOJ2286: [Sdoi2011]消耗战

    建出虚树dp. 把询问点按dfs序排序,用一个以dfs序为关键字的单调栈(以深度为关键字也是一样的),每次将一个询问点与栈顶的点的lca入栈,再将这个询问点入栈,在这个过程中建出一棵树就是虚树.具体看 ...

  10. [bzoj2286] [Sdoi2011消耗战

    还是虚树恩..模板都能打挂QAQ 先在原树上预处理出mndis[i],表示根节点到节点i 路径上边权的最小值(就是断开i与根的联系的最小花费) 建完虚树在虚树上跑树形DP..f[i]表示断开  i 所 ...

随机推荐

  1. nrm+nvm

    一.nvm的安装和使用   nvm全称Node Version Manager是 Nodejs 版本管理器,它让我们能方便的对 Nodejs 的版 本进行切换. nvm 的官方版本只支持 Linux ...

  2. Codeforces Gym - 101147G The Galactic Olympics

    Discription Altanie is a very large and strange country in Mars. People of Mars ages a lot. Some of ...

  3. spring-mvc 的一些使用技巧(转)

    APP 服务端的 Token 验证 通过拦截器对使用了@Authorization注解的方法进行请求拦截,从 http header 中取出 token 信息,验证其是否合法.非法直接返回 401 错 ...

  4. FireDAC 出现Variable length column[*] overflow. Value length - [80], column maximum length

    FireDAC 出现Variable length column[*] overflow. Value length - [80], column maximum length FireDAC的 TF ...

  5. 移除array中重复的item

    //move the repeated item            NSInteger index = [orignalArray count] - 1;            for (id o ...

  6. ios7.1后setting中没有开启相机服务应用程序相机预览黑屏问题

    if ( [[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){                               ...

  7. Geometry Shader 【转】

    Geometry shader – receives as its input complete primitives as a collection of vertices, and these i ...

  8. C#中如何让ListView控件点击选中整行

    将Listview控件的FullRowSelect属性置为True,当然Listview的View属性应该是Details. 2017年6月25日17:15:55

  9. Git以及github的使用方法(五),暂存区和工作区

    Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...

  10. C 标准库 - <math.h>

    C 标准库 - <math.h> 简介 math.h 头文件定义了各种数学函数和一个宏.在这个库中所有可用的功能都带有一个 double 类型的参数,且都返回 double类型的结果. 库 ...