题目背景

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

题目描述

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. android fragment ontouch 事件

    由于fragment是存在于activity之中的,所以触摸事件会被activity首先得到. 为了在fragment中执行ontouch方法,需要做如下处理: mFragment = new Tab ...

  2. POJ - 1458 Common Subsequence DP最长公共子序列(LCS)

    Common Subsequence A subsequence of a given sequence is the given sequence with some elements (possi ...

  3. Git 分支管理 BUG分支

    BUG分支  廖雪峰 软件开发中,bug就像家常便饭一样.有了bug就需要修复, 在Git中,由于分支是如此的强大,所以, 每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支 ...

  4. surface shader相关参数,命令

    https://docs.unity3d.com/Manual/SL-SurfaceShaders.html 说明: 注意下surfaceshader相关开关选项,input结构体全部可用参数 goo ...

  5. RenderTexture

    https://docs.unity3d.com/Manual/class-RenderTexture.html Size: 图片像素尺寸,这个size可以直接在脚本里通过width和height动态 ...

  6. Hyperledger Fabric java chaincode 中文乱码问题

    开发java chaincode过程中遇到一个中文乱码的问题.都是官方的demo,请求的sdk是用java写的,部署的chaincode有两种选择(不考虑node),一种go语言写的chaincode ...

  7. 2014-10-4 NOIP模拟赛

    1.某种密码(password.*) 关于某种密码有如下描述:某种密码的原文A是由N个数字组成,而密文B是一个长度为N的01数串,原文和密文的关联在于一个钥匙码KEY.若KEY=∑▒[Ai*Bi],则 ...

  8. ios 实现 cell 的动态高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { Mes ...

  9. C# IE环境 - 重置IE(WshShell & Rundll32)

    前言 IE问题: 如何重置IE选项? 通过修改注册表,理论上是可行的.前提是你知道重置IE时,有多少注册表项要更改. 如果能直接调用IE的重置设置窗口,并执行重置,能完美重置IE. WshShell ...

  10. map练习小例题

    "fdgavcbsacdfs" 获取该字符串中,每一个字母出现的次数. 要求打印结果是:a(2)b(1)...; 思路: 对于结果的分析发现,字母和次数之间存在着映射关系.而且这种 ...