[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 ...
随机推荐
- 【多校训练2】HDU 6047 Maximum Sequence
http://acm.hdu.edu.cn/showproblem.php?pid=6047 [题意] 给定两个长度为n的序列a和b,现在要通过一定的规则找到可行的a_n+1.....a_2n,求su ...
- java邮件工具类【最终版】
http://www.xdemo.org/java-mail/ 对比链接中,添加了抄送和暗抄送功能(已解决,如图代码:抄送不能多个用户,会报错,未解之谜) sendHtmlmail方法可以发送附件以及 ...
- 反编译sencha toucha打包的apk文件,修改应用名称支持中文以及去除应用标题栏
一.去除安卓应用标题栏 sencha touch打包android安装包,去掉标题栏titlebar的简单方法 (有更复杂更好的方法,参看"二.利用反编译修改apk的应用名称为中文" ...
- 最长回文(hdu 3068)
Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有 ...
- 售货员的难题(codevs 2596)
题目描述 Description 某乡有n个村庄(1<n<=15),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且A村到B村与B村到A村 ...
- 【BZOJ1403】Divisibility Testing(数论)
题意: 思路: #include<cstdio> #include<cstdlib> #include<algorithm> #include<map> ...
- linux 报错:E: Package 'libmemcached' has no installation candidate
linux 报错:E: Package 'libmemcached' has no installation candidate 网上查资料说是软件安装源没有这个软件,需要添加软件源. 1.备份源列表 ...
- 怎么删除"自豪地采用WordPress"
wordpress刚刚安装完毕,打开默认的主页,会发现底部有这样的一行文字:“自豪地采用WordPress”.当然了,我们做一个网站,不一定需要这些文字,我们可以删除或者修改这些文字.今天,小编就来教 ...
- 寒武纪camp Day6
补题进度:10/10 A(树形dp) 略 B(dp) 题意: 给出一个n个关键节点的机械手臂,最开始是竖直的,即关键点在二维平面上的坐标分别是(0,0) (0,100) (0,200) (0,300) ...
- samba.conf (香港中华厨房有限公司实例)
# Sample configuration file for the Samba suite for Debian GNU/Linux. # # This is the main Samba con ...