2018.09.25 bzoj2286: [Sdoi2011]消耗战(虚树+树形dp)
传送门
又一道虚树入门题。
这个dp更简单啊。
直接记录每个点到1的距离,简单转移就行了。
代码:
#include<bits/stdc++.h>
#define N 250005
#define ll long long
#define min(a,b) (a<b?a:b)
using namespace std;
inline ll read(){
ll ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
return ans;
}
int n,q,m,First[N],first[N],cnt=0,dfn_cnt=0,tot=0,top=0,stk[N],dfn[N],dep[N],st[N][21],a[N];
ll dis[N],f[N];
bool col[N];
struct edge{int v,next,w;}e[N<<1];
inline void add(int u,int v,int w){e[++cnt].v=v,e[cnt].w=w,e[cnt].next=First[u],First[u]=cnt;}
inline void Add(int u,int v){e[++cnt].v=v,e[cnt].next=first[u],first[u]=cnt;}
inline void dfs(int p){
dfn[p]=++dfn_cnt;
for(int i=First[p];i;i=e[i].next){
int v=e[i].v;
if(v==st[p][0])continue;
st[v][0]=p,dep[v]=dep[p]+1,dis[v]=min(dis[p],e[i].w),dfs(v);
}
}
inline int lca(int x,int y){
if(dep[x]<dep[y])x^=y,y^=x,x^=y;
for(int i=20;~i;--i)if(dep[x]-(1<<i)>=dep[y])x=st[x][i];
if(x==y)return x;
for(int i=20;~i;--i)if(st[x][i]!=st[y][i])x=st[x][i],y=st[y][i];
return st[x][0];
}
inline bool cmp(int x,int y){return dfn[x]<dfn[y];}
inline void dfs1(int p){
f[p]=dis[p];
ll sum=0;
for(int i=first[p];i;i=e[i].next){
int v=e[i].v;
dfs1(v),sum+=f[v];
}
if(sum&&!col[p])f[p]=min(f[p],sum);
first[p]=0;
}
int main(){
n=read(),dis[1]=1e18;
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read();
add(u,v,w),add(v,u,w);
}
dfs(1),cnt=0;
for(int j=1;j<=20;++j)for(int i=1;i<=n;++i)st[i][j]=st[st[i][j-1]][j-1];
q=read();
while(q--){
m=read(),tot=cnt=top=0;
for(int i=1;i<=m;++i)a[i]=read(),col[a[i]]=true;
sort(a+1,a+m+1,cmp);
int t;
for(int i=1;i<=m;++i){
if(!top){stk[++top]=a[i];continue;}
t=lca(a[i],stk[top]);
while(dep[stk[top]]>dep[t]){
if(dep[stk[top-1]]<=dep[t]){
Add(t,stk[top]),--top;
if(stk[top]!=t)stk[++top]=t;
break;
}
Add(stk[top-1],stk[top]),--top;
}
stk[++top]=a[i];
}
while(top>1)Add(stk[top-1],stk[top]),--top;
dfs1(stk[1]),printf("%lld\n",f[stk[1]]);
for(int i=1;i<=m;++i)col[a[i]]=0;
}
return 0;
}
2018.09.25 bzoj2286: [Sdoi2011]消耗战(虚树+树形dp)的更多相关文章
- BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5246 Solved: 1978[Submit][Status][Discuss] Descript ...
- 【BZOJ-2286】消耗战 虚树 + 树形DP
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2120 Solved: 752[Submit][Status] ...
- 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). 对于两个要删除的 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- 【BZOJ2286】【SDOI2011】消耗战 [虚树][树形DP]
消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一场战争中,战场由n个岛屿和n-1 ...
- bzoj2286: [Sdoi2011]消耗战 虚树
在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军胜利在望.已知在其他k个 ...
- [SDOI2011]消耗战(虚树+树形动规)
虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
随机推荐
- nginx 域名绑定
单个域名 upstream web{ server ;//这里绑定你要访问的服务器地址 keepalive ; } server { listen ; server_name www.xxxx.con ...
- SQL 2012 分页取数据
,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...
- 机器学习入门-数据过采样(上采样)1. SMOTE
from imblearn.over_sampling import SMOTE # 导入 overstamp = SMOTE(random_state=0) # 对训练集的数据进行上采样,测试集的 ...
- python限制函数执行时间
from:https://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call-in-py ...
- Zuul超时问题,微服务响应超时,zuul进行熔断
天碰到了微服务响应超时问题,而且超时时间特别短,2秒就超时,zuul就走熔断了. 我采用zuul作为网关,根据不同的访问路径进行微服务的路由,譬如有个服务是user,我访问user服务的某个接口时,该 ...
- ssh 免密码登录,以及 本地和远端用户名不一致 问题
ssh 远程登录 ssh -l u1 u1@192.168.0.7 ssh u1@192.168.0.7 每次远程都要输入 用户名,密码 比较麻烦.所以比较好的是免密码登录 1.安装ssh服务器 su ...
- ubuntu 16.04 完整安装 phantomjs
摘自 stackoverflow sudo apt-get install nodejssudo apt-get install nodejs-legacysudo apt-get install n ...
- linux一些基本常识(四)
tail -f时时监控 一开启内存最小位u原则,尽量优化代码 grep -v "" /etc/passwd 这样行不行 怎么清除last nice调整进程运行级别 pkill是匹配 ...
- poj3616(LIS简单变式)
题目链接:http://poj.org/problem?id=3616 思路: 我的第一反应是背包,因为每个interval要么选择要么不选,后来发现状态方程很难写出来.后来想一想发现就是LIS的简单 ...
- ccf认证模拟题之三---最大的矩形
问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...