BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边。
如果当前点需要删除,那么直接断不需要再管子树。
复杂度O(m*n)。
对于两个要删除的点 u,v 之间的链,若链上没有其它需要删的点,则只需保留链上的最小边权即可。
把有用的点按DFS序排序,依次构建出一棵虚树,可以在上面进行同样的DP。
代码在下面
这儿是虚树构建详细过程(这图有点。。图可以拖到后台打开):
参考:https://www.cnblogs.com/Michael-Li/p/8763242.html
这个算数据结构么。。
卡常技巧:
本题建虚树时,对于某要删除点的子树中的点,都可以忽略,即Insert时若LCA(sk[top],now)=sk[top],可直接忽略now的入栈。(因为刚开始栈顶一定是个要删的点)
这样就可以用表头是否为空来判断是否要删除该点了,不需要标记。
本题建虚树时不需要边权,直接用点权即可,权值为到根路径的最小边权。
还因为是棵树,那么连边时用链表即可,不需要用边表。(然并软。。还是用边表吧)
//17428kb 3264ms(Rank4 嗯...)
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 1000000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=250005,M=N<<1;
int n,m,K,A[N],Enum,H[N],nxt[M],to[M],len[M],val[N],dfn[N],Index,fa[N],tp[N],sz[N],son[N],dep[N],top,sk[N];
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline void AddEdge(int u,int v,int w){
to[++Enum]=v, nxt[Enum]=H[u], len[Enum]=w, H[u]=Enum;
to[++Enum]=u, nxt[Enum]=H[v], len[Enum]=w, H[v]=Enum;
}
inline void Add_direct(int u,int v){
to[++Enum]=v, nxt[Enum]=H[u], H[u]=Enum;
}
inline bool cmp_dfn(const int &a,const int &b){
return dfn[a]<dfn[b];
}
void DFS1(int x,int mn)
{
int mx=0; sz[x]=1, val[x]=mn;
for(int v,i=H[x]; i; i=nxt[i])
if((v=to[i])!=fa[x])
{
fa[v]=x, dep[v]=dep[x]+1, DFS1(v,std::min(mn,len[i])), sz[x]+=sz[v];
if(mx<sz[v]) mx=sz[v], son[x]=v;
}
}
void DFS2(int x,int _tp)
{
dfn[x]=++Index, tp[x]=_tp;
if(son[x])
{
DFS2(son[x],_tp);
for(int i=H[x]; i; i=nxt[i])
if(to[i]!=fa[x]&&to[i]!=son[x]) DFS2(to[i],to[i]);
}
}
int LCA(int u,int v)
{
while(tp[u]!=tp[v]) dep[tp[u]]>dep[tp[v]]?u=fa[tp[u]]:v=fa[tp[v]];
return dep[u]<dep[v]?u:v;
}
void Insert(int p)
{
if(top==1) {sk[++top]=p; return;}//if(sk[top]==1)
int lca=LCA(sk[top],p);
if(lca==sk[top]) return;
// int las=sk[top];
// while(lca!=las)
// {
// if(dfn[sk[--top]]<dfn[lca])
// {
// Add_direct(lca,las), sk[++top]=lca;
// break;
// }
// Add_direct(sk[top],las), las=sk[top];
// }
while(dfn[sk[top-1]]>=dfn[lca]) Add_direct(sk[top],sk[top--]);//参数调用顺序...还是写个las=sk[top]吧
if(lca!=sk[top] && dfn[sk[top-1]]<dfn[lca]) Add_direct(lca,sk[top]), sk[top]=lca;//++top, --top
sk[++top]=p;
}
LL DP(int x)
{
if(!H[x]) return val[x];
LL sum=0;
for(int i=H[x]; i; i=nxt[i]) sum+=DP(to[i]);
H[x]=0;
return std::min((LL)val[x],sum);
}
int main()
{
n=read();
for(int u,v,i=1; i<n; ++i) u=read(),v=read(),AddEdge(u,v,read());
DFS1(1,0x7fffffff), DFS2(1,1), Enum=0, memset(H,0,sizeof H);
m=read();
while(m--)
{
K=read();
for(int i=1; i<=K; ++i) A[i]=read();
std::sort(A+1,A+1+K,cmp_dfn);
sk[top=1]=1;
for(int i=1; i<=K; ++i) Insert(A[i]);
while(--top) Add_direct(sk[top],sk[top+1]);
LL res=0;
for(int i=H[1]; i; i=nxt[i]) res+=DP(to[i]);//对1要单独处理,要不应把val[1]设为很大的longlong
H[1]=0;
printf("%lld\n",res);
Enum=0; //for(int i=1; i<=K; ++i) H[A[i]]=0;
}
return 0;
}
BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)的更多相关文章
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- bzoj 2286: [Sdoi2011]消耗战 虚树+树dp
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...
- bzoj 2286 [Sdoi2011]消耗战 虚树+dp
题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树
Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军 ...
- BZOJ2286: [Sdoi2011]消耗战(虚树/树形DP)
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 5246 Solved: 1978[Submit][Status][Discuss] Descript ...
- BZOJ 2286 [Sdoi2011]消耗战 ——虚树
虚树第一题. 大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP #include <map> #include <ctime> #include <cmath&g ...
- 【BZOJ-2286】消耗战 虚树 + 树形DP
2286: [Sdoi2011消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2120 Solved: 752[Submit][Status] ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
- 【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP
[题意]给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价.n<=250000,Σki<=500000. [算法]虚树+DP [题解]考虑普通树上的dp,设f[x ...
随机推荐
- linux 配置免密码登陆
在使用scp命令传输的时候需要密码 配置免密码登陆 ssh-keygen -t rsa (四个回车) 执行命令完成后,会生成两个文件id_rsa(私钥).id-rsa.pub(公钥) 将公钥拷贝到要免 ...
- Floyd_Warshall算法
Floyd_Warshall算法主要用于求解所有节点对的最短路径,代码如下: #include<iostream> using namespace std; #define Inf 655 ...
- python类中的私有方法
假设有如下一个python类: class Foo(object): def __a(self): print "Bet you can't see me..." def bar( ...
- 安装window系统
安装服务器系统,进入windowpe后将iso中sources,bootmgr,和boot拷贝到C盘,执行bootsect.exe /nt60 c:,调试froad13的consle win8 改 ...
- linux下nc提交web报文问题
1.用wireshark截取访问百度首页拿到的请求数据包: GET /index.php?tn=newbdie_bd_dg&bar= HTTP/1.1 Host: www.baidu.com ...
- mac上Python安装和修改Python默认路径遇到的问题
此处例子是我使用homebrew安装了python3.6.1,建立一个符号链接,创建一个python3的命令,达到使用自己安装的python3的目的.此处不修改PATH,而是把需要添加的可执行文件或者 ...
- 一个文件系统过滤驱动的demo
因为没写过FSD过滤驱动,所以拿来练练手,没有什么技术含量.参考自Win内核安全与驱动开发. 先梳理一下大概的流程,就是怎么去绑定设备栈.怎么去过滤各种请求的. 首先肯定是要绑定设备栈的,来看下怎么绑 ...
- Struts – MappingDispatchAction Example
Struts MappingDispatchAction class is used to group similar functionality into a single action class ...
- spring-boot分环境打包为tar包
1.pom配置 <!-- 多环境打包 start --> <profiles> <!-- 开发环境配置 --> <profile> <id> ...
- hmm学习笔记(二)
学习hmm近一周的时间,做个总结. 参考 李航的<统计学习方法>第9章,第10章 本文包含: 1.hmm模型 2.前向后向算法 3.Baum-Welch算法 4.维特比算法 1.hmm模 ...