题目背景

征求翻译。如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献。

题目描述

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

输入输出样例

输入样例#1:

9 6
1
1
2
8
1
8
6
6
2 7
4 2
3 3
4 1
7 5
9 5
输出样例#1:

1
2
3
1
8
6
 
lca模板题 
#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的更多相关文章

  1. 洛谷P3018 [USACO11MAR]树装饰Tree Decoration

    洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...

  2. 洛谷P3116 [USACO15JAN]约会时间Meeting Time

    P3116 [USACO15JAN]约会时间Meeting Time 题目描述 Bessie and her sister Elsie want to travel from the barn to ...

  3. 洛谷——P3018 [USACO11MAR]树装饰Tree Decoration

    P3018 [USACO11MAR]树装饰Tree Decoration 比较水的一道树上模拟水题,更新每个点的价值为以这个点为根的子树中的价值最小值,同时更新以每个节点为根的$sum$值,即以这个节 ...

  4. 洛谷 P5044 - [IOI2018] meetings 会议(笛卡尔树+DP+线段树)

    洛谷题面传送门 一道笛卡尔树的 hot tea. 首先我们考虑一个非常 naive 的区间 DP:\(dp_{l,r}\) 表示区间 \([l,r]\) 的答案,那么我们考虑求出 \([l,r]\) ...

  5. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  6. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

  7. 洛谷P1108 低价购买[DP | LIS方案数]

    题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...

  8. 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP

    题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...

  9. 洛谷P1710 地铁涨价

    P1710 地铁涨价 51通过 339提交 题目提供者洛谷OnlineJudge 标签O2优化云端评测2 难度提高+/省选- 提交  讨论  题解 最新讨论 求教:为什么只有40分 数组大小一定要开够 ...

随机推荐

  1. Auto Layout Guide----(二)-----Auto Layout Without Constraints

    Auto Layout Without Constraints 没有约束的自动布局 Stack views provide an easy way to leverage the power of A ...

  2. FZU2216【二分】

    题意: 百度. 思路: 一个连续数组111222233344444555666的每一个起伏转折即需要一张万能牌. 然后二分一下得最长区间. #include<cstdio> #includ ...

  3. Picture poj1177

    Picture Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 12265   Accepted: 6484 Descript ...

  4. 联盟链FISCO BCOS权限控制一览

    FISCO BCOS是完全开源的联盟区块链底层技术平台,由金融区块链合作联盟(深圳)(简称金链盟)成立开源工作组通力打造.开源工作组成员包括博彦科技.华为.深证通.神州数码.四方精创.腾讯.微众银行. ...

  5. snipaste截图软件

  6. CentOS6.7 i686上安装JDK7

    内核版本: [root@heima01 java]# uname -a Linux heima01 2.6.32-573.el6.i686 #1 SMP Thu Jul 23 12:37:35 UTC ...

  7. 手机APP开发(安卓、IOS)logo图标在线生成工具上线啦。

    网址:http://www.bejson.com/ui/create_logo/ 您只需要上传一张2M以内的jpg或png图片.然后输入验证码点击提交后,会自动的处理并生成一个压缩包下载. 目前支持2 ...

  8. Codeforces 190C(模拟)

    坑点 不记它难解我心头之恨-- WA1:不要读错题Orz,顺序是按它给定的.那就是个类似栈的东西,重点在于输出. 然而我输出很快就模拟对了-- WA2:数据:1 int.我日了不看数据我真的去de模拟 ...

  9. HDU-1556-Color the ball (线段树和差分数组两种解法)

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...

  10. [RDL]多级占比做法

    先添加[店铺],然后,对[店铺]添加父组,记得勾选[添加组头] 然后直接删除[区域2],[省份2] 添加到[店铺列] [区域]行,生意额占比表达式:=sum(Fields!生意额.Value)/Sum ...