#P17141. [NOI 2026] 传送

    ID: 19489 远端评测题 5000ms 1024MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>贪心二分点分治三分NOI交互题2026

[NOI 2026] 传送

Background

The statement and sample attachments come from QOJ.

When submitting to Luogu, there is no need to include the header #include "teleport.h". Just copy

std::vector<std::pair<long long, int>> teleport(int c, int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> x, std::vector<int> y);

to the beginning of your program, and compile with a C++17 or higher compiler.

Problem Description

Country CC has nn cities, numbered 0∼n−10\sim n-1. These nn cities are connected by n−1n-1 roads, forming a tree structure. The ii-th (0≤i<n−10\le i<n-1) road connects cities uiu_i and viv_i, and it takes 11 unit of time to travel from one endpoint city to the other.

To improve traffic efficiency, Country CC has developed a new type of teleport gate. Each city has one teleport gate. Using a teleport gate also takes 11 unit of time, but since the system is not yet stable, it will teleport the user to one of all nn cities with equal probability. Note: using a teleport gate may also teleport you to the city you are currently in.

To test the effect of the teleport gates, Country CC conducted mm tests. In the ii-th (0≤i<m0\le i<m) test, the tester is required to start from city xix_i and go to city yiy_i. During the trip from the start to the destination, the tester may choose to move along roads or use teleport gates. Since there are many possible ways to travel, the tester needs to find the travel strategy with the minimum expected time.

Specifically, define a travel strategy as follows: for every non-destination city, choose one adjacent city or choose to use the teleport gate. Every time the tester arrives at that city, they will move according to the pre-determined choice, i.e., move to the chosen adjacent city, or use the teleport gate.

Formally, a travel strategy can be represented by a sequence of length nn, [a0,…,an−1][a_0,\ldots,a_{n-1}], where ayi=−1a_{y_i}=-1, and for all j≠yij\ne y_i, aja_j is adjacent to jj, or aj=na_j=n. Every time the tester arrives at city jj (j≠yij\ne y_i), if aj<na_j<n, the tester moves to aja_j; otherwise, the tester uses the teleport gate.

A travel strategy is called valid if and only if its expected time is finite.

For each test, compute the minimum expected time among all valid travel strategies.

【Implementation Details】

Contestants do not need to, and should not, implement the main function.

Contestants need to ensure that the submitted source file includes the header teleport.h, i.e., add the following code at the beginning of the program:

#include "teleport.h"

Contestants need to implement the following function in the submitted source file teleport.cpp:

std::vector<std::pair<long long, int>> teleport(int c, int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> x, std::vector<int> y);
  • c,n,mc,n,m represent the test point index, the number of cities, and the number of tests, respectively. c=0c=0 means this test point is the sample.
  • u,vu,v represent the two cities connected by each road.
  • x,yx,y represent the start and the destination of each test.
  • This function needs to return a sequence of pairs of length exactly mm: (a0,b0),(a1,b1),…,(am−1,bm−1)(a_0,b_0),(a_1,b_1),\ldots,(a_{m-1},b_{m-1}), where ai,bia_i,b_i (0≤i<m0\le i<m) mean that, in the ii-th test, the minimum expected time in lowest terms is aibi\frac{a_i}{b_i}. In particular, if the minimum expected time is a positive integer, treat it as bi=1b_i=1.
  • For each test point, this function will be called exactly once by the grader.

template_teleport.cpp in this problem directory is the provided sample code. You may refer to it and implement your own solution.

Input Format

【Grader Program Mode】

You can compile an executable in this problem directory with the following command:

g++ grader.cpp teleport.cpp -o teleport -O2 -std=c++14 -static

For the compiled executable teleport:

  • The executable reads input from standard input in the following format:
    • The first line contains three non-negative integers c,n,mc,n,m.
    • Line i+2i+2 (0≤i<n−10\le i<n-1) contains two non-negative integers ui,viu_i,v_i.
    • Line i+n+1i+n+1 (0≤i<m0\le i<m) contains two non-negative integers xi,yix_i,y_i.
  • The executable outputs to standard output in the following format:
    • Line i+1i+1 (0≤i<m0\le i<m) contains two positive integers ai,bia_i,b_i.
0 4 4
0 1
1 2
2 3
0 3
0 1
0 2
1 2
7 3
1 1
2 1
1 1

Hint

【Sample 11 Explanation】

For the 00-th test:

  • If the travel strategy is [1,2,3,−1][1,2,3,-1], then the time cost is the fixed value 33.
  • If the travel strategy is [4,2,3,−1][4,2,3,-1], then the tester will keep using the teleport gate until leaving city 00, so the expected time is 73\frac{7}{3}.
  • If the travel strategy is [4,4,3,−1][4,4,3,-1], then the tester will keep using the teleport gate until reaching city 22 or city 33, so the expected time is 33.
  • If the travel strategy is [1,0,4,−1][1,0,4,-1], then the tester will move forever between city 00 and city 11, so this travel strategy is not valid.

It can be proven that the minimum expected time is 73\frac{7}{3}.

【Sample 22】

See teleport/teleport2.in and teleport/teleport2.ans in the contestant directory.

This sample satisfies the constraints of test points 2,32,3.

【Sample 33】

See teleport/teleport3.in and teleport/teleport3.ans in the contestant directory.

This sample satisfies the constraints of test points 4∼64\sim6.

【Sample 44】

See teleport/teleport4.in and teleport/teleport4.ans in the contestant directory.

This sample satisfies the constraints of test points 7∼87\sim8.

【Sample 55】

See teleport/teleport5.in and teleport/teleport5.ans in the contestant directory.

This sample satisfies the constraints of test point 99.

【Sample 66】

See teleport/teleport6.in and teleport/teleport6.ans in the contestant directory.

This sample satisfies the constraints of test point 1616.

【Sample 77】

See teleport/teleport7.in and teleport/teleport7.ans in the contestant directory.

This sample satisfies the constraints of test points 17∼2017\sim20.

【Constraints】

For all testdata:

  • 2≤n≤5×1052\le n\le5\times10^5, 1≤m≤1061\le m\le10^6.
  • For all 0≤i<n−10\le i<n-1, 0≤ui,vi<n0\le u_i,v_i<n, and all (ui,vi)(u_i,v_i) form a tree.
  • For all 0≤i<m0\le i<m, 0≤xi,yi<n0\le x_i,y_i<n and xi≠yix_i\ne y_i.

::cute-table{tuack} | Test Point Index | n≤n\le | m≤m\le | Special Property | |:-:|:-:|:-:|:-:| | 11 | 44 | 2020 | None | | 2,32,3 | 55 | 3030 | ^ | | 4∼64\sim6 | 10210^2 | 11 | ^ | | 7,87,8 | 10310^3 | 20002000 | AA | | 99 | ^ | 10610^6 | None | | 10,1110,11 | 10510^5 | ^ | AA | | 12∼1512\sim15 | ^ | ^ | None | | 1616 | 5×1055\times10^5 | 5×1055\times10^5 | BB | | 17∼2017\sim20 | ^ | 10610^6 | None |

Special property AA: For all 0≤i<n−10\le i<n-1, ui=iu_i=i and vi=i+1v_i=i+1.

Special property BB: For all 0≤i<m0\le i<m, yi=0y_i=0.

Translated by ChatGPT 5