首页 技术 正文
技术 2022年11月6日
0 收藏 691 点赞 601 浏览 3656 个字

New Barns

时间限制: 1 Sec  内存限制: 128 MB

题目描述

Farmer John notices that his cows tend to get into arguments if they are packed too closely together, so he wants to open a series of new barns to help spread them out.
Whenever FJ constructs a new barn, he
connects it with at most one bidirectional pathway to an existing barn.
In order to make sure his cows are spread sufficiently far apart, he
sometimes wants to determine the distance from a certain barn to the
farthest possible barn reachable from it (the distance between two barns
is the number of paths one must traverse to go from one barn to the
other).

FJ will give a total of Q (1≤Q≤105)
queries, each either of the form “build” or “distance”. For a build
query, FJ builds a barn and links it with at most one previously built
barn. For a distance query, FJ asks you the distance from a certain barn
to the farthest barn reachable from it via a series of pathways. It is
guaranteed that the queried barn has already been built. Please help FJ
answer all of these queries.

输入

The
first line contains the integer Q. Each of the next Q lines contains a
query. Each query is of the form “B p” or “Q k”, respectively telling
you to build a barn and connect it with barn pp, or give the farthest
distance, as defined, from barn k. If p=−1, then the new barn will be
connected to no other barn. Otherwise, p is the index of a barn that has
already been built. The barn indices start from 1, so the first barn
built is barn 1, the second is barn 2, and so on.

输出

Please
write one line of output for each distance query. Note that a barn
which is connected to no other barns has farthest distance 0.

样例输入

7
B -1
Q 1
B 1
B 2
Q 3
B 2
Q 2

样例输出

0
2
1

提示

The example input corresponds to this network of barns:
  (1) 
    \   
     (2)—(4)
    /
  (3)
In query 1, we build barn number 1. In query 2, we ask for the distance
of 1 to the farthest connected barn. Since barn 1 is connected to no
other barns, the answer is 0. In query 3, we build barn number 2 and
connect it to barn 1. In query 4, we build barn number 3 and connect it
to barn 2. In query 5, we ask for the distance of 3 to the farthest
connected barn. In this case, the farthest is barn 1, which is 2 units
away. In query 6, we build barn number 4 and connect it to barn 2. In
query 7, we ask for the distance of 2 to the farthest connected barn.
All three barns 1, 3, 4 are the same distance away, which is 1, so this
is our answer.

分析:题意:给定一个森林,在建森林的过程中有一些询问,距离某个点最远的点的距离;

   考虑分治,对于每个点的祖先,要么经过祖先,要么不经过;

   如果经过,考虑维护这个祖先的最大的两个点的深度,这两个点不在同一棵子树且已经被标记;更新答案分在不在同一子树里即可;

   如果不经过,则可以递归到子树,对于分治来说,维护重心,每个点有log个祖先;

   注意如果两个点被标记则lca也被标记过;

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+,mod=1e9+,inf=0x3f3f3f3f;
int n,m,k,t,rt,a[maxn],sz[maxn],all;
bool vis[maxn];
vector<int>e[maxn];
struct node
{
int mx1,mx2,mxid;
vector<pair<int,int> >anc;
}p[maxn];
int getroot(int x,int y=)
{
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(z==y||vis[z])continue;
if(sz[z]*>=all)return getroot(z,x);
}
return x;
}
void getdep(int x,int y,int dep)
{
p[x].anc.push_back({rt,dep});
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(z==y||vis[z])continue;
getdep(z,x,dep+);
}
}
void getsz(int x,int y=)
{
sz[x]=;
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(z==y||vis[z])continue;
getsz(z,x);
sz[x]+=sz[z];
}
}
void dfs(int x)
{
getsz(x);
all=sz[x];
rt=getroot(x);
getdep(rt,,);
vis[rt]=true;
for(int i=;i<e[rt].size();i++)
{
int z=e[rt][i];
if(!vis[z])dfs(z);
}
}
int main()
{
int i,j;
scanf("%d",&m);
int pos=;
for(i=;i<=m;i++)
{
char str[];
scanf("%s%d",str,&n);
if(str[]=='B')
{
a[i]=++pos;
if(n==-)continue;
else e[n].push_back(pos),e[pos].push_back(n);
}
else a[i]=-n;
}
for(i=;i<=pos;i++)if(!vis[i])dfs(i);
pos=;
for(i=;i<=m;i++)
{
int last=-;
if(a[i]>)
{
++pos;
for(j=p[pos].anc.size()-;j>=;j--)
{
int fa=p[pos].anc[j].first,w=p[pos].anc[j].second;
if(p[fa].mx1<=w)
{
if(p[fa].mxid!=last)p[fa].mx2=p[fa].mx1;
p[fa].mx1=w;
p[fa].mxid=last;
}
else if(p[fa].mx2<=w)
{
if(last!=p[fa].mxid)p[fa].mx2=w;
}
last=fa;
}
}
else
{
int now=-a[i];
int ret=p[now].mx1;
for(j=p[now].anc.size()-;j>=;j--)
{
int fa=p[now].anc[j].first,w=p[now].anc[j].second;
if(fa>pos)
{
last=fa;
continue;
}
if(last==p[fa].mxid)ret=max(ret,w+p[fa].mx2);
else ret=max(ret,w+p[fa].mx1);
last=fa;
}
printf("%d\n",ret);
}
}
return ;
}
上一篇: 【04】JSONP 教程
下一篇: Scala解析Json格式
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777