洛谷 P3019 [USACO11MAR]会见点Meeting Place
题目背景
征求翻译。如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献。
题目描述
Bessie and Jonell are great friends. Since Farmer John scrambles where the cows graze every day, they are sometimes quite far from each other and can't talk.
The pastures and paths on FJ's farm form a 'tree' structure. Each pasture has exactly one distinct path to any other pasture, and each pasture (except pasture #1, the 'root') also has a single parent node.
Bessie and Jonell have decided that they will always meet at the closest pasture that that is both an ancestor of Jonell's pasture and of Bessie's pasture.
FJ created a map of his N (1 <= N <= 1,000) pastures (conveniently numbered 1..N) that tells the parent P_i (1 <= P_i <= N) of each pasture except pasture 1, which has no parent.
FJ has released his daily grazing schedule for the next M (1 <= M <= 1,000) days, so Bessie and Jonell are deciding where they should meet each day for gossip. On day k, Bessie is in pasture B_k (1 <= B_k <= N) and Jonell is in pasture J_k (1 <= J_k <= N).
Given a map and schedule, help Bessie and Jonell find their meeting places.
Consider, for example, the following farm layout:
Pasture Parent Pasture
[1] --------- ----------------
/ | \ 1 ---
/ | \ 2 1
[2] [3] [6] 3 1
/ | \ 4 2
/ | \ 5 8
[4] [8] [9] 6 1
/ \ 7 8
/ \ 8 6
[5] [7] 9 6
Here are the meeting places that Bessie and Jonell would choose
given a six day schedule of their initial grazing locations:
Bessie Jonell Meeting Place
-------- -------- ---------------
2 7 1
4 2 2
1 1 1
4 1 1
7 5 8
9 5 6
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and M
Lines 2..N: Line i contains a single integer that describes the parent of pasture i: P_i
- Lines N+1..N+M: Line k+N describes Bessie and Jonell's respective pastures with two space-separated integers: B_k and J_k
输出格式:
- Lines 1..M: Line j contains the meeting place Bessie and Jonell would use for line j+N of the input
输入输出样例
9 6
1
1
2
8
1
8
6
6
2 7
4 2
3 3
4 1
7 5
9 5
1
2
3
1
8
6
#include <vector>
#include <cstdio>
#define N 1005 using std::vector;
int n,m,dad[N][],dep[N],siz[N];
vector<int>G[N];
void dfs(int x)
{
dep[x]=dep[dad[x][]]+;
for(int i=;dad[x][i];++i)
dad[x][i+]=dad[dad[x][i]][i];
for(int i=;i<G[x].size();++i)
{
int v=G[x][i];
if(dad[x][]!=v)
{
dad[v][]=x;
dfs(v);
}
}
}
inline void swap(int &m,int &n)
{
int tmp=n;
n=m;
m=tmp;
}
int lca(int x,int y)
{
if(dep[x]>dep[y]) swap(x,y);
for(int i=;i>=;--i)
if(dep[dad[y][i]]>=dep[x]) y=dad[y][i];
if(x==y) return x;
for(int i=;i>=;--i)
if(dad[x][i]!=dad[y][i]) x=dad[x][i],y=dad[y][i];
return dad[x][];
}
int main()
{
scanf("%d%d",&n,&m);
for(int x,i=;i<=n;++i)
{
scanf("%d",&x);
G[x].push_back(i);
G[i].push_back(x);
}
dfs();
for(int x,y;m--;)
{
scanf("%d%d",&x,&y);
printf("%d\n",lca(x,y));
}
return ;
}
洛谷 P3019 [USACO11MAR]会见点Meeting Place的更多相关文章
- 洛谷P3018 [USACO11MAR]树装饰Tree Decoration
洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...
- 洛谷P3116 [USACO15JAN]约会时间Meeting Time
P3116 [USACO15JAN]约会时间Meeting Time 题目描述 Bessie and her sister Elsie want to travel from the barn to ...
- 洛谷——P3018 [USACO11MAR]树装饰Tree Decoration
P3018 [USACO11MAR]树装饰Tree Decoration 比较水的一道树上模拟水题,更新每个点的价值为以这个点为根的子树中的价值最小值,同时更新以每个节点为根的$sum$值,即以这个节 ...
- 洛谷 P5044 - [IOI2018] meetings 会议(笛卡尔树+DP+线段树)
洛谷题面传送门 一道笛卡尔树的 hot tea. 首先我们考虑一个非常 naive 的区间 DP:\(dp_{l,r}\) 表示区间 \([l,r]\) 的答案,那么我们考虑求出 \([l,r]\) ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- 洛谷P1710 地铁涨价
P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交 讨论 题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...
随机推荐
- MySQL 之 导出导入数据
导出数据库(sql脚本) mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u root -p --databases db_name > test ...
- Kolla多节点环境安装OVN
安装OVN组件 控制节点 ### 安装ovn-northd # wget https://copr.fedorainfracloud.org/coprs/leifmadsen/ovs-master/r ...
- OVS编译
下载源码 # git clone https://github.com/openvswitch/ovs.git # cd ovs # git checkout branch-2.8 下载依赖包 # y ...
- 2014-11-2 NOIP模拟赛1
Noip2009 团结模拟赛如题目理解困难,请自行阅读或参考样例.内存限制均为 256MB,时间限制均为 1s.出题人不会 故意 在题目中设置陷阱,但请自己注意程序的正确性.IO 文件名(.in/.o ...
- 洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon
P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W ( ...
- 洛谷P3190 [HNOI2007]神奇游乐园(插头dp)
传送门 大概是算第一道自己做出来的插头dp? (虽然都是照着抄板子的) (虽然有个地方死活没调出来最后只能看题解才发现自己错在哪里的) 我就当你们都会插头dp了…… 因为必须得是一条路径,所以扫描线上 ...
- Js $.merge() 函数(合并两个数组内容到第一个数组)
定义和用法 $.merge() 函数用于合并两个数组内容到第一个数组. 语法 $.merge( first, second ) 参数 描述 first Array类型 第一个用于合并的数组,合并后 ...
- 数组Array的相关操作。
一 数组的对象(元素): 1. 数字, 2 .字符串 3 变量 4. 函数 .... 二 数组的创建 1 var arrayObj = new Array(); var a =new Array(si ...
- UVa 11168(凸包、直线一般式)
要点 找凸包上的线很显然 但每条线所有点都求一遍显然不可行,优化方法是:所有点都在一侧所以可以使用直线一般式的距离公式\(\frac{|A* \sum{x}+B* \sum{y}+C*n|}{\sqr ...
- WAMP3.1.10/Apache 设置站点根目录
网上的资料很多都不起作用了,都是说修改 WAMP\bin\apache\apache2.4.27\conf\httpd.conf 文件,但是修改了之后完全没有效果!! 最后,稀里糊涂的把 WAMP\b ...