BZOJ 3611 [Heoi2014]大工程 ——虚树
虚树第二题。。。。
同BZOJ2286
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define ll long long
#define mp make_pair
#define inf 0x3f3f3f3f
#define maxn 2000005
int h[maxn],to[maxn],ne[maxn],en=0;
int n,dep[maxn],st[maxn][22],in[maxn],out[maxn],ord[maxn],tot;
int _log[maxn],m,sta[maxn],top=0,se[maxn],idx,k,a[maxn]; void add(int a,int b)
{to[en]=b;ne[en]=h[a];h[a]=en++;} void dfs1(int o,int fa)
{
in[o]=++tot;ord[tot]=o;
for (int i=h[o];i>=0;i=ne[i])
if (to[i]!=fa)
{
dep[to[i]]=dep[o]+1;
dfs1(to[i],o);
ord[++tot]=o;
}
out[o]=tot;
} void initst()
{
F(i,2,maxn-1) _log[i]=_log[i>>1]+1;
F(i,1,21) F(j,1,tot-(1<<i)+1)
{
if (dep[st[j][i-1]]<dep[st[j+(1<<i-1)][i-1]]) st[j][i]=st[j][i-1];
else st[j][i]=st[j+(1<<i-1)][i-1];
}
} int lca(int l,int r)
{
l=in[l];r=in[r];if (l==r) return st[l][0];
if (l>r) swap(l,r);int tmp=_log[r-l+1];
if (dep[st[l][tmp]]<dep[st[r-(1<<tmp)+1][tmp]]) return st[l][tmp];
else return st[r-(1<<tmp)+1][tmp];
} void debug()
{
int a,b;
while (scanf("%d%d",&a,&b)!=EOF)
printf("%d\n",lca(a,b));
} bool cmp(int a,int b)
{
return in[a]<in[b];
} int hd[maxn],tl[maxn],nxt[maxn],ed,lst[maxn]; void add_edge(int a,int b)
{
if (lst[a]!=idx) lst[a]=idx,hd[a]=-1;
tl[ed]=b;
nxt[ed]=hd[a];
hd[a]=ed++;
} int siz[maxn],mn[maxn],mx[maxn],ans_max,ans_min; ll dfs2(int o,int fa)
{
siz[o]=0;ll ret=0;
mn[o]=inf;mx[o]=-inf;
if (se[o]==idx) mn[o]=0,mx[o]=0,siz[o]++;
for (int i=hd[o];i>=0;i=nxt[i])
if (tl[i]!=fa){
ret+=dfs2(tl[i],o);
siz[o]+=siz[tl[i]];
ans_max=max(ans_max,mx[o]+mx[tl[i]]+dep[tl[i]]-dep[o]);
ans_min=min(ans_min,mn[o]+mn[tl[i]]+dep[tl[i]]-dep[o]);
mn[o]=min(mn[o],mn[tl[i]]+dep[tl[i]]-dep[o]);
mx[o]=max(mx[o],mx[tl[i]]+dep[tl[i]]-dep[o]);
}
for (int i=hd[o];i>=0;i=nxt[i])
if (tl[i]!=fa){
ret+=((ll)k-(ll)siz[tl[i]])*(ll)siz[tl[i]]*((ll)dep[tl[i]]-(ll)dep[o]);
}
return ret;
} int main()
{
memset(h,-1,sizeof h);
scanf("%d",&n);
F(i,2,n)
{
int a,b;scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
dfs1(1,-1);
F(i,1,tot) st[i][0]=ord[i]; initst();
scanf("%d",&m);
for (idx=1;idx<=m;++idx)
{
ed=0;
scanf("%d",&k);
F(i,1,k) scanf("%d",&a[i]);
sort(a+1,a+k+1,cmp);
int rt=a[1];F(i,2,k) rt=lca(rt,a[i]);
sta[top=1]=rt;
F(i,1,k)
{
se[a[i]]=idx; if (a[i]==rt) continue;
int x=lca(a[i],sta[top]); //printf("Add and lca is %d\n",x);
while(dep[x]<dep[sta[top]])
{
int tmp;
if (dep[x]>dep[sta[top-1]])tmp=x;
else tmp=sta[top-1];
add_edge(sta[top],tmp);
add_edge(tmp,sta[top]);
top--;
}
if (x!=sta[top]) sta[++top]=x;
sta[++top]=a[i];
}
while (top>1) add_edge(sta[top],sta[top-1]),add_edge(sta[top-1],sta[top]),top--;
ans_max=-inf;ans_min=inf;
printf("%lld",dfs2(rt,0));
printf(" %d %d\n",ans_min,ans_max);
}
}
BZOJ 3611 [Heoi2014]大工程 ——虚树的更多相关文章
- bzoj 3611: [Heoi2014]大工程 虚树
题目: 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 ...
- BZOJ.3611.[HEOI2014]大工程(虚树 树形DP)
题目链接 要求的和.最大值.最小值好像都可以通过O(n)的树形DP做,总询问点数<=2n. 于是建虚树就可以了.具体DP见DP()函数,维护三个值sum[],mx[],mn[]. sum[]要开 ...
- bzoj 3611[Heoi2014]大工程 虚树+dp
题意: 给一棵树 每次选 k 个关键点,然后在它们两两之间 新建 C(k,2)条 新通道. 求: 1.这些新通道的代价和 2.这些新通道中代价最小的是多少 3.这些新通道中代价最大的是多少 分析:较常 ...
- bzoj 3611 [Heoi2014]大工程(虚树+DP)
3611: [Heoi2014]大工程 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 408 Solved: 190[Submit][Status] ...
- bzoj 3611(洛谷 4103) [Heoi2014]大工程——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3611 https://www.luogu.org/problemnew/show/P4103 ...
- bzoj 3611: [Heoi2014]大工程 && bzoj 2286: [Sdoi2011消耗战
放波建虚树的模板. 大概是用一个栈维护根节点到当前关键点的一条链,把其他深度大于lca的都弹出去. 每次做完记得复原. 还有sort的时候一定要加cmp!!! bzoj 3611 #include&l ...
- luogu P4103 [HEOI2014]大工程 虚树 + 树形 DP
Description 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通 ...
- bzoj 3611: [Heoi2014]大工程
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)
题意 链接 Sol 虚树. 首先建出虚树,然后直接树形dp就行了. 最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案. 任意两点的路径和可以考虑每条边两边的贡献,\(d[x ...
随机推荐
- Azure 项目构建 – 托管静态网站
本课程主要介绍了如何在 Azure 平台上快速构建和部署基于 Azure Web 应用的静态托管网站, 实践讲解如何使用 Azure 门户创建 Web 应用, 部署静态网站源代码,设置自定义域名等. ...
- MySQL流程控制和存储过程介绍
/*定义变量方式1:set @变量名=值;方式2:select 值 into @变量名;方式3:declare 变量名 类型(字符串类型加范围) default 值; in参数 入参的值会仅在存储过程 ...
- 系统妈Win10系统64位和32位快速专业版
win10系统64位快速专业安装版 V2016年 系统妈:http://www.xitongma.com/ Ghost Win10 64位正式装机专业版2016 微软向Windows用户推送了win1 ...
- iTOP-IMX6UL 实战项目:ssh 服务器移植到 arm 开发板
实验环境:迅为提供的Ubuntu12.04.2 以及虚拟机 编译器:arm-2009q3 编译器 开发板系统:QT系统 开发板使用手册中给Windows 系统安装了 ssh 客户端,给 Ubunt ...
- Gym - 101291C (很有意思的最短路)
题意: 给出一张地图和机器人还有出口的位置,地图上面有障碍.然后给出UDLR上下左右四种指令,遇到障碍物或者越界的指令会忽略,剩下的继续执行. 只要到达出口就算找到出口,然后给你一串指令,让你修改指令 ...
- [Android 测试] 压力稳定性测试之: Monkey 详解分析脚本(转载)
一.什么是稳定性测试? 通过随机点击屏幕一段时间,看看app会不会奔溃,能不能维持正常运行. 二. Money是什么? Monkey测试是Android平台自动化测试的一种手段,通过Monkey程序模 ...
- CPP-基础:内部函数应该在当前源文件中说明和定义
static函数与普通函数作用域不同,仅在本文件.只在当前源文件中使用的函数应该说明为内部函数(static),内部函数应该在当前源文件中说明和定义.对于可在当前源文件以外使用的函数,应该在一个头文件 ...
- github更换仓库
1.找到.git目录 2.打开config文件 3.修改仓库地址 4.重新提交 git push --all origin 这样就替我们的项目换仓啦!!!^_^ 分类: git 参考资料: h ...
- 将Xcode的本地代码push到github仓库上
1.首先,你得有一个github账号,如果没有的话就去注册一个,通过下面图片的方式创建一个github仓库. 2.创建仓库后填写相关的信息,比如说仓库名等. 3.在xcode上进行设置,添加远程git ...
- Safari不能保存session的处理方法
在vue单页应用项目中,safari浏览器验证码登陆提示'验证码过期'或者验证码校验不通过的问题 原因:验证码存储在了session里,接着验证时又发起了一次会话,因为Safari不保存cookie, ...