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. js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd

    昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...

  2. 给独立搭建的博客启用https的过程

    申请SSL证书 我自己独立搭建的博客部署在阿里云服务器上,因此我就先搜索阿里云启用https的方法,网上有比较详细的讲解,在此提供一个参考网址: https://blog.csdn.net/csluc ...

  3. 基于Numpy的神经网络+手写数字识别

    基于Numpy的神经网络+手写数字识别 本文代码来自Tariq Rashid所著<Python神经网络编程> 代码分为三个部分,框架如下所示: # neural network class ...

  4. 慕课网2-5 编程练习:通过jQuery通配符选择器进行文字变色

    2-5 编程练习 请请使用*选择器将div标签中的字体颜色变成红色 效果图: 任务 (1)使用通配符选择器 (2)使用jQuery的.css()方法设置样式,语法css('属性 '属性值') 参考代码 ...

  5. (快排)51NOD 1018 排序

    给出N个整数,对着N个整数进行排序   Input 第1行:整数的数量N(1 <= N <= 50000) 第2 - N + 1行:待排序的整数(-10^9 <= A[i] < ...

  6. *RelativeLayout的布局参数含义表,如android:layout_alignParentTop等

    RelativeLayout 参数规则 一个控件的位置由横,纵两个方向上的距离决定 控件默认的位置在左上角. 单独使用以下属性都只是改变一个方向的相对位置. 如:只使用了android:layout_ ...

  7. Java多线程——线程之间的协作

    Java多线程——线程之间的协作 摘要:本文主要学习多线程之间是如何协作的,以及如何使用wait()方法与notify()/notifyAll()方法. 部分内容来自以下博客: https://www ...

  8. 如何删除sublime目录

    左侧栏的sublime目录一直删不掉,删除列直接变成了灰色. 今天才发现应该选择文件夹右击选择工程——从工程中删除文件夹. 这个设计真的很醉,删除这么常用的键还放进了第二层……

  9. sql Server与ORACLE的语法区别 自用整理!

    /*整理背景201403订单中心数据库迁移(整理Oracle与SQL的差异)整理规则第一句为SQL Server 第二句为Oracle*/--数据类型int integervarchar varcha ...

  10. CommHelper

    18位流水号: public static string GenerateTransId(int i) { string transId = DateTime.Now.ToString("y ...