虚树第二题。。。。

同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]大工程 ——虚树的更多相关文章

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

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

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

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

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

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

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

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

  5. bzoj 3611(洛谷 4103) [Heoi2014]大工程——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3611 https://www.luogu.org/problemnew/show/P4103 ...

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

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

  7. luogu P4103 [HEOI2014]大工程 虚树 + 树形 DP

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

  8. bzoj 3611: [Heoi2014]大工程

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

  9. 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)

    题意 链接 Sol 虚树. 首先建出虚树,然后直接树形dp就行了. 最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案. 任意两点的路径和可以考虑每条边两边的贡献,\(d[x ...

随机推荐

  1. iOS之核心动画

    .将动画的所有方法封装到一个类里面 MyCAHelper.h #import <Foundation/Foundation.h> #import <QuartzCore/Quartz ...

  2. Fragment(一)--Fragment用法常见问题

    fragment notes fragment相关内容包括 基本定义与使用 回退栈内部实现 fragment通信(与activity 与fragment) DialogFragment VP + Fr ...

  3. selenium-Python之鼠标事件

    通过click()来模拟鼠标的单击操作,鼠标还具有鼠标右击,双击,悬停甚至鼠标拖动等功能.在webdriver中,将这些鼠标操作方法封装在ActionChains类提供. ActionChains类提 ...

  4. vba 时间

    Sub tt1() Dim d1, d2 As Date d1 = #//# d2 = #//# Debug.Print "相隔" & (d2 - d1) & &q ...

  5. MFC技术积累——基于MFC对话框类的那些事儿3

    3.3.2 创建图形画刷来实现位图加载 1.首先在Resource View中导入一幅位图,位图大小96×96像素: 2.其次在主对话框中添加一个静态文本资源,ID号是IDC_BITMAPAREA,添 ...

  6. Eclipse Java类编辑器里出现乱码的解决方案

    如图:在Java Class编辑器里出现的这种乱码,非常烦人. 解决方案:Windows->Preference->General->Appearance, 在里面将Theme设置成 ...

  7. JavaScript中valueOf函数与toString方法

    基本上,所有JS数据类型都拥有valueOf和toString这两个方法,null除外.它们俩解决javascript值运算与显示的问题,本文将详细介绍,有需要的朋友可以参考下   JavaScrip ...

  8. Windows server 2012安装oracle11g(32/64位)步骤

    Oracle官方下地址: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html以下两网址 ...

  9. java 正则表达式如何提取中文的问题

    String regex="([\u4e00-\u9fa5]+)"; String str="132更新至456"; Matcher matcher = Pat ...

  10. struct结构体内存大小

    一. 基本原则 1. struct中成员变量的声明顺序,与成员变量对应的内存顺序是一致的: 2. struct本身的起始存储地址必须是成员变量中最长的数据类型的整倍数,注意是最长的数据类型,而不是最长 ...