#P13539. [IOI 2025] Migrations

[IOI 2025] Migrations

题目描述

The Natural History Museum is investigating the migration patterns of dinosaurs in Bolivia. Paleontologists have discovered dinosaur footprints at NN different sites, numbered from 00 to N1N - 1 in order of decreasing age: site 00 contains the oldest footprints, while site N1N - 1 holds the youngest.

The dinosaurs migrated to each site (except site 00) from a specific, older site. For every site ii such that 1iN11 \leq i \leq N - 1, there exists exactly one older site P[i]P[i] (with P[i]<iP[i] < i) such that some dinosaurs migrated directly from site P[i]P[i] to site ii. A single older site may have been the source of migrations to multiple younger sites.

The paleontologists model each migration as an undirected connection between sites ii and P[i]P[i]. Note that for any two distinct sites xx and yy, it is possible to travel from xx to yy by traversing a sequence of connections. The distance between sites xx and yy is defined as the minimum number of connections required to travel from xx to yy.

For example, the following image displays a case with N=5N = 5 sites, where P[1]=0P[1] = 0, P[2]=1P[2] = 1, P[3]=2P[3] = 2, and P[4]=2P[4] = 2. One can travel from site 33 to site 44 through 22 connections, so the distance between them is 22.

The Museum aims to identify a pair of sites with the maximum possible distance.

Note that the pair is not necessarily unique: for instance, in the example above, both pairs (0,3)(0, 3) and (0,4)(0, 4) have a distance of 33, which is the maximum. In such cases, any of the pairs attaining the maximum distance is considered valid.

Initially, the values of P[i]P[i] are not known. The Museum sends a research team to visit the sites 1,2,,N11, 2, \ldots, N - 1, in order. Upon reaching each site ii (1iN11 \leq i \leq N - 1), the team performs both of the following actions:

  • Determines the value of P[i]P[i], that is, the source of migration to site ii.
  • Decides whether to send one message to the Museum or not to send any message from this site, based on the previously gathered information.

Messages are transmitted via an expensive satellite communication system, so each message must be an integer between 11 and 2000020000, inclusive. Additionally, the team is allowed to send at most 5050 messages in total.

Your task is to implement a strategy, through which:

  • The research team selects the sites from which messages are sent, along with the value of each message.
  • The Museum can determine a pair of sites with the maximum distance, based solely on the messages received from each site, knowing from which sites these messages were sent.

Sending large numbers through the satellite is costly. Your score will depend on both the highest integer sent and the total number of messages transmitted.

Implementation Details

You need to implement two procedures; one for the research team, and another one for the Museum.

The procedure you should implement for the research team is:

int send_message(int N, int i, int Pi)
  • NN: the number of sites with footprints.
  • ii: the index of the site the team is currently visiting.
  • P[i]P[i]: the value of P[i]P[i].
  • This procedure is called N1N - 1 times for each test case, in the order of i=1,2,...,N1i = 1, 2, ..., N - 1.

This procedure should return S[i]S[i] specifying the action taken by the research team at site ii:

  • S[i]=0S[i] = 0: the research team decides not to send a message from site ii.
  • 1S[i]200001 \leq S[i] \leq 20000: the research team sends the integer S[i]S[i] as the message from site ii.

The procedure you should implement for the Museum is:

std::pair<int,int> longest_path(std::vector<int> S)
  • SS: an array of length NN such that:
    • S[0]=0S[0] = 0.
    • For each 1iN11 \leq i \leq N - 1, S[i]S[i] is the integer returned by send_message(N, i, Pi).
  • This procedure is called exactly once for each test case.

This procedure should return a pair of sites (U,V)(U, V) with the maximum distance.

In the actual grading, a program that calls the above procedures is run exactly two times.

  • During the first run of the program:
    • send_message is called exactly N1N - 1 times.
    • Your program can store and retain information across successive calls.
    • The returned values (array SS) are stored by the grading system.
    • In some cases the behavior of the grader is adaptive. This means that the value of P[i]P[i] in a call to send_message may depend on the actions taken by the research team during the previous calls.
  • During the second run of the program:
    • longest_path is called exactly once. The only information available to longest_path from the first run is the array SS.

输入格式

The sample grader calls both send_message and longest_path in the same run, which is different from the actual grader.

Input format:

N
P[1] P[2] ... P[N-1]

输出格式

Output format:

S[1] S[2] ... S[N-1]
U V

Note that you can use the sample grader with any value of NN.

提示

Example

Let N=10000N = 10000. Consider the situation in which P[1]=0P[1] = 0, P[2]=1P[2] = 1, P[3]=2P[3] = 2, P[4]=2P[4] = 2, and P[i]=1P[i] = 1 for i>4i > 4.

Assume that the research team's strategy is to send the message 10V+U10 \cdot V + U whenever the pair of sites (U,V)(U, V) with the maximum distance changes as a result of a send_message call.

Initially, the pair with the maximum distance is (U,V)=(0,0)(U, V) = (0, 0). Consider the following sequence of calls during the first run of the program:

Procedure call (U,V)(U, V) Return value (S[i])(S[i])
send_message(10000, 1, 0) (0,1)(0,1) 1010
send_message(10000, 2, 1) (0,2)(0,2) 2020
send_message(10000, 3, 2) (0,3)(0,3) 3030
send_message(10000, 4, 2) 00

Note that in all the remaining calls the value of P[i]P[i] is 11. This means that the pair with the maximum distance does not change, and the team does not send any more messages.

Then, in the second run of the program, the following call is made:

longest_path([0, 10, 20, 30, 0, ...])

The Museum reads the last message sent by the research team, which is S[3]=30S[3] = 30, and deduces that (0,3)(0, 3) is the pair of sites with the maximum distance. Therefore, the call returns (0,3)(0, 3).

Note that this approach does not always make it possible for the Museum to determine the pair with the maximum distance correctly.

Subtasks and Scoring

Subtask Score Additional Constraints
1 30 Site 0 and some other site have a maximum distance among all pairs of sites.
2 70 No additional constraints.

Let ZZ be the maximum integer appearing in the array SS, and let MM be the number of messages sent by the research team.

In any of the test cases, if at least one of the following conditions hold, then the score of your solution for that test case will be 0 (reported as Output isn't correct in CMS):

  • At least one element in SS is invalid.
  • Z>20000Z > 20\,000 or M>50M > 50.
  • The return value of the call to procedure longest_path is incorrect.

Otherwise, your score for subtask 1 is calculated as follows:

Condition Score
9998Z200009998 \leq Z \leq 20000 10
102Z9997102 \leq Z \leq 9997 16
5Z1015 \leq Z \leq 101 23
Z4Z \leq 4 30

Your score for subtask 2 is calculated as follows:

Condition Score
5Z200005 \leq Z \leq 20000 and M50M \leq 50 3525log4000(Z5)35 - 25 \log_{4000} \left(\frac{Z}{5}\right)
Z4Z \leq 4 and 32M5032 \leq M \leq 50 40
Z4Z \leq 4 and 9M319 \leq M \leq 31 7030log4(M8)70 - 30 \log_{4} \left(\frac{M}{8}\right)
Z4Z \leq 4 and M8M \leq 8 70