题目描述

Byteasar is a ranger who works in the Arrow Cave - a famous rendezvous destination among lovers.

The cave consists of nn chambers connected with one-way corridors.

In each chamber exactly one outgoing corridor is marked with an arrow.

Every corridor leads directly to some (not necessarily different) chamber.

The enamoured couples that agree to meet in the Arrow Cave are notorious for forgetting to agree upon specific chamber, and consequently often cannot find their dates.

In the past this led to many mix-ups and misunderstandings\dots But ever since each chamber is equipped with an emergency telephone line to the ranger on duty, helping the enamoured find their dates has become the rangers' main occupation.

The rangers came up with the following method.

Knowing where the enamoured are, they tell each of them how many times they should follow the corridor marked with an arrow in order to meet their date.

The lovers obviously want to meet as soon as possible - after all, they came to the cave to spend time together, not to wander around alone!

Most rangers are happy to oblige: they do their best to give each couple a valid pair of numbers such that their maximum is minimal.

But some rangers, among their numbers Byteasar, grew tired of this extracurricular activity and ensuing puzzles. Byteasar has asked you to write a program that will ease the process. The program, given a description of the cave and the current location of kk couples, should determine kk pairs of numbers x_ixi​ and y_iyi​ such that if the ii -th couple follows respectively: he x_ixi​ and she y_iyi​ corridors marked with arrows,then they will meet in a single chamber of the cave max(x_i,y_i)max(xi​,yi​) is minimal,subject to above min(x_i,y_i)min(xi​,yi​) is minimal,if above conditions do not determine a unique solution, then the woman should cover smaller distance ( x_i\ge y_ixi​≥yi​ ).

It may happen that such numbers x_ixi​ and y_iyi​ do not exist - then let x_i=y_i=-1xi​=yi​=−1 . Note that it is fine for several couples to meet in a single chamber. Once the lovers have found their dates, they will be happy to lose themselves in the cave again...

给定一棵内向森林,多次给定两个点a和b,求点对(x,y)满足:

1.从a出发走x步和从b出发走y步会到达同一个点

2.在1的基础上如果有多解,那么要求max(x,y)最小

3.在1和2的基础上如果有多解,那么要求min(x,y)最小

4.如果在1、2、3的基础上仍有多解,那么要求x>=y

输入输出格式

输入格式:

In the first line of the standard input there are two positive integers nn and kk ( 1\le n,k\le 500\ 0001≤n,k≤500 000 ), separated by a single space, that denote the number of chambers in the Arrow Cave and the number of couples who want to find their dates, respectively.

The chambers are numbered from 1 to nn , while the enamoured couples are numbered from 1 to kk .

The second line of input contains nn positive integers separated by single spaces:

the ii -th such integer determines the number of chamber to which the corridor marked with an arrow going out of chamber ii leads.

The following kk lines specify the queries by the separated couples. Each such query consists of two positive integers separated by a single space - these denote the numbers of chambers where the lovers are - first him, then her.

In the tests worth 40% of the total points it additionally holds that n,k\le 2\ 000n,k≤2 000 .

输出格式:

Your program should print exactly kk lines to the standard output, one line per each couple specified in the input:

the ii -th line of the output should give the instructions for the ii -th couple on the input.

I.e., the ii -th line of output should contain the integers x_i,y_ixi​,yi​ , separated by a single space.

输入输出样例

输入样例#1: 复制

12 5
4 3 5 5 1 1 12 12 9 9 7 1
7 2
8 11
1 2
9 10
10 5

输出样例#1: 复制

2 3
1 2
2 2
0 1
-1 -1

说明

给定一棵内向森林,多次给定两个点a和b,求点对(x,y)满足:

1.从a出发走x步和从b出发走y步会到达同一个点

2.在1的基础上如果有多解,那么要求max(x,y)最小

3.在1和2的基础上如果有多解,那么要求min(x,y)最小

4.如果在1、2、3的基础上仍有多解,那么要求x>=y

题解:

n个点n条边保证了它是一个基环树(每个连通块都有唯一一个环)

然后转自PoPoQQQ:

如果a和b不在同一棵内向树上,显然无解,否则一定有解

定义根为从一个点出发能走到的第一个环上点,如果a和b的根相同,则到达LCA是最优的

否则到达的点一定是a的根和b的根中的一个,两种情况都计算一下,取最优解即可

《倍增LCA万年写不对系列》

题外话:

调了半天发现连样例都有好几个不对,又输出了半天发现是a和b的根相同的情况炸了。

怎么可能?!

然后我眼睛一斜:

我艹!!!

然后闷声改完,一次ac。

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m,n1,a[1000001],g[1000001],dep[1000001],rt[1000001],size[1000001],pos[1000001],f[500001][21],ha[1000001];
void dfs(int x){//printf("%d\n",x);int i,j=1;g[x]=n1;if(g[a[x]]==n1){for(i=x;j==1||i!=x;i=a[i],j++){ha[i]=n1;pos[i]=j;rt[i]=i;dep[i]=1;}size[n1]=j-1;return;}if(!g[a[x]])dfs(a[x]);if(!pos[x]){//printf("%d %d\n",x,a[x]);f[x][0]=a[x];dep[x]=dep[a[x]]+1;rt[x]=rt[a[x]];}
}
int lca(int x,int y){int i;//printf("%d\n",x);if(dep[x]<dep[y])swap(x,y);for(i=20;i>=0;i--)if(dep[f[x][i]]>=dep[y])x=f[x][i];//printf("%d %d %d %d\n",x,y,dep[x],dep[y]); if(x==y)return x;for(i=20;i>=0;i--)if(f[x][i]!=f[y][i]){x=f[x][i];y=f[y][i];}return f[x][0];
}
int go(int x,int y,int z){if(pos[y]>pos[x])return pos[y]-pos[x];else return pos[y]+size[z]-pos[x];
}
void solve(int a,int b){int x1,y1,x2,y2,x;if(g[rt[a]]!=g[rt[b]]){printf("-1 -1\n");return;}if(rt[a]==rt[b]){x=lca(a,b);printf("%d %d\n",dep[a]-dep[x],dep[b]-dep[x]);return;}//printf("%d %d %d %d\n",a,b,rt[a],rt[b]);x1=dep[a]-1+go(rt[a],rt[b],g[rt[a]]);y1=dep[b]-1;x2=dep[a]-1;y2=dep[b]-1+go(rt[b],rt[a],g[rt[a]]);if(max(x1,y1)<max(x2,y2)){printf("%d %d\n",x1,y1);}else if(max(x1,y1)>max(x2,y2))printf("%d %d\n",x2,y2);else if(min(x1,y1)<min(x2,y2))printf("%d %d\n",x1,y1);else if(min(x1,y1)>min(x2,y2))printf("%d %d\n",x2,y2);else printf("%d %d\n",max(x1,y1),min(x1,y1));
}
int main(){int i,j,t,k;scanf("%d%d",&n,&m);for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++)if(!g[i]){n1++;//    printf("%d %d\n",i,n1);dfs(i);}//printf("-1\n");for(j=1;j<=20;j++)for(i=1;i<=n;i++)f[i][j]=f[f[i][j-1]][j-1]; while(m--){scanf("%d%d",&t,&k);solve(t,k);}
}

洛谷P3533 [POI2012]RAN-Rendezvous相关推荐

  1. 洛谷 P3539 [POI2012]ROZ-Fibonacci Representation 解题报告

    P3539 [POI2012]ROZ-Fibonacci Representation 题意:给一个数,问最少可以用几个斐波那契数加加减减凑出来 多组数据10 数据范围1e17 第一次瞬间yy出做法, ...

  2. BZOJ2801/洛谷P3544 [POI2012]BEZ-Minimalist Security(题目性质发掘+图的遍历+解不等式组)...

    题面戳这 化下题面给的式子: \(z_u+z_v=p_u+p_v-b_{u,v}\) 发现\(p_u+p_v-b_{u,v}\)是确定的,所以只要确定了一个点\(i\)的权值\(x_i\),和它在同一 ...

  3. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  4. 洛谷 P1142 轰炸

    洛谷 P1142 轰炸 题目描述 "我该怎么办?"飞行员klux向你求助. 事实上,klux面对的是一个很简单的问题,但是他实在太菜了. klux要想轰炸某个区域内的一些地方,它们 ...

  5. 洛谷 P1387 最大正方形

    P1387 最大正方形 题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=10 ...

  6. 洛谷P2763 试题库问题

    题目:https://www.luogu.org/problemnew/show/P2763 题目描述 «问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性. ...

  7. 动态规划——洛谷_P1057传球游戏

    题目: 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏.游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球, ...

  8. 洛谷P1417 烹调方案

    洛谷P1417 烹调方案 如果是一般的01背包的话 选的先后是没关系的 但是这题选的先后是有关系的,因为他的价值是随着时间而变化的, 而你的01背包是做不到先选2再选1的 那么我们就跟国王游戏一样 用 ...

  9. 记忆优化搜索(简单题)(洛谷P3183 [HAOI2016]食物链 )( P5635 【CSGRound1】天下第一 )

    昨天做了蓝桥杯的时候,发现自己对于记忆优化搜索甚是不熟悉,所以今天随便找了几个基础题做做,顺便写下两片题解,顺便用了一下devc++敲的代码,发现没有代码补全真的可以说是灰常难受了... 洛谷P318 ...

  10. 洛谷 - 试炼场(全部题目备份)

    整理的算法模板合集: ACM模板 目录 1.新手村 1 - 1 洛谷的第一个任务 1 - 2 顺序与分支 1 - 3 循环!循环!循环! 1 - 4 数组 1 - 5 简单字符串 1 - 6 过程函数 ...

最新文章

  1. 驱动华为_再补齐一个短板,华为正式宣布进军屏幕驱动行业
  2. Modular Multiplicative Inverse(模乘逆元)
  3. 【Spring Cloud中文社区】正式启动
  4. redis持久化之rdb篇
  5. 十六位顶尖专家齐聚,解密阿里云最新核心技术竞争力!
  6. 机房收费系统重构版:那个系统我们一起遇到的问题
  7. [转载] [Python图像处理] 二十二.Python图像傅里叶变换原理及实现
  8. ceph客户端使用_Ceph 基础篇 认证
  9. 黎活明给程序员的忠告
  10. Node.js 应用开发详解07 CPU 过载保护设计:如何在服务层面确保系统稳定?
  11. Golang学习——error错误处理浅谈
  12. python帝国cms_用python 发 帝国cms 文章
  13. 星城,你准备好了么?Greenplum走进长沙技术研讨会
  14. Unity报错:InvalidOperationException:You are tring to read lnput using the UnityEngine. ……的解决办法
  15. java如何找出勾股数组_勾股数组 学习笔记
  16. unity 关于搜索
  17. 洛谷 P3426 [POI2005]SZA-Template
  18. MATLAB中repmat函数用法
  19. 计算机自定义桌面,电脑桌面图标自定义摆放,如何自定义桌面图标
  20. 全球与中国PH传感器盒市场深度研究分析报告

热门文章

  1. 再见了, 达叔!我用Python回顾一代喜剧大师203部作品,太经典了!
  2. Aspose.word Java实现html转word,word转html
  3. 将文件夹中的图片批量分割
  4. 锋迷商城spring-vue项目流程和笔记
  5. Java中间件mock_JAVA中间件Diamond整理
  6. 【C++要笑着学】类的默认成员函数详解 | 构造函数 | 析构函数 | 构造拷贝函数
  7. MySQL和Navicat基本使用
  8. Visual Studio 番茄助手 安装问题
  9. Winform中HelpButton的用法总结
  10. grub4dos命令和grldr引导文件介绍