题目背景

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

题目描述

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. AngularJs(Part 2)

    I am still tired to translate these into Chinese. but who cares? i write these posts just for myself ...

  2. Hash表的实现

    #include "stdafx.h" #include <iostream> #include <exception> using namespace s ...

  3. POJ 3281 Dining (网络流之最大流)

    题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给 ...

  4. MFC——4个基本类中的成员函数介绍

    09121852 杜军 机械设计及理论 1. CMainFrame ActivateFrame使框架对用户可视并可用 CalcWindowRect每当主框架窗口的客户区尺寸发生变化或控制条的位置发生变 ...

  5. NativeScript官方书籍:1.为什么选择nativescript

    1.为什么选择nativescript 本章介绍 什么是NativeScript nativescript对于移动开发世界意味着什么 NativeScript工作原理 在早期的移动应用程序(前iPho ...

  6. 蓝桥杯 正则问题(dfs)

    1607: 正则问题 时间限制: 1 Sec  内存限制: 256 MB提交: 34  解决: 13[提交][状态][讨论版] 题目描述 考虑一种简单的正则表达式:只由 x ( ) | 组成的正则表达 ...

  7. Razor的主版页面框架

    类似于2.0版本中的MasterPage主版页面框架,不过mvc3.0推出的RazorView内建的主版页面语法与原本的webFormview的MasterPage相差甚远   1,Razor的页面执 ...

  8. IT兄弟连 JavaWeb教程 使用Servlet实现在页面中显示随机数

    在com.xdl.servlet包下定义RandomServlet类并HttpServlet类,在该类中生成随机数并发送给客户端.RandomServlet类详细代码如下: package com.x ...

  9. SpringMVC入门 bug集锦X2

    package cn.itcast.converter; import org.springframework.core.convert.converter.Converter; import jav ...

  10. mac系统之前做过Windows8系统,可能移除时没有通过boot camp助理移除,所以想要再安装windows系统时,点击boot camp助理的继续,弹出启动磁盘不能被分区或恢复单分区。

    因为把bootcamp分区抹掉,卸载,装载,点减号,合并成单分区,一直是操作失败.为了通过boot camp安装上Windows系统,索性重新安装mac ox系统,重新分区.重新开机,按住comman ...