3572

思路:

  虚树+乱搞;

代码:

#include <bits/stdc++.h>
using namespace std;
#define maxn 300005
#define INF 0x3f3f3f3f
struct KiType {
int id,key;
bool operator<(const KiType pos)const
{
return key<pos.key;
}
};
struct KiType ki[maxn];
int bel[maxn],dis[maxn],size[maxn],li[maxn],ri[maxn],lar[maxn],f[maxn];
int head[maxn],E[maxn<<],V[maxn<<],cnt,id[maxn],id_[maxn],deep[maxn];
int top[maxn],ai[maxn],sta[maxn],sum[maxn],n,m;
int W[maxn<<];
bool vis[maxn],if_[maxn];
queue<int>que;
inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'')Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
}
inline void edge_add(int u,int v)
{
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
}
inline void edge_add1(int u,int v)
{
if(deep[u]<deep[v]) swap(u,v);
int w=deep[u]-deep[v];
// printf("%d %d %d\n",u,v,w);
E[++cnt]=head[u],V[cnt]=v,W[cnt]=w,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,W[cnt]=w,head[v]=cnt;
}
void dfs1(int now,int fa)
{
f[now]=fa,deep[now]=deep[fa]+,size[now]=;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
dfs1(V[i],now),size[now]+=size[V[i]];
if(size[V[i]]>size[lar[now]]) lar[now]=V[i];
}
}
void dfs2(int now,int chain)
{
top[now]=chain,id[now]=++cnt,id_[cnt]=now;
if(lar[now]) dfs2(lar[now],chain);
for(int i=head[now];i;i=E[i])
{
if(V[i]==lar[now]||V[i]==f[now]) continue;
dfs2(V[i],V[i]);
}
ri[now]=cnt;
}
inline int find(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=f[top[x]];
}
return deep[x]<deep[y]?x:y;
}
inline int up(int now,int to)
{
while(to<deep[top[now]]) now=f[top[now]];
return id_[id[now]-deep[now]+to];
}
void Count(int now,int fa)
{
int pos,pos_;
if(!fa||bel[fa]==bel[now]) goto cur;
pos=deep[now]-deep[fa]-;
if(pos)
{
pos_=(dis[fa]+pos+dis[now]+(bel[fa]<bel[now]?:))>>;
pos_=up(now,pos_-dis[fa]+deep[fa]+);
pos_=size[pos_]-size[now];
sum[bel[now]]+=pos_,sum[bel[fa]]-=pos_;
}
cur:
sum[bel[now]]+=size[now];
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
sum[bel[now]]-=size[V[i]];
Count(V[i],now);
}
}
void clear(int now,int fa)
{
for(int i=head[now];i;i=E[i])
{
if(fa==V[i]) continue;
clear(V[i],now);
}
head[now]=,sum[now]=,dis[now]=,vis[now]=false;
bel[now]=,dis[now]=INF;
}
int main()
{
freopen("worldtree.in","r",stdin);
freopen("worldtree.out","w",stdout);
in(n);int u,v;
for(int i=;i<n;i++) in(u),in(v),edge_add(u,v);
cnt=,dfs1(,),dfs2(,),memset(head,,sizeof(head));
in(m);
for(int i=;i<=n;i++) dis[i]=INF;
while(m--)
{
in(u),cnt=;
for(int i=;i<=u;i++)
{
in(ki[i].id);
ai[i]=ki[i].id;
dis[ki[i].id]=;
vis[ki[i].id]=true;
if_[ki[i].id]=true;
que.push(ki[i].id);
bel[ki[i].id]=ki[i].id;
ki[i].key=id[ki[i].id];
}
sort(ki+,ki+u+),v=,sta[v]=;
for(int i=;i<=u;i++)
{
int now=ki[i].id,pos=;
if(sta[v]==now) continue;
while(id[now]<li[sta[v]]||id[now]>ri[sta[v]])
{
if(pos) edge_add1(pos,sta[v]);
pos=sta[v],v--;
}
if(pos)
{
int lca=find(pos,now);
if(lca!=pos) edge_add1(pos,lca);
if(lca!=sta[v]) sta[++v]=lca;
}
sta[++v]=now;
}
while(v>) edge_add1(sta[v],sta[v-]),v--;
while(!que.empty())
{
int now=que.front();que.pop(),if_[now]=false;
for(int i=head[now];i;i=E[i])
{
if(dis[V[i]]>dis[now]+W[i])
{
dis[V[i]]=dis[now]+W[i];
bel[V[i]]=bel[now];
if(!if_[V[i]]) if_[V[i]]=true,que.push(V[i]);
}
else if(dis[V[i]]==dis[now]+W[i])
{
if(bel[now]<bel[V[i]])
{
bel[V[i]]=bel[now];
if(!if_[V[i]]) if_[V[i]]=true,que.push(V[i]);
}
}
}
}
Count(,);
for(int i=;i<u;i++) printf("%d ",sum[ai[i]]);
printf("%d \n",sum[ai[u]]);
clear(,);
}
return ;
}

AC日记——[HNOI2014]世界树 bzoj 3572的更多相关文章

  1. AC日记——[Hnoi2017]影魔 bzoj 4826

    4826 思路: 主席树矩阵加减+单调栈预处理: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 ...

  2. AC日记——[LNOI2014]LCA bzoj 3626

    3626 思路: 离线操作+树剖: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 #defin ...

  3. AC日记——[ZJOI2012]网络 bzoj 2816

    2816 思路: 多个LCT: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 10005 #define l ...

  4. AC日记——[SCOI2009]游戏 bzoj 1025

    [SCOI2009]游戏 思路: 和为n的几个数最小公倍数有多少种. dp即可: 代码: #include <bits/stdc++.h> using namespace std; #de ...

  5. AC日记——NOI2016区间 bzoj 4653

    4653 思路: 线段树,指针滑动: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1000005 #def ...

  6. AC日记——Rmq Problem bzoj 3339

    3339 思路: 恶心: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  7. AC日记——[HNOI2008]越狱 bzoj 1008

    1008 思路: 越狱情况=总情况-不越狱情况: 代码: #include <cstdio> #include <cstring> #include <iostream& ...

  8. AC日记——[FJOI2007]轮状病毒 bzoj 1002

    1002 思路: 打表找规律: dp[i]=dp[i-1]*3-dp[i-2]+2; 套个高精就a了: 代码: #include <cstdio> #include <cstring ...

  9. AC日记——[Ahoi2013]作业 bzoj 3236

    3236 思路: 莫队+树状数组维护: 代码: #include <cmath> #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. ZooKeeper管理员指南——部署与管理ZooKeeper

    1.部署 本章节主要讲述如何部署ZooKeeper,包括以下三部分的内容: 系统环境 集群模式的配置 单机模式的配置 系统环境和集群模式配置这两节内容大体讲述了如何部署一个能够用于生产环境的ZK集群. ...

  2. php中error_reporting

    error_reporting(255);是列出所有提示error_reporting(0);是不显示所有提示建议使用error_reporting(7);只显示严重错误1 E_ERROR 致命的运行 ...

  3. [LeetCode] 19. Remove Nth Node From End of List ☆

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  4. android之解析json数据格式详解

    1.JSON解析     (1).解析Object之一: view sourceprint? 1 {"url":"http://www.cnblogs.com/qianx ...

  5. MySQL新建用户,授权

    登录MySQL mysql -u root -p 添加新用户 允许本地 IP 访问 localhost, 127.0.0.1 create user 'test'@'localhost' identi ...

  6. C语言数据库-二叉树

    一.定义 二叉树在图论中是这样定义的:二叉树是一个连通的无环图,并且每一个顶点的度不大于3.有根二叉树还要满足根结点的度不大于2.有了根结点之后,每个顶点定义了唯一的父结点,和最多2个子结点.然而,没 ...

  7. asp.net 权限管理系统

    asp.net webform ,基于组织机构.角色的权限管理系统. 网上找的,挺好.随拿来分享. https://bitbucket.org/zzhi/asp.net

  8. 【Codeforces549F】Yura and Developers [单调栈][二分]

    Yura and Developers Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 4 ...

  9. 基本控件文档-UIButton属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 //转载请注明出处--本文永久链接:http://www.cnblogs.com/Ch ...

  10. {转}用ADMM求解大型机器学习问题

    [本文链接:http://www.cnblogs.com/breezedeus/p/3496819.html] 从等式约束的最小化问题说起:                               ...