#P6766. [APIO2020] 有趣的旅途

    ID: 5787 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2020APIO交互题Special JudgeO2优化

[APIO2020] 有趣的旅途

Description

In the largest theme park in Jakarta, there are NN attractions, numbered from 00 to N1N - 1. These attractions are connected by N1N - 1 bidirectional roads, and between any two attractions there is a unique simple path using these roads. The roads are numbered from 00 to N2N - 2. Road ii connects attraction A[i]A[i] and attraction B[i]B[i], and traversing this road takes one hour. To avoid congestion, each attraction is connected to at most three roads.

You want to find a touring route such that every attraction is visited exactly once. You think it is very boring if you have to pass too many roads when moving from one attraction to the next. To find an interesting route, you plan the visiting order so that the time needed to reach the next attraction is not greater than the time needed to reach the previous attraction. In other words, you want to find a sequence P[0],P[1],,P[N1]P[0], P[1], \dots, P[N - 1] that contains every integer from 00 to N1N - 1 exactly once, and for 0<i<N10 < i < N - 1, the time needed to go from attraction P[i]P[i] to attraction P[i+1]P[i + 1] is not greater than the time needed to go from attraction P[i1]P[i - 1] to attraction P[i]P[i].

You do not have the complete map of the park, so you must make some queries to the information center to find an interesting route. You can make at most QQ queries. Each query provides two parameters XX and YY, where 0X,Y<N0 \leq X, Y < N. Each query is one of the following:

  • How many hours are needed to travel from attraction XX to attraction YY. In particular, if X=YX = Y, the answer is 00.

  • How many attractions ZZ satisfy that, when you want to travel from attraction XX to attraction ZZ, you must pass through attraction YY. Attraction YY is included in the count. In particular, if X=YX = Y, the answer is NN.

You must implement the function createFunTour:

  • createFunTour(N, Q) - This function will be called by the grader exactly once.
    • NN: an integer representing the number of attractions.
    • QQ: an integer representing the maximum number of queries.
    • This function may call the following two interactive functions:
      • hoursRequired(X, Y)
        • XX: an integer representing the index of the first attraction.
        • YY: an integer representing the index of the second attraction.
        • This function returns an integer representing the number of hours needed to travel from attraction XX to attraction YY.
        • If the value of XX or YY is not in the range 00 to N1N - 1, this test will be judged as Wrong Answer.
      • attractionsBehind(X, Y)
        • XX: an integer representing the index of the first attraction.
        • YY: an integer representing the index of the second attraction.
        • This function returns an integer representing how many attractions ZZ satisfy that, when you want to travel from attraction XX to attraction ZZ, you must pass through attraction YY.
        • If the value of XX or YY is not in the range 00 to N1N - 1, this test will be judged as Wrong Answer.
    • This function must return an integer sequence of length NN, representing the visiting order of attractions that you found.

Input Format

The sample grader reads input in the following format:

N Q
A[0] B[0]
A[1] B[1]
.
.
.
A[N-2] B[N-2]

Output Format

If createFunTour correctly returns a sequence that satisfies the statement, and the total number of calls to hoursRequired and attractionsBehind does not exceed QQ, then the sample grader will output the sequence returned by createFunTour. Otherwise, the sample grader will output an error message.

7 400000
0 1
0 5
0 6
1 2
1 4
2 3

3 6 4 5 2 0 1

Hint

In the example in the figure below, N=7N = 7, Q=400000Q = 400 000, A=[0,0,0,1,1,2]A = [0, 0, 0, 1, 1, 2], and B=[1,5,6,2,4,3]B = [1, 5, 6, 2, 4, 3].

The grader will call createFunTour(7, 400000).

  • If you query hoursRequired(3, 5), the function will return 44.
  • If you query hoursRequired(5, 4), the function will return 33.
  • If you query attractionsBehind(5, 1), the function will return 44. From the 55-th attraction to the 11-st, 22-nd, 33-rd, and 44-th attractions, you must pass through the 11-st attraction.
  • If you query attractionsBehind(1, 5), the function will return 11.
  • One valid returned sequence is [3,6,4,5,2,0,1][3, 6, 4, 5, 2, 0, 1], and the required times to reach the next attraction in order are [4,3,3,3,2,1][4, 3, 3, 3, 2, 1].

Constraints

  • 2N1000002 \leq N \leq 100 000.
  • Q=400000Q = 400 000.
  • Any two attractions can reach each other through bidirectional roads.
  • Each attraction is connected to at most three roads.

Subtask 11 (1010 points)

  • N17N \leq 17.

Subtask 22 (1616 points)

  • N500N \leq 500.

Subtask 33 (2121 points)

  • For all 1i<N1 \leq i < N, there is a bidirectional road connecting attraction ii and attraction i12\lfloor \dfrac{i-1}{2} \rfloor.

Subtask 44 (1919 points)

There exists at least one attraction TT such that for all 0i<N0 \leq i < N, hoursRequired(T, i) <30< 30, and there exists an integer interval [L[i],R[i]](0L[i]iR[i]<N)[L[i], R[i]](0 \leq L[i] \leq i \leq R[i] < N) satisfying the following conditions:

  • Traveling from attraction TT to attraction jj must pass through attraction ii if and only if L[i]jR[i]L[i] \leq j \leq R[i].

  • If L[i]<iL[i] < i, then there is exactly one attraction XX such that:

    • L[i]X<iL[i] \leq X < i.
    • There is a road connecting attraction ii and attraction XX.
  • If i<R[i]i < R[i], then there is exactly one attraction YY such that:

    • i<YR[i]i < Y \leq R[i].
    • There is a road connecting attraction ii and attraction YY.

Subtask 55 (3434 points)

  • No additional constraints.

Translated by ChatGPT 5