#P3533. [POI2012] RAN-Rendezvous

    ID: 2592 Type: RemoteJudge 1500ms 256MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2012倍增POI最近公共祖先,LCA

[POI2012] RAN-Rendezvous

题目描述

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 xix_i and yiy_i such that if the ii-th couple follows respectively: he xix_i and she yiy_i corridors marked with arrows,then they will meet in a single chamber of the cave max(xi,yi)max(x_i,y_i) is minimal,subject to above min(xi,yi)min(x_i,y_i) is minimal,if above conditions do not determine a unique solution, then the woman should cover smaller distance (xiyix_i\ge y_i).

It may happen that such numbers xix_i and yiy_i do not exist - then let xi=yi=1x_i=y_i=-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...

输入格式

In the first line of the standard input there are two positive integers nn and kk(1n,k500 0001\le n,k\le 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,k2 000n,k\le 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 xi,yix_i,y_i, separated by a single space.

题目大意

题目描述

译自 POI 2012 Stage 1. 「Rendezvous

给定一个有 nn 个顶点的有向图,每个顶点有且仅有一条出边。每次询问给出两个顶点 aia_ibib_i,求满足以下条件的 xix_iyiy_i

  • 从顶点 aia_i 沿出边走 xix_i 步与从顶点 bib_i 沿出边走 yiy_i 步到达的顶点相同。
  • max(xi,yi)\max(x_i, y_i) 最小。
  • 满足以上条件的情况下 min(xi,yi)\min(x_i, y_i) 最小。
  • 如果以上条件没有给出一个唯一的解,则还需要满足 xiyix_i \ge y_i.

如果不存在这样的 xix_iyiy_i,则 xi=yi=1x_i = y_i = -1.

输入格式

第一行两个正整数 nnkk1n500 000,1k500 0001 \le n \le 500\ 000,1 \le k \le 500\ 000),表示顶点数和询问个数。

接下来一行 nn 个正整数,第 ii 个数表示 ii 号顶点出边指向的顶点。

接下来 kk 行表示询问,每行两个整数 aia_ibib_i.

输出格式

对每组询问输出两个整数 xix_iyiy_i.

数据范围

对于 40%40\% 的数据,n2000,k2000n \le 2000,k \le 2000.

对于 100%100\% 的数据,1n500 000,1k500 0001 \le n \le 500\ 000,1 \le k \le 500\ 000.

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