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 ;
}

New Barns的更多相关文章

  1. P4271 [USACO18FEB]New Barns

    题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...

  2. 题解【[USACO18FEB]New Barns 】

    浅谈一下对于这题做完之后的感受(不看题解也是敲不出来啊qwq--) 题意翻译 Farmer John注意到他的奶牛们如果被关得太紧就容易吵架,所以他想开放一些新的牛棚来分散她们. 每当FJ建造一个新牛 ...

  3. [usaco18Feb] New Barns

    题意 每次新建一个节点,并与一个已知节点连边.(或者不连).多次询问以某个已知点点出发的最远路径长度. 分析 显然,在任何时候图都是一个森林.由树的直径算法可知,与某点最远距的点必然是树的直径的一段. ...

  4. [Usaco2018 Feb] New Barns

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5192 [算法] 维护树的直径,在树上离一个点最远的点一定是一条直径的端点.     ...

  5. bzoj5192: [Usaco2018 Feb]New Barns

    不想写看zory大佬 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...

  6. Luogu P4271 [USACO18FEB]New Barns P

    题意 给一个一开始没有点的图,有 \(q\) 次操作,每次为加点连边或者查询一个点到连通块内所有点的距离最大值. \(\texttt{Data Range}:1\leq q\leq 10^5\) 题解 ...

  7. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  8. 续并查集学习笔记——Closing the farm题解

    在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. Description Farmer John and his cows are planning ...

  9. 【BZOJ-3697&3127】采药人的路径&YinandYang 点分治 + 乱搞

    3697: 采药人的路径 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 681  Solved: 246[Submit][Status][Discus ...

随机推荐

  1. sublime text2 配置php本地环境时遇到的错误。

    首先,将PHP加到电脑的环境变量中如图(D:\PHPEnv\PHP5是我PHP的安装目录): 第二步:添加编译系统配置 第三步:配置详情: { "cmd": ["php. ...

  2. INT类型知多少

    前言: 整型是MySQL中最常用的字段类型之一,通常用于存储整数,其中int是整型中最常用的,对于int类型你是否真正了解呢?本文会带你熟悉int类型相关知识,也会介绍其他整型字段的使用. 1.整型分 ...

  3. HUE通过oozie工作流执行shell脚本

    HUE通过oozie工作流执行shell脚本 2018年01月17日 16:20:38 阅读数:217 首先上传对应的jar包和storm.sh脚本到hdfs,脚本内容如下: 脚本主要内容是:从hdf ...

  4. MongoDB一些常用指令与他的JavaScript的对应表

  5. linux学习之路6 Vi文本编辑器

    vim是vi的增强版本 vim拥有三种模式: 命令模式(常规模式) vim启动后,默认进入命令模式.任何模式都可以通过按esc键回到命令模式(可以多按几次.命令模式可以通过键入不同的命令完成选择.复制 ...

  6. 记录一次mysql导入千万条测试数据过慢的问题!

    数据库在没有做任何优化的情况下,使用存储过程,插入1千万条测试数据. CREATE PROCEDURE addmaxdata(IN n int) BEGIN DECLARE i INT DEFAULT ...

  7. android序列化(1)Parcelable与Serializable

    1.Android中实现序列化有两个选择 一是实现Serializable接口(是JavaSE本身就支持的),实现Serializable接口非常简单. 一是实现Parcelable接口(是Andro ...

  8. 数据采集框架Gobblin简介

    问题导读: Gobblin的架构设计是怎样的? Gobblin拥有哪些组建,如何实现可扩展? Gobblin采集执行流程的过程? 前面我们介绍Gobblin是用来整合各种数据源的通用型ETL框架,在某 ...

  9. C#学习-EF在三层中使用

    1.搭建普通三层 DAL层,BLL层,Model层,Web层: DAL层引用Model层 BLL层引用DAL层和Model层 Web层引用BLL层和Model层 2.实现EF三层的搭建(添加引用,修改 ...

  10. CentOS6.6从头到尾部署nginx与tomcat多实例

    前提条件: 1.需要一个全新的centos系统(本文中用到是centos6.6) 2.vmware虚拟机 3.vmware下安装centos系统,以NAT方式与宿主机相连 4.在centos系统中pi ...