虚树+树形DP


  本题100W的点数……不用虚树真的好吗……

  Orz ZYF

我的感悟:

  dp的过程跟SPOJ 1825 FTOUR2 的做法类似,依次枚举每个子树,从当前子树和之前的部分中各找一条最长(短)路径更新答案,再把这个子树的最短路径加入到x节点中去(之前算过的部分)这样就实现了枚举所有可能的最优情况!而且复杂度很低!避免了两两之间的枚举……

 /**************************************************************
Problem: 3611
User: Tunix
Language: C++
Result: Accepted
Time:7460 ms
Memory:181024 kb
****************************************************************/ //BZOJ 3611
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')sign=-;ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
return v*=sign;
}
/******************tamplate*********************/
const int N=,INF=~0u>>;
typedef long long LL; int n,m,dfn[N],dfs_clock,dep[N],fa[N][],a[N];
LL ans,g[N];
int f[N],_min[N],_max[N],ans1,ans2;
bool v[N]; inline int LCA(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
int t=dep[x]-dep[y];
F(i,,) if(t&(<<i)) x=fa[x][i];
if (x==y) return x;
D(i,,) if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][];
}
struct graph{
int head[N],to[N<<],next[N<<],len[N<<],cnt;
void add(int x,int y){
to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt;
to[++cnt]=x; next[cnt]=head[y]; head[y]=cnt;
}
void ad(int x,int y){
to[++cnt]=y; next[cnt]=head[x]; head[x]=cnt; len[cnt]=dep[y]-dep[x];
}
void dfs(int x){
dfn[x]=++dfs_clock;
F(i,,) if(dep[x]>=(<<i)) fa[x][i]=fa[fa[x][i-]][i-]; else break;
for(int i=head[x];i;i=next[i])
if(to[i]!=fa[x][]){
fa[to[i]][]=x;
dep[to[i]]=dep[x]+;
dfs(to[i]);
}
}
void DP(int x){
f[x]=v[x]; g[x]=;
_min[x]=v[x] ? : INF;
_max[x]=v[x] ? : -INF;
for(int i=head[x];i;i=next[i]){
int y=to[i];
DP(y);
ans+=(g[x]+f[x]*len[i])*f[y]+g[y]*f[x];
f[x]+=f[y];
g[x]+=g[y]+(LL)len[i]*f[y];
ans1=min(ans1,_min[x]+_min[y]+len[i]);
ans2=max(ans2,_max[x]+_max[y]+len[i]);
_min[x]=min(_min[x],_min[y]+len[i]);
_max[x]=max(_max[x],_max[y]+len[i]);
}
head[x]=;
}
}G1,G2;
inline bool cmp(int a,int b){ return dfn[a]<dfn[b]; }
int st[N],top;
int main(){
// freopen("3611.in","r",stdin);
n=getint();
int x,y;
F(i,,n){
x=getint(); y=getint();
G1.add(x,y);
}
G1.dfs(); int T=getint();
while(T--){
m=getint();
F(i,,m) {a[i]=getint(); v[a[i]]=;}
sort(a+,a+m+,cmp); st[top=]=; G2.cnt=;
ans=; ans1=INF; ans2=-INF;
F(i,,m){
int x=a[i],f=LCA(x,st[top]);
while(dep[f]<dep[st[top]]){
if(dep[f]>=dep[st[top-]]){
G2.ad(f,st[top--]);
if(st[top]!=f) st[++top]=f;
break;
}
G2.ad(st[top-],st[top]); top--;
}
if(st[top]!=x) st[++top]=x;
}
while(--top) G2.ad(st[top],st[top+]);
G2.DP();
printf("%lld %d %d\n",ans,ans1,ans2);
F(i,,m) v[a[i]]=;
}
return ;
}

3611: [Heoi2014]大工程

Time Limit: 60 Sec  Memory Limit: 512 MB
Submit: 273  Solved: 129
[Submit][Status][Discuss]

Description

国家有一个大工程,要给一个非常大的交通网络里建一些新的通道。 
我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上。 
在 2 个国家 a,b 之间建一条新通道需要的代价为树上 a,b 的最短路径。
 现在国家有很多个计划,每个计划都是这样,我们选中了 k 个点,然后在它们两两之间 新建 C(k,2)条 新通道。
现在对于每个计划,我们想知道:
 1.这些新通道的代价和
 2.这些新通道中代价最小的是多少 
3.这些新通道中代价最大的是多少

Input

第一行 n 表示点数。

 接下来 n-1 行,每行两个数 a,b 表示 a 和 b 之间有一条边。
点从 1 开始标号。 接下来一行 q 表示计划数。
对每个计划有 2 行,第一行 k 表示这个计划选中了几个点。
 第二行用空格隔开的 k 个互不相同的数表示选了哪 k 个点。

Output

输出 q 行,每行三个数分别表示代价和,最小代价,最大代价。

 

Sample Input

10
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

3 3 3
6 6 6
1 1 1
2 2 2
2 2 2

HINT

n<=1000000

q<=50000并且保证所有k之和<=2*n 

Source

[Submit][Status][Discuss]

【BZOJ】【3611】【HEOI2014】大工程的更多相关文章

  1. bzoj 3611 [Heoi2014]大工程(虚树+DP)

    3611: [Heoi2014]大工程 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 408  Solved: 190[Submit][Status] ...

  2. bzoj 3611: [Heoi2014]大工程 && bzoj 2286: [Sdoi2011消耗战

    放波建虚树的模板. 大概是用一个栈维护根节点到当前关键点的一条链,把其他深度大于lca的都弹出去. 每次做完记得复原. 还有sort的时候一定要加cmp!!! bzoj 3611 #include&l ...

  3. bzoj 3611: [Heoi2014]大工程 虚树

    题目: 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 ...

  4. bzoj 3611[Heoi2014]大工程 虚树+dp

    题意: 给一棵树 每次选 k 个关键点,然后在它们两两之间 新建 C(k,2)条 新通道. 求: 1.这些新通道的代价和 2.这些新通道中代价最小的是多少 3.这些新通道中代价最大的是多少 分析:较常 ...

  5. bzoj 3611: [Heoi2014]大工程

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...

  6. BZOJ.3611.[HEOI2014]大工程(虚树 树形DP)

    题目链接 要求的和.最大值.最小值好像都可以通过O(n)的树形DP做,总询问点数<=2n. 于是建虚树就可以了.具体DP见DP()函数,维护三个值sum[],mx[],mn[]. sum[]要开 ...

  7. BZOJ 3611 [Heoi2014]大工程 ——虚树

    虚树第二题.... 同BZOJ2286 #include <map> #include <cmath> #include <queue> #include < ...

  8. 3611: [Heoi2014]大工程

    3611: [Heoi2014]大工程 链接 分析: 树形dp+虚树. 首先建立虚树,在虚树上dp. dp:sum[i]为i的子树中所有询问点之间的和.siz[i]为i的子树中有多少询问点,mn[i] ...

  9. BZOJ2286 [Sdoi2011]消耗战 和 BZOJ3611 [Heoi2014]大工程

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6371  Solved: 2496[Submit][Statu ...

  10. [Bzoj3611][Heoi2014]大工程(虚树)

    3611: [Heoi2014]大工程 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 2000  Solved: 837[Submit][Status ...

随机推荐

  1. 02:实现Singleton模式

    Java实现单例模式有很多种实现方法,其中我们应根据需要选择线程安全的与非线程安全的两种方式,根据对象实现的方式又分为饱汉与饿汉方式. 这里使用java中的volatile关键字与synchroniz ...

  2. 【mysql】当where后接字符串,查询时会发生什么?

    好久没有研究一个“深层次”的问题了. 首先来看我们为什么要讨论这个问题~ 首先这是一个正常的数据库查询,我们可以看到在ruizhi数据库里的chouka表内,所有数据如图. 现在,我们运行查询: se ...

  3. 同步VDP时间

    使用yast 进入蓝屏界面,修改system—date and time,取消hardware clock set to utc,时区设置为上海或者北京,然后sntp -r 时间服务器地址 敲击syn ...

  4. SQL注入实验

    看到他们黑站感觉很有意思的样子,于是我也玩了一下午,虽然都是些狠狠狠简单的东西,不过还是记录下来啦. 虽然和我现在做的没啥关系,不过,,,挺好 浏览器的“工具”——“internet选项”——“高级” ...

  5. 在python中独立运行orm

  6. android viewStub

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 开发应用的时候,需要根据条件决定显示某个视图, 这个时候可以用ViewStub Stub ...

  7. luoguP5024 保卫王国 动态dp

    题目大意: emmmmm 题解: QAQ #include <cstdio> #include <cstring> #include <iostream> usin ...

  8. Azure ServiceBus的消息中带有@strin3http//schemas.microsoft.com/2003/10/Serialization/�

    今天碰到一个很讨厌的问题,使用nodejs 接收Azure service bus队列消息的时候,出现了:@strin3http//schemas.microsoft.com/2003/10/Seri ...

  9. String 字符串详解 / 常用API

    String 详解 / 常用API 简介 String 是不可改变的字符串序列.String 为字符串常量 StringBuilder 与StringBuffer 均为可改变的字符串序列.为字符串变量 ...

  10. Codeforces Round #283 (Div. 2) C. Removing Columns 暴力

    C. Removing Columns time limit per test 2 seconds memory limit per test 256 megabytes input standard ...