题解【[USACO18FEB]New Barns 】
浅谈一下对于这题做完之后的感受(不看题解也是敲不出来啊qwq……)
题意翻译
Farmer John注意到他的奶牛们如果被关得太紧就容易吵架,所以他想开放一些新的牛棚来分散她们。 每当FJ建造一个新牛棚的时候,他会将这个牛棚用至多一条双向道路与一个现有的牛棚连接起来。为了确保他的奶牛们足够分散,他有时想要确定从某个特定的牛棚出发,到它能够到达的最远的牛棚的距离(两个牛棚之间的距离等于从一个牛棚出发到另一个之间必须经过的道路条数)。
FJ总共会给出QQ(1 \leq Q \leq 10^51≤Q≤105)次请求,每个请求都是“建造”和“距离”之一。对于一个建造请求,FJ建造一个牛棚,并将其与至多一个现有的牛棚连接起来。对于一个距离请求,FJ向你询问从某个特定的牛棚通过一些道路到离它最远的牛棚的距离。保证询问的牛棚已经被建造。请帮助FJ响应所有的请求。
输入格式(文件名:newbarn.in): 第一行包含一个整数QQ。以下QQ行,每行包含一个请求。每个请求的格式都是“B p”或是“Q k”,分别告诉你建造一个牛棚并与牛棚pp连接,或是根据定义求从牛棚kk出发最远的距离。如果p = -1p=−1,则新的牛棚不会与其他牛棚连接。否则,pp是一个已经建造的牛棚的编号。牛棚编号从11开始,所以第一个被建造的谷仓是11号谷仓,第二个是22号谷仓,以此类推。 输出格式(文件名:newbarn.out): 对于每个距离请求输出一行。注意一个没有连接到其他牛棚的牛棚的最远距离为00。
题目描述
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 QQ (1 \leq Q \leq 10^51≤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 QQ. Each of the next QQ 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 kk. If p = -1p=−1, then the new barn will be connected to no other barn. Otherwise, pp is the index of a barn that has already been built. The barn indices start from 11, so the first barn built is barn 11, the second is barn 22, 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 00.
解:
题意就是要动态连边(合并一个点和联通块)和求树的直径。
每次找最大距离的时候,分别求点的左右子树的距离取较大者。
每次可以用并查集查询联通块的根。
用一个数组记录每次深度,每次合并更新。
每次查询,split一下,LCT的每个节点的值都是1,那么距离就是sum-1.
用LCT维护区间和&Link操作即可。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int n,m,opt,a,b,dis[500000];//dis 保存距离
int bel[500000];//并查集
struct node{
int fa,ch[2],sum,tag;
}tr[400000];
struct Node{
int a,b;
}s[500000];//本题要求合并一个点和一个联通块,连着俩点保存一下
char cg[3];
inline bool root(int x){
int g=tr[x].fa;
return !(tr[g].ch[0]==x||tr[g].ch[1]==x);
}inline void pushup(int x){tr[x].sum=1+tr[tr[x].ch[0]].sum+tr[tr[x].ch[1]].sum;}
inline void pushr(int x){
if(!x)return;
swap(tr[x].ch[0],tr[x].ch[1]);
tr[x].tag^=1;
}inline void pushdown(int x){
if(tr[x].tag){
pushr(tr[x].ch[0]);
pushr(tr[x].ch[1]);
tr[x].tag=0;
}
}inline void push(int x){
if(!root(x))push(tr[x].fa);
pushdown(x);
}inline void rotate(int x){
int y=tr[x].fa,z=tr[y].fa,k=tr[y].ch[1]==x;
if(!root(y))tr[z].ch[tr[z].ch[1]==y]=x;
tr[x].fa=z;tr[y].ch[k]=tr[x].ch[k^1];
if(tr[x].ch[k^1])tr[tr[x].ch[k^1]].fa=y;
tr[y].fa=x;tr[x].ch[k^1]=y;
pushup(y);pushup(x);
}inline void splay(int x){
int y,z;
push(x);
while(!root(x)){
y=tr[x].fa,z=tr[y].fa;
if(!root(y))(tr[z].ch[0]==y)^(tr[y].ch[0]==x)?rotate(x):rotate(y);
rotate(x);
}pushup(x);
}inline void access(int x){for(int y=0;x;y=x,x=tr[x].fa){splay(x);tr[x].ch[1]=y;pushup(x);}}
inline void makeroot(int x){
access(x);splay(x);pushr(x);
}inline void split(int x,int y){
makeroot(x);
access(y);splay(y);
}inline void link(int x,int y){
makeroot(x);tr[x].fa=y;
}inline void cut(int x,int y){
makeroot(x);access(y);splay(y);
if(tr[y].ch[0]!=x||tr[x].ch[1])return;
tr[x].fa=tr[y].ch[0]=0;
pushup(x);
}inline int find(int x){
return x==bel[x]?x:bel[x]=find(bel[x]);
}
int main(){
scanf("%d",&n);//其它都是模板qwq……
for(int i=1;i<=n;i++)tr[i].sum=1,bel[i]=i,s[i]={i,i};//初始化
for(int i=1;i<=n;++i){
cin>>cg;
scanf("%d",&a);
if(cg[0]=='B'){
++opt;
if(a==-1)continue;
else{
int d=find(a);
link(opt,a);
bel[opt]=d;//并查集更新
split(opt,s[d].a);//每次合并点查询距离
int x1=tr[s[d].a].sum-1;
split(opt,s[d].b);
int x2=tr[s[d].b].sum-1;
if(dis[d]<x1)dis[d]=x1,s[d]={opt,s[d].a};//更新每次的最大距离,一起更新s结构体
if(dis[d]<x2)dis[d]=x2,s[d]={opt,s[d].b};
}
}else{
int d=find(a);
split(a,s[d].a);//s是a所连的两个点
int x1=tr[s[d].a].sum-1;//求当前距离,一个点连着两个点,求两次取最大
split(a,s[d].b);
int x2=tr[s[d].b].sum-1;
printf("%d\n",max(x1,x2));//取最大咯
}
}
return 0;//完结撒花~
}
有不当之处也还请看到的各位dalao指出。
6.13地理中考加油!
题解【[USACO18FEB]New Barns 】的更多相关文章
- P4271 [USACO18FEB]New Barns
题目 P4271 [USACO18FEB]New Barns 做法 这题很长见识啊!! 知识点:两棵树\((A,B)\)联通后,新树的径端点为\(A\)的径端点与\(B\)的径端点的两点 不断加边,那 ...
- Luogu P4271 [USACO18FEB]New Barns P
题意 给一个一开始没有点的图,有 \(q\) 次操作,每次为加点连边或者查询一个点到连通块内所有点的距离最大值. \(\texttt{Data Range}:1\leq q\leq 10^5\) 题解 ...
- [usaco18Feb] New Barns
题意 每次新建一个节点,并与一个已知节点连边.(或者不连).多次询问以某个已知点点出发的最远路径长度. 分析 显然,在任何时候图都是一个森林.由树的直径算法可知,与某点最远距的点必然是树的直径的一段. ...
- LCT[Link-Cut-Tree学习笔记]
部分摘抄于 FlashHu candy99 所以文章篇幅较长 请有足够的耐心(不是 其实不用学好splay再学LCT的-/kk (至少现在我平衡树靠fhq) 如果学splay的话- 也许我菜吧-LCT ...
- usaco 2002 月赛 Fiber Communications 题解
Description Farmer John wants to connect his N (1 <= N <= 1,000) barns (numbered 1..N) with a ...
- 线段树||BZOJ5194: [Usaco2018 Feb]Snow Boots||Luogu P4269 [USACO18FEB]Snow Boots G
题面:P4269 [USACO18FEB]Snow Boots G 题解: 把所有砖和靴子排序,然后依次处理每一双靴子,把深度小于等于它的砖块都扔线段树里,问题就转化成了求线段树已有的砖块中最大的砖块 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
随机推荐
- 入门的艰难——关于LR的使用
这年头做一件事真是TM不容易啊.做测试也很纠结,不是都说商业工具很强大么,我去,这个不支持那个不支持的,这还有什么搞头,还非要按照你说的这个版本的才行,高一点的就crash了,结果连最初级的录制脚本都 ...
- ACwing 你能回答这些问题吗(线段树求最大连续字段和)
给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“1 x y”,查询区间 [x,y] 中的最大连续子段和,即 maxx≤l≤r≤ymaxx≤l≤r≤y{∑ri=lA[i]∑i=l ...
- 实操ES6之Promise
箭头函数和this 写Promise的时候,自然而然会使用箭头函数的编写方式.箭头函数就是.Neter们熟知的lambda函数,已经被大部分主流语言支持,也受到了广大码农的交口称赞,但是Jser们却会 ...
- .Net在Windows上使用Jenkins做CI/CD的那些事
背景 最近入职了一家新公司,公司各个方面都让我非常的满意,我也怀着紧张与兴奋的心情入职后,在第一天接到了领导给我的第一个任务——把整个项目的依赖引用重新整理并实施项目的CI/CD. 本篇的重点主要分享 ...
- Python的链接数上升得太快了!足见Python之火!
- JAVA JDK 环境变量配置 入门详解 - 精简归纳
JAVA JDK 环境变量配置 入门详解 - 精简归纳 JERRY_Z. ~ 2020 / 9 / 13 转载请注明出处!️ 目录 JAVA JDK 环境变量配置 入门详解 - 精简归纳 一.为什么j ...
- First-blog:解决mybatis 用mysql进行模糊搜索时,查不了中文问题
如图:点击小字 按搜索时,出现乱码搜索不了 解决办法:出现乱码问题,一般无非两种 1.是数据库问题 2.是服务器问题 我在MySQL命令行搜索时,中文可以实现,说明时服务器问题 通过修改 tomcat ...
- oracle之三手工备份与恢复
手工备份与恢复 2.1 手工备份和恢复的命令 1)备份和还原都使用OS命令,如linux中的cp 2)恢复用sqlplus命令:recover 2.2 备份前要对数据库进行检查: 1) 检查需要备份的 ...
- 软件测试----xml文件介绍
软件测试 目录 软件测试 一.什么是XML?: 二.XML和HTML的差异: 三.XML的特点 1.XML可以自定义标签 2.XML必须包含根元素 如上所示, 3.XML标签对大小写敏感 4.XML ...
- Redis哨兵模式(sentinel)部署
1 主机环境 我这里使用的操作系统是centos 6.5,安装在vmware上,共三台. 主机名 IP 操作系统 用户名 安装目录 node1 192.168.1.101 centos 6.5 wxy ...