#P6541. [WC2018] 即时战略

    ID: 5523 远端评测题 3000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2018WC/CTSC/集训队交互题Special Judge

[WC2018] 即时战略

Description

Little M is playing a Real Time Strategy game. Unlike most similar games, the map of this game is a tree. That is, the map can be represented by a connected graph with nn nodes and n1n - 1 edges. The nodes are numbered 1n1 \sim n.

Each node has two possible states: "known" or "unknown". When the game starts, only node 11 is known.
During the game, Little M can try to explore more nodes. Specifically, in each operation, Little M needs to choose a known node xx, and an arbitrary node yy different from xx (node yy may be unknown). Then the game's automatic pathfinding system will give the second node zz on the shortest path from xx to yy, i.e., the node adjacent to xx on the shortest path from xx to yy. At this time, if node zz is unknown, Little M will mark it as known.

The goal of the game is: using at most TT exploration operations, make all nodes become known. However, Little M is still a beginner at this game, and she hopes to get your help.

To make the game process easier, Little M provides you with an interactive library for this game; see "Task Description" and "Implementation Details".
Also, Little M provides some hints; see the last section "Hint".

Task Description

You need to implement a function play to help Little M achieve the goal.

  • play(n, T, dataType)
    • n is the number of nodes in the tree.
    • T is the limit on the number of exploration operations.
    • dataType is the data type of this test case; see "Constraints and Conventions".

In each test case, the interactive library will call the play function exactly once. Before it is called, the game is at the initial state.

You can call the function explore to help you explore more nodes in the game, but the number of calls to this function must not exceed TT.

  • explore(x, y)
    • x is a known node.
    • y is any node different from xx (it does not have to be known).
    • This function returns the index of the second node on the shortest path from node xx to node yy.

After the function play returns, the interactive library will check the game state: only if every node is known will the goal be considered completed.

Implementation Details

You must submit exactly one source file rts.cpp/c/pas implementing the above function, and follow the naming and interface below.

For C/C++ users:

The source code needs to include the header file rts.h (note that when submitting on Luogu, you do not need to include this header file).

The function play you need to implement:

void play(int n, int T, int dataType);

The interface of the function explore is:

int explore(int x, int y);

For Pascal users:

You need to use the unit graderhelperlib.

The function play you need to implement:

procedure play(n, T, dataType : longint);

The interface of the function explore is:

function explore(x, y : longint) : longint;

Input Format

The first line contains three integers nn, TT, dataType\texttt{dataType}. It is guaranteed that 2n3×1052 \leqslant n \leqslant 3 \times 10^5, 1T5×1061 \leqslant T \leqslant 5 \times 10^6, and dataType=1\texttt{dataType} = 1 or 22 or 33.

The next n1n - 1 lines each contain two integers uu, vv. It is guaranteed that 1u,vn1 \leqslant u, v \leqslant n and uvu \ne v, representing an edge between uu and vv.

The input guarantees that these n1n - 1 edges form a tree.

Output Format

The interactive library will judge whether the game goal is completed. If completed, it will output Correct\texttt{Correct}; otherwise, it will output the corresponding error message.

4 100 1
1 3
3 4
3 2
Correct

Hint

Sample Explanation

This is the output file obtained using the grader in the problem directory and a correct program.

For this sample, one possible call sequence of the explore function is:

  • explore(1, 2), returns 3
  • explore(3, 2), returns 2
  • explore(2, 4), returns 3
  • explore(3, 4), returns 4

How to Test Your Program

For C/C++ users:

In the problem directory, you need to compile to an executable using the following command:

g++ grader.cpp rts.cpp -o rts -O2

The executable will read input from standard input.

After reading is finished, the interactive library will call the play function. If at this time the number of times you call explore exceeds TT, the interactive library will output detailed error information and exit.

Then the interactive library will check whether the game goal is completed. If completed, it will output "Correct\texttt{Correct}"; otherwise, it will output the corresponding error message.

If the parameters passed to explore are invalid (x,yx, y are not in the range 11 to nn, or xx is not a known node, or x=yx = y), then the interactive library will output detailed error information and exit.

If you want to test using your own input file, please ensure that the input file meets the above format requirements. Otherwise, the program is not guaranteed to run correctly.

How to Use the Sample Source Code

In the problem directory, there are sample source codes for each language: rts_sample.cpp/c/pas. Choose the language you need, copy it as rts.cpp/c/pas, and compile as described above to obtain an executable.

For unofficial participants, you can only choose one language to answer. That is, in the problem directory, you cannot have multiple versions of rts.cpp/c/pas in different languages at the same time; otherwise, the system will arbitrarily select one source file for judging and use it as the final result.

Next, you need to modify the implementation of this file to meet the requirements of the problem.

Subtasks

There are 20 test points in total, and each test point is worth 5 points.

For all test points and all samples, 2n3×1052 \leqslant n \leqslant 3 \times 10^5, 1T5×1061 \leqslant T \leqslant 5 \times 10^6, and dataType=1\texttt{dataType} = 1 or 22 or 33.
The data types corresponding to different dataType\texttt{dataType} are as follows:

  • For test points with dataType=1\texttt{dataType} = 1, there are no special restrictions.
  • For test points with dataType=2\texttt{dataType} = 2, the game map is a complete binary tree rooted at node 11.
    That is, there exists a permutation aa of 11 ~ nn such that a1=1a_1 = 1, and node aia_i (1<in1 < i \leqslant n) is connected by an edge to node ai/2a_{\lfloor i/2 \rfloor}.
  • For test points with dataType=3\texttt{dataType} = 3, the game map is a chain.
    That is, there exists a permutation aa of 11 ~ nn such that node aia_i (1<in1 < i \leqslant n) is connected by an edge to node ai1a_{i-1}.

For each test point, the values of nn, TT, and dataType\texttt{dataType} are as follows:

Test Point ID n=n = T=T = dataType=\texttt{dataType} =
1 22 1000010000 1
2 33
3 1010
4 100100
5 10001000 2
6 2000020000 300000300000
7 250000250000 50000005000000
8 10001000 2000020000 3
9 50005000 1550015500
10 3000030000 6300063000
11 150000150000 165000165000
12 250000250000 250100250100
13 300000300000 300020300020
14 10001000 5000050000 1
15 50005000 200000200000
16 3000030000 900000900000
17 150000150000 37500003750000
18 200000200000 44000004400000
19 250000250000 50000005000000
20 300000300000

Hint

Here are some thoughtful tips from Little M:

  • A graph (undirected graph) consists of nodes and edges. An edge is an unordered pair of nodes, used to describe the relationships between nodes.
  • A path is a non-empty sequence of nodes such that there is an edge between every two adjacent nodes in the sequence.
  • Two nodes are connected if and only if there exists a path that starts at one of them and ends at the other.
  • A graph is connected if and only if every pair of nodes in the graph is connected.
  • A tree with nn nodes is a connected graph with nn nodes and n1n - 1 edges.
  • The shortest path between two nodes means, among all possible paths connecting these two nodes, the one with the smallest sequence length.
  • In a tree, the shortest path between any two nodes is unique.
  • Gaining points by accessing input/output files, attacking the judging system, attacking the judging library, etc., is considered cheating, and the score obtained is invalid.

Translated by ChatGPT 5