大工程(bzoj 3611)
Description
Input
第一行 n 表示点数。
Output
输出 q 行,每行三个数分别表示代价和,最小代价,最大代价。
Sample Input
2 1
3 2
4 1
5 2
6 4
7 5
8 6
9 7
10 9
5
2
5 4
2
10 4
2
5 2
2
6 1
2
6 1
Sample Output
6 6 6
1 1 1
2 2 2
2 2 2
/*
f[i]表示以i为根的子树的路径和
f[i]=f[son[i]]+siz[son[y]]*(cnt-siz[son[y]])*dis(i,son[i])
maxs[i]表示以i为根的子树的节点到i的最大长度
用maxs[i]+maxs[son[i]]+dis(i,son[i])来更新答案。
然后在虚树上做DP
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 1000010
#define lon long long
#define inf 1000000000
using namespace std;
int n,m,dfn[N],dep[N],tim,fa[N][],a[N],num;
int g[N],sta[N],top,siz[N],mins[N],maxs[N],ans1,ans2;
lon f[N];
bool cmp(int x,int y){return dfn[x]<dfn[y];}
struct Node{
int head[N],son[N*],pre[N*],cnt;
void add(int u,int v){
son[++cnt]=v;pre[cnt]=head[u];head[u]=cnt;
}
void dfs1(int u){
dfn[u]=++tim;
for(int i=;i<=;i++) fa[u][i]=fa[fa[u][i-]][i-];
for(int i=head[u];i;i=pre[i])
if(son[i]!=fa[u][]){
fa[son[i]][]=u;
dep[son[i]]=dep[u]+;
dfs1(son[i]);
}
}
void dfs2(int x){
siz[x]=g[x];maxs[x]=;mins[x]=inf;f[x]=;
for(int i=head[x];i;i=pre[i]){
int d=dep[son[i]]-dep[x];
dfs2(son[i]);siz[x]+=siz[son[i]];
ans1=min(ans1,mins[x]+mins[son[i]]+d);
mins[x]=min(mins[x],mins[son[i]]+d);
ans2=max(ans2,maxs[x]+maxs[son[i]]+d);
maxs[x]=max(maxs[x],maxs[son[i]]+d);
f[x]+=f[son[i]]+1LL*siz[son[i]]*(num-siz[son[i]])*d;
}
if(g[x]) ans1=min(ans1,mins[x]),ans2=max(ans2,maxs[x]),mins[x]=;
head[x]=;
}
}g1,g2;
int lca(int a,int b){
if(dep[a]<dep[b]) swap(a,b);
int t=dep[a]-dep[b];
for(int i=;~i;i--) if(t&(<<i)) a=fa[a][i];
if(a==b) return a;
for(int i=;~i;i--)
if(fa[a][i]!=fa[b][i])
a=fa[a][i],b=fa[b][i];
return fa[a][];
}
void work(){
top=;
for(int i=;i<=num;i++){
if(!top){sta[++top]=a[i];continue;}
int anc=lca(a[i],sta[top]);
while(dep[anc]<dep[sta[top]]){
if(dep[anc]>=dep[sta[top-]]){
g2.add(anc,sta[top]);
top--;
if(sta[top]!=anc) sta[++top]=anc;
break;
}
else g2.add(sta[top-],sta[top]),top--;
}
if(sta[top]!=a[i]) sta[++top]=a[i];
}
while(top>) g2.add(sta[top-],sta[top]),top--;
ans1=inf;ans2=;g2.dfs2(sta[]);
printf("%lld %d %d\n",f[sta[]],ans1,ans2);
for(int i=;i<=num;i++) g[a[i]]=;g2.cnt=;
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
g1.add(u,v);g1.add(v,u);
}
g1.dfs1();scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d",&num);
for(int j=;j<=num;j++) scanf("%d",&a[j]),g[a[j]]=;
sort(a+,a+num+,cmp);
work();
}
return ;
}
大工程(bzoj 3611)的更多相关文章
- bzoj 3611: [Heoi2014]大工程 && bzoj 2286: [Sdoi2011消耗战
放波建虚树的模板. 大概是用一个栈维护根节点到当前关键点的一条链,把其他深度大于lca的都弹出去. 每次做完记得复原. 还有sort的时候一定要加cmp!!! bzoj 3611 #include&l ...
- 【BZOJ】【3611】【HEOI2014】大工程
虚树+树形DP 本题100W的点数……不用虚树真的好吗…… Orz ZYF 我的感悟: dp的过程跟SPOJ 1825 FTOUR2 的做法类似,依次枚举每个子树,从当前子树和之前的部分中各找一条最长 ...
- bzoj 3611 [Heoi2014]大工程(虚树+DP)
3611: [Heoi2014]大工程 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 408 Solved: 190[Submit][Status] ...
- bzoj 3611: [Heoi2014]大工程 虚树
题目: 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 ...
- BZOJ 3611 大工程 (虚树)
题面 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 a ...
- 3611: [Heoi2014]大工程
3611: [Heoi2014]大工程 链接 分析: 树形dp+虚树. 首先建立虚树,在虚树上dp. dp:sum[i]为i的子树中所有询问点之间的和.siz[i]为i的子树中有多少询问点,mn[i] ...
- 【BZOJ3611】大工程(虚树,动态规划)
[BZOJ3611]大工程(虚树,动态规划) 题面 BZOJ Description 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树 ...
- BZOJ2286 [Sdoi2011]消耗战 和 BZOJ3611 [Heoi2014]大工程
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6371 Solved: 2496[Submit][Statu ...
- [Bzoj3611][Heoi2014]大工程(虚树)
3611: [Heoi2014]大工程 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 2000 Solved: 837[Submit][Status ...
随机推荐
- python__系统 : socket_UDP相关
socket.socket() 可以创建一个套接字: from socket import * from threading import Thread udp_socket = None dest_ ...
- swoole中swoole_timer_tick回调函数使用对象方法
swoole_timer_tick的回调函数无法使用类似于array($this, 'method')来调用对象方法,所以我用一下方法变通了一下来调用 swoole_timer_tick(1000, ...
- 1,flask简介
一. Python 现阶段三大主流Web框架 Django Tornado Flask 对比 1.Django 主要特点是大而全,集成了很多组件,例如: Models Admin Form 等等, 不 ...
- ListView getChildCount 以及getChildAt 坑 误区指南
今天调试的时候,才知道. 原来listview 的 getChildCount 取得是当前可先的list item 的个数,而不是整个listview 的count. 整个listview 的数量应该 ...
- How to send CTRL+BREAK signal to detached command-line process
1.GenerateConsoleCtrlEvent function Sends a specified signal to a console process group that shares ...
- Tensorflor实现文本分类
Tensorflor实现文本分类 下面我们使用CNN做文本分类 cnn实现文本分类的原理 下图展示了如何使用cnn进行句子分类.输入是一个句子,为了使其可以进行卷积,首先需要将其转化为向量表示,通常使 ...
- Category的真相
Objective-C 中的 Category 就是对设计模式中装饰模式的一种具体实现.它的主要作用是在不改变原有类的前提下,动态地给这个类添加一些方法. 使用场景 根据苹果官方文档对 Categor ...
- 【APUE】Chapter11 Threads
看完了APUE第三版的Chapter11 Threads,跟着书上的demo走了一遍,并且参考了这个blog(http://www.cnblogs.com/chuyuhuashi/p/4447817. ...
- Oracle 学习----游标(使用无参光标cursor)
表: ----游标----------------------------- declare cursor tt is select name,sal from temp; vname temp.na ...
- heat launch an instance
在包含Orchestration服务的环境中,可以创建启动实例的堆栈 创建yam文件 heat_template_version: 2015-10-15 description: Launch a b ...