New Barns
New Barns
时间限制: 1 Sec 内存限制: 128 MB
题目描述
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.
输入
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.
输出
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的更多相关文章
- P4271 [USACO18FEB]New Barns
题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...
- 题解【[USACO18FEB]New Barns 】
浅谈一下对于这题做完之后的感受(不看题解也是敲不出来啊qwq--) 题意翻译 Farmer John注意到他的奶牛们如果被关得太紧就容易吵架,所以他想开放一些新的牛棚来分散她们. 每当FJ建造一个新牛 ...
- [usaco18Feb] New Barns
题意 每次新建一个节点,并与一个已知节点连边.(或者不连).多次询问以某个已知点点出发的最远路径长度. 分析 显然,在任何时候图都是一个森林.由树的直径算法可知,与某点最远距的点必然是树的直径的一段. ...
- [Usaco2018 Feb] New Barns
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5192 [算法] 维护树的直径,在树上离一个点最远的点一定是一条直径的端点. ...
- bzoj5192: [Usaco2018 Feb]New Barns
不想写看zory大佬 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...
- Luogu P4271 [USACO18FEB]New Barns P
题意 给一个一开始没有点的图,有 \(q\) 次操作,每次为加点连边或者查询一个点到连通块内所有点的距离最大值. \(\texttt{Data Range}:1\leq q\leq 10^5\) 题解 ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- 续并查集学习笔记——Closing the farm题解
在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. Description Farmer John and his cows are planning ...
- 【BZOJ-3697&3127】采药人的路径&YinandYang 点分治 + 乱搞
3697: 采药人的路径 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 681 Solved: 246[Submit][Status][Discus ...
随机推荐
- P1128 [HNOI2001]求正整数
传送门 rqy是我们的红太阳没有它我们就会死 可以考虑dp,设\(dp[i][j]\)表示只包含前\(j\)个质数的数中,因子个数为\(i\)的数的最小值是多少,那么有转移方程 \[f[i][j]=m ...
- Django day 34 过滤课程,登录,redis,python操作redis
一:过滤课程, 二:登录 三:redis, 四:python操作redis
- 源码阅读之ArrayList(JDK8)
ArrayList概述 ArrayList是一个的可变数组的实现,实现了所有可选列表操作,并允许包括 null 在内的所有元素.每个ArrayList实例都有一个容量,该容量是指用来存储列表元素的数组 ...
- Shell脚本,简单& 强大
摘自<码农增刊Linus与Linux>,章节:你可能不知道的Shell. 最近阅读完这本书,觉得其中有很多不错的内容,这是其中的一个Shell小甜点,拿来和大家一起分享一下,增加了 ...
- maven build过程中遇到的问题以及解决方案
(1)不支持泛型以及@Override 问题来源:使用了低版本的jdk,默认情况下maven使用的是jdk1.5的版本,而泛型和@Override是后期版本才有的,需要更改maven默认的jdk版本. ...
- http请求和响应头部
说一说常见的请求头和响应头都有什么呢? 1)请求(客户端->服务端[request]) GET(请求的方式) /newcoder/hello.html(请求的目标资源) HTTP/1.1 ...
- Fiddler——基本常识
web session界面 inspector面板 xml:查看XML数据 json:查看json数据 raw:可以完整查看请求的内容 cookies:可以查看请求的cookie header:查看请 ...
- 北大ACM(POJ1016-Numbers That Count)
Question:http://poj.org/problem?id=1016 问题点:水题. Memory: 232K Time: 125MS Language: C++ Result: Accep ...
- (转)淘淘商城系列——使用solrj来测试索引库
http://blog.csdn.net/yerenyuan_pku/article/details/72892280 我们使用solrj来操作索引库,一般习惯先建一个单元测试类测试下增删改查方法是否 ...
- (转) 淘淘商城系列——Redis集群的搭建
http://blog.csdn.net/yerenyuan_pku/article/details/72860432 本文我将带领大家如何搭建Redis集群.首先说一下,为何要搭建Redis集群.R ...