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. eclipse ide for java ee developers 开发环境搭建(J2EE) 【转载】

    使用eclipse真的有年头了,相信java程序员没有不知道它的,最近在给团队中新来的应届生做指导,专门讲解了一下Eclipse开发环境的搭建过程, 一是帮助他们尽快的熟悉IDE的使用,二也是保证团队 ...

  2. LoadRunner中的IP欺骗

    应用程序服务器和网络设备使用IP地址来识别客户端.应用程序服务器通常会对来自同一计算机的客户端信息进行高速缓存. 网络路由器尝试对原信息和目标信息进行高速缓存以优化吞吐量.如果多个用户具有相同的IP地 ...

  3. SDUT3926 kmp

    bLue的二叉树 Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Problem Description Keke 是一个喜爱种树的 ...

  4. oracle xml操作

      /*=====================生成\修改xml========================= */ --xmlelement多个标签层级 SELECT XMLELEMENT(& ...

  5. jQuery图表插件Flot中文文档

    转载自:http://www.itivy.com/ivy/archive/2011/6/4/jquery-flot-chinese-doc.html 最近正在使用JQuery的flot进行画图,但是这 ...

  6. kvm增加硬盘挂载

    1.查询需要添加虚拟主机 [root@sz-kvm-110 images]# virsh list --all  Id    名称                         状态 ------- ...

  7. homebrew常见用法

    1. 安装 Homebrew是mac下安装软件的好帮手, 是使用 ruby 写的,采用 github 来存放信息库,很方便吧. Ruby 已经内置,最好装上 Xcode,因为可能需要一些编译包.然后在 ...

  8. HTML或者JSP页面--执行完某事件后刷新页面,重置表单,清空数据

    在提交表单或者执行某个事件之后,如果需要重置表单(即清空表单里的数据) 可以执行下面代码来完成 方式一: self.location.href="userController.do?goAd ...

  9. gradle web项目启动报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    严重: Error configuring application listener of class org.springframework.web.util.IntrospectorCleanup ...

  10. 【hdu5381】维护区间内所有子区间的gcd之和-线段树

    题意:给定n个数,m个询问,每次询问一个区间内所有连续子区间的gcd的和.n,m<=10^5 题解: 这题和之前比赛的一题很像.我们从小到大枚举r,固定右端点枚举左端点,维护的区间最多只有log ...