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 ...
随机推荐
- Chisel语言
1 What is Chisel? Chisel(Constructing Hardware In a Scala Embedded Language)是一种嵌入在高级编程语言Scala的硬 ...
- 2017“编程之美”终章:AI之战勇者为王
编者按:8月15日,第六届微软“编程之美”挑战赛在选手的火热比拼中圆满落下帷幕.“编程之美”挑战赛是由微软主办,面向高校学生开展的大型编程比赛.自2012年起,微软每年都在革新比赛命题.紧跟时代潮流, ...
- Azure 项目构建 – 构建和部署 .NET 应用程序
本课程主要介绍了如何在 Azure 平台上快速构建和部署基于 .NET 语言的 Web 应用, 实践讲解如何使用 Azure 门户创建 Web 应用, 部署 ASP.NET 代码, 连接 Azure ...
- 如何找到SAP Cloud for Customer标准培训和认证方面的信息
有一些朋友询问我如何在SAP官网上找到和SAP Cloud for Customer相关的标准培训信息,我这里把步骤写出来: 登录SAP官网https://training.sap.com 输入和Cl ...
- Google Colab的一些注意事项
1.执行命令行前面加! 当我们使用python解释器时,我们需要不停地在命令行和IDE 之间切换,当我们需要使用命令行工具时.不过,Jupyter Notebook给了我们在notebook中运行sh ...
- linux_1
注: 创建软连接: "ln -s xxx 路径1" 在路径1创建xxx的软连接 特点: 1.文件类型 l 2.相当于windows的快捷方式 创建硬链接: "ln xxx ...
- OpenCV2:总结篇 core模块
一.cv::Mat 1.作用 cv::Mat表示图像类,用来操作图像和矩阵,它包含很多属性和方法 2.构造方法 cv::Mat image; //cv::Mat image() 无参数构造 ...
- Linux关于FTP安全
https://www.cnblogs.com/Hyber/archive/2017/02/04/6362916.htmlhttps://www.cnblogs.com/ichunqiu/p/7300 ...
- UVa-1368-DNA序列
这题的话,我们每次统计的话,是以列为外层循环,以行为内层循环,逐一按列进行比较. 统计完了之后,题目中要求说到要hamming值最小的,那我们就选用该列最多的字母就可以了,如果有数目相等的字母,那就按 ...
- python 发送附件
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: sksendmail.py #@time: 2018/8/20 13:3 ...