[Usaco2018 Feb] New Barns
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=5192
[算法]
维护树的直径,在树上离一个点最远的点一定是一条直径的端点。
在直径为(x , y)的树上加入一个叶子结点z,则新的直径必然为(x , y) , (x , z) , (y , z)中的一条 , 问题转化为询问树上两点距离 , 倍增即可 , 时间复杂度 :O(MlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + ;
const int MAXLOG = ; int n , total , k;
int depth[MAXN] , x[MAXN] , y[MAXN] , belong[MAXN];
int anc[MAXN][MAXLOG];
char op[]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline int lca(int u,int v)
{
if (depth[u] > depth[v]) swap(u , v);
for (int i = MAXLOG - ; i >= ; i--)
{
if (depth[anc[v][i]] >= depth[u])
v = anc[v][i];
}
if (u == v) return u;
for (int i = MAXLOG - ; i >= ; i--)
{
if (anc[u][i] != anc[v][i])
u = anc[u][i] , v = anc[v][i];
}
return anc[u][];
}
inline int dist(int x,int y)
{
return depth[x] + depth[y] - * depth[lca(x , y)];
}
inline void update(int u)
{
int tmp = belong[u];
int nx , ny , nd;
if (dist(u , x[tmp]) > dist(u , y[tmp]))
{
nd = dist(u , x[tmp]);
nx = u;
ny = x[tmp];
} else
{
nd = dist(u , y[tmp]);
nx = u;
ny = y[tmp];
}
if (nd > dist(x[tmp] , y[tmp]))
{
x[tmp] = nx;
y[tmp] = ny;
}
}
inline int query(int k)
{
int tmp = belong[k];
return max(dist(k , x[tmp]) , dist(k , y[tmp]));
} int main()
{ int Q;
scanf("%d",&Q);
while (Q--)
{
scanf("%s%d",&op , &k);
if (op[] == 'B')
{
++n;
if (k == -)
{
belong[n] = ++total;
anc[n][] = ;
depth[n] = ;
x[total] = y[total] = n;
} else
{
depth[n] = depth[k] + ;
anc[n][] = k;
for (int i = ; i < MAXLOG; i++)
anc[n][i] = anc[anc[n][i - ]][i - ];
belong[n] = belong[k];
}
update(n);
} else printf("%d\n",query(k));
} return ; }
[Usaco2018 Feb] New Barns的更多相关文章
- bzoj5192: [Usaco2018 Feb]New Barns
不想写看zory大佬 #include<cstdio> #include<iostream> #include<cstring> #include<cstdl ...
- BZOJ5196: [Usaco2018 Feb]Taming the Herd(DP暴力)
5196: [Usaco2018 Feb]Taming the Herd Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 78 Solved: 71[ ...
- BZOJ5194: [Usaco2018 Feb]Snow Boots(排序&set)(可线段树优化)
5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 102 Solved: 79[Subm ...
- 线段树||BZOJ5194: [Usaco2018 Feb]Snow Boots||Luogu P4269 [USACO18FEB]Snow Boots G
题面:P4269 [USACO18FEB]Snow Boots G 题解: 把所有砖和靴子排序,然后依次处理每一双靴子,把深度小于等于它的砖块都扔线段树里,问题就转化成了求线段树已有的砖块中最大的砖块 ...
- Bzoj[Usaco2018 Feb]5194 Snow Boots(线段树)
Description 到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1…N,第i块地砖上积了fi英尺的雪 .在Farmer John的农舍的地窖中,总共有B双靴子,编号为1… ...
- bzoj 5195: [Usaco2018 Feb]Directory Traversal【树形dp】
注意到目录是一颗树结构,然后就简单了,预以1为根的处理出dis[u]为以这个点为根,到子树内的目录总长,si为子树内叶子数 第二遍dfs换根即可 #include<iostream> #i ...
- [Usaco2018 Feb]Snow Boots
Description 到冬天了,这意味着下雪了!从农舍到牛棚的路上有N块地砖,方便起见编号为1-N,第i块地砖上积了fi英尺的雪.在Farmer John的农舍的地窖中,总共有B双靴子,编号为1-B ...
- bzoj5194: [Usaco2018 Feb]Snow Boots
还真是.. 就是 一个被不点名批评的垃圾骗分暴力选手被普及难度的省选信心(??)模拟赛艹爆的题解 的t3嘛... #include<cstdio> #include<iostream ...
- BZOJ 5194--[Usaco2018 Feb]Snow Boots(STL)
5194: [Usaco2018 Feb]Snow Boots Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: 61[Submi ...
随机推荐
- HDU 1874 最直接的最短路径问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...
- 可以学习相关框架【转:https://testerhome.com/topics/6283】
https://testerhome.com/topics/6283 单元测试方面(Java): Junit:本来想用我熟悉的testng,但是开发的同学说测试springmvc只能用Junit.所以 ...
- Linux(3):linux目录结构
查看系统版本: [root@neo ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@neo ~]# uname -r 2.6. ...
- [转]SQL SERVER数据库还原的方法
SQL SERVER数据库还原的方法 在SQL SERVER 2005下还原数据库 1.新建数据库A,右键还原数据库,此时目标数据库为A,选择备份 文件B_db_201311040200.BAK,还原 ...
- [NOIP2002] 提高组 洛谷P1033 自由落体
题目描述 在高为 H 的天花板上有 n 个小球,体积不计,位置分别为 0,1,2,….n-1.在地面上有一个小车(长为 L,高为 K,距原点距离为 S1).已知小球下落距离计算公式为 d=1/2*g* ...
- OSGI是什么
OSGI(Open Services Gateway Initiative),或者通俗点说JAVA动态模块系统,定义了一套模块应用开发的框架.OSGI容器实现方案如Knopflerfish, Equi ...
- 【APUE】信号
一.信号概念 信号都被定义为正整数,不存在编号为0的信号. 信号是异步事件的经典实例.产生信号的事件对进程而言是随机出现的,进程不能简单地测试一个变量来判别是否出现了一个信号,而是必须告诉内核在此信号 ...
- CSS浮动通俗讲解
首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...
- [RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS
When subscribers create new "inner" sources and subscriptions, you run the risk of losing ...
- pagePiling.js - 创建美丽的全屏滚动效果
在线演示 在线演示 本地下载 全屏滚动效果是近期很流行的网页设计形式,带给用户良好的视觉和交互体验. pagePiling.js 这款jQuery插件能够帮助前端开发者轻松实现这样的效果.支持全部的主 ...