[Bzoj2286]消耗战(虚树+DP)
Description
Solution
在虚树上跑DP即可
Code
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#define ll long long
#define N 250010
using namespace std;
const ll Inf=1ll<<60;
struct info{int to,nex;ll w;}vir[N*2],e[N*2];
int n,m,tot,head[N],dfn[N],cnt;
int _log,f[N][20],dep[N];
int q[N],sta[N],top;
ll dis[N],dp[N];
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void Link(int u,int v,int w){
e[++tot].to=v;e[tot].w=w;e[tot].nex=head[u];head[u]=tot;
}
inline void Link_vir(int u,int v){
if(u==v) return;
vir[++tot].to=v;vir[tot].nex=head[u];head[u]=tot;
}
void dfs(int u, int fa){
dfn[u]=++cnt;
for (int j=1;j<=_log;++j) f[u][j]=f[f[u][j-1]][j-1];
for(int i=head[u];i;i=e[i].nex) {
int v=e[i].to;
if (v==fa) continue;
f[v][0]=u;
dis[v]=min(dis[u],e[i].w);
dep[v]=dep[u]+1;
dfs(v,u);
}
}
int LCA(int u,int v){
if(dep[u]>dep[v]) swap(u,v);
int d=dep[v]-dep[u];
for(int i=0;i<=_log;++i)
if(d&(1<<i)) v=f[v][i];
if(u==v) return v;
for(int i=_log;i>=0;--i)
if(f[u][i]!=f[v][i]){
u=f[u][i];
v=f[v][i];
}
return f[u][0];
}
void DP(int x){
ll tmp=0;dp[x]=dis[x];
for(int i=head[x];i;i=vir[i].nex){
int v=vir[i].to;
DP(v);
tmp+=dp[v];
}
head[x]=0;
if(!tmp) dp[x]=dis[x];
else if(tmp<dp[x]) dp[x]=tmp;
}
bool cmp(int a,int b){return dfn[a]<dfn[b];}
void solve(){
m=read();tot=0;
for(int i=1;i<=m;++i) q[i]=read();
sort(q+1,q+m+1,cmp);
cnt=1;
for(int i=2;i<=m;++i) if(LCA(q[i],q[cnt])!=q[cnt]) q[++cnt]=q[i];
sta[top=1]=1;
for(int i=1;i<=cnt;++i){
int grand=LCA(q[i],sta[top]);
while(1){
if(dep[sta[top-1]]<=dep[grand]){
Link_vir(grand,sta[top]); top--;
if(sta[top]!=grand) sta[++top]=grand;
break;
}
Link_vir(sta[top-1],sta[top]); top--;
}
if(sta[top]!=q[i]) sta[++top]=q[i];
}
top--;
while(top) Link_vir(sta[top],sta[top+1]),top--;
DP(1);
printf("%lld\n",dp[1]);
}
int main(){
n=read();_log=log(n)/log(2);
for(int i=1;i<n;++i){
int u=read(),v=read(),w=read();
Link(u,v,w);
Link(v,u,w);
}
dis[1]=Inf;dfs(1,0);
memset(head,0,sizeof(head));
int k=read();while(k--) solve();
return 0;
}
[Bzoj2286]消耗战(虚树+DP)的更多相关文章
- [SDOI2011][bzoj2286] 消耗战 [虚树+dp]
题面: 传送门 思路: 看到所有询问中的点数总和是十万级别的,就想到用虚树~\(≧▽≦)/~啦 首先,树形dp应该是很明显可以看出来的: 设dp[u]表示以u为根的子树(不包括u)中的宝藏岛全部切断的 ...
- [BZOJ2286][SDOI2011]消耗战(虚树DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4998 Solved: 1867[Submit][Statu ...
- 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 ...
- 洛谷P2495 [SDOI2011]消耗战(虚树dp)
P2495 [SDOI2011]消耗战 题目链接 题解: 虚树\(dp\)入门题吧.虚树的核心思想其实就是每次只保留关键点,因为关键点的dfs序的相对大小顺序和原来的树中结点dfs序的相对大小顺序都是 ...
- bzoj 3572世界树 虚树+dp
题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...
- [SDOI2011]消耗战(虚树+树形动规)
虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...
- [BZOJ5287][HNOI2018]毒瘤(虚树DP)
暴力枚举非树边取值做DP可得75. 注意到每次枚举出一个容斥状态的时候,都要做大量重复操作. 建立虚树,预处理出虚树上两点间的转移系数.也可动态DP解决. 树上倍增.动态DP.虚树DP似乎是这种问题的 ...
- BZOJ 3572 [HNOI2014]世界树 (虚树+DP)
题面:BZOJ传送门 洛谷传送门 题目大意:略 细节贼多的虚树$DP$ 先考虑只有一次询问的情况 一个节点$x$可能被它子树内的一个到x距离最小的特殊点管辖,还可能被管辖fa[x]的特殊点管辖 跑两次 ...
- 【BZOJ-2286】消耗战 虚树 + 树形DP
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2120 Solved: 752[Submit][Status] ...
随机推荐
- python面试题——爬虫相关
1.接触过几种爬虫模块 urllib.requests这两种爬虫模块. 2.robots协议是什么? 它就是一个防君子不防小人的协议,网站有一些数据不想被爬虫程序爬取,可以编写robots协议文件,明 ...
- mybatis VS hibernate
转自:http://blog.csdn.net/firejuly/article/details/81902 第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/ ...
- #include stdio.h(5)
#include <stdio.h> int main() { //1.数组的排序-冒泡排序 /* 1.规则:相邻的两个数据进行比较 2.如果有N个数据,需要选择N-1次参照物来比较, 因 ...
- 一步步理解typedef
1.如何用C语言实现一个函数,传递两个整形数,返回两个数的和? #include<stdio.h> int add(int a,int b) { return a+b; } void ma ...
- 【从业余项目中学习2】C# 实现调用Matlab函数(Visual Studio:2008, Matlab:R2009a)
最近正在给客户做的个人项目,要求实现C#与Matlab之间的调用,即C# winform界面收集用户输入的参数,将参数传递给Matlab的算法计算,Matlab函数返回的结果显示在winform界面上 ...
- (转)轻松解决 MyEclipse、Eclipse 编译时提示 @Override The method of type must override a superclass method 即 @Override 标注问题
刚才在把工程从其他地方导入到自己机子的 MyEclipse 下时,出现了 The method of type must override a superclass method ,提示的是实现类必须 ...
- API:Sign签名的执行流程
Sign签名存在目的:为了防止不法分子修改参数数据,进而攻击服务器,导致数据泄露或从中获得利益 例如:一个接口是用户把积分转帐给他的朋友,修改后,变为转帐到攻击者的帐户,这样,攻击者就能得到利益 ...
- NYOJ(325)+NYOJ(456),01背包
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=325 http://acm.nyist.net/JudgeOnline/problem. ...
- css3之Media Queries 媒体查询
一.初步了解 Media Queries是CSS3新增加的一个模块功能,其最大的特点就是通过css3来查询媒体,然后调用对应的样式. 了解Media Queries之前需要了解媒体类型以及媒体特性: ...
- printf、sprintf与fprintf 的用法区分
原文链接 1: fprintf()#include <stdio.h> int fprintf( FILE *stream, const char *format, ... );fprin ...