#P5473. [NOI2019] I 君的探险

    ID: 4422 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2019NOI 系列交互题Special JudgeO2优化

[NOI2019] I 君的探险

Description

Half a year later, Mr. I’s shop finally could not stay open anymore. He decided to transfer the shop and become an explorer to explore the vast unknown world.

According to ancient books, he found an underground palace created by an unknown civilization deep in a great desert. The palace consists of NN large caves and MM bidirectional passages connecting these caves. Mr. I can use the ancient book to identify which cave he is in, but the book does not record the connection structure of the MM passages, so it is difficult for him to search for the endless treasure said to be hidden in the palace.

However, Mr. I has now found a mysterious mechanism that can reveal information about the palace. Mr. I decides to use this mechanism to obtain the connection structure of the palace, and asks you to assist him.

The underground palace can be abstracted as an undirected simple graph with NN vertices and MM edges (a simple graph means there is at most one directly connected edge between any two vertices). The caves are numbered from 0n10 \sim n - 1. Currently, you do not know what the edges are.

Each cave has a light source, which has two states: on and off. A cave is lit only when its light source is on. Initially, all light sources are off, and the light states can only be changed using the mysterious mechanism discovered by Mr. I. More specifically, the mechanism supports the following four operations:

  1. Given an index xx, the mechanism will toggle the light state of cave xx and all caves that are directly connected to cave xx by a passage. That is, lights that were on will be turned off, and lights that were off will be turned on.

  2. Given an index xx, the mechanism will display the current light state of cave xx.

  3. Given two indices x,yx, y, it means you confirm that there is a passage connecting cave xx and cave yy, and you ask the mechanism to record it.

  4. Given an index xx, the mechanism will determine whether all passages incident to cave xx have already been recorded.

The mechanism can perform the next operation only after finishing the previous one. The mechanism cannot be used freely, so the number of times each operation can be used is limited to Lm,Lq,M,LcL_m, L_q, M, L_c, respectively. Your task is to write a program to help Mr. I decide how to use the mechanism properly, so that all MM passages can be found correctly.

Implementation Details

You do not need to, and should not, implement the main function. You only need to implement the function explore(N, M), where NN and MM are the numbers of caves and passages. You can interact with the interactive library by calling the following four functions:

  1. modify(x)
  • This function makes the mechanism perform operation 11 with the given index xx.

  • You must ensure 0x<N0 \leq x < N. This function has no return value.

  1. query(x)
  • This function makes the mechanism perform operation 22 with the given index xx.

  • You must ensure 0x<N0 \leq x < N. This function returns 00 or 11, meaning the light source of cave xx is currently off (00) or on (11).

  1. report(x, y)
  • This function makes the mechanism perform operation 33 with the given indices x,yx, y.

  • You must ensure 0x,y<N0 \leq x, y < N and xyx \neq y. This function has no return value.

  1. check(x)
  • This function makes the mechanism perform operation 44 with the given index xx.

  • You must ensure 0x<N0 \leq x < N. This function returns 00 or 11, where it returns 11 if and only if all passages incident to cave xx have already been recorded via operation 3.

During judging, the interactive library will call explore exactly once.

This problem guarantees that the graph used has been fully determined before the interaction starts, and will not be constructed dynamically based on the interaction process. Therefore, all interactive operations in the statement are deterministic, and you do not need to care about their internal implementation in the library.

The testdata guarantees that under the call limits, the time needed for the interactive library to run does not exceed 1 s. The memory used by the interactive library is fixed and does not exceed 128 MB.

Implementation Method

A template_explore.cpp/c/pas has already been provided in the contestant working directory. Please copy this file and rename it to explore.cpp/c/pas, then solve the problem based on it.

  1. For C++ / C contestants
  • Please make sure your program starts with
#include "explore.h"。
  • The interface of the function explore you need to implement is:
void explore(int N, int M);
  • The interfaces of the interactive functions you can call are:
void modify(int x);
int query(int x);
void report(int x, int y);
int check(int x);
  1. For Pascal contestants
  • Note: The syntax for implementing the interface in Pascal code is relatively complex. Please solve the problem directly based on the provided template_explore.pas, rather than implementing everything from scratch.

  • The interface of the function you need to implement is:

procedure _explore(N, M : longint);
  • Note: The function name here is _explore rather than explore. Using explore will cause compilation failure.

  • The interfaces of the interactive functions you can call are:

procedure modify(x : longint);
function query(x : longint) : longint;
procedure report(x : longint; y : longint);
function check(x : longint) : longint;

The grader.cpp/c and graderhelperlib.pas in the problem directory are a reference implementation of the interactive library provided by us. The interactive library used in the final test is different from this reference implementation, so your solution should not depend on the library implementation.

  1. For C/C++ contestants:
  • You need to compile the executable in this problem directory using the following commands:

  • For C:

gcc grader.c explore.c -o explore -O2 -lm
  • For C++:
g++ grader.cpp explore.cpp -o explore -O2 -lm
  1. For Pascal contestants:
  • You need to compile the executable in this problem directory using the following command:
fpc grader.pas -o"explore" -O2
  1. For the compiled executable:
  • The executable will read input from standard input in the following format:

The first line contains three integers Lm,Lq,LcL_m, L_q, L_c. The second line contains two integers N,MN, M, with the meanings as described above.

The next MM lines each contain two integers x,yx, y, describing a passage connecting cave xx and cave yy.

  • After reading is finished, the interactive library will call the function explore exactly once, and test your function with the input data. After your function returns correctly, the interactive library will check whether your result is correct. If it is correct, it will output Correct and information related to the number of interactive calls; otherwise it will output the corresponding error information.
100 200 300
3 2
0 1
1 2

见“提示与说明”

Hint

The three integers on the first line of the testdata are the call limits of the three operations: the number of calls to modify(x) must not exceed 100100, the number of calls to query(x) must not exceed 200200, and the number of calls to check(x) must not exceed 300300.

The two integers on the second line of the testdata are the number of caves and the number of passages, i.e. N=3,M=2N = 3 , M = 2.

The number of calls to report(x, y) must not exceed MM, so in this example it must not exceed 22 calls.

Below is a correct interaction process:

Contestant Program Interactive Library Explanation
Calls explore(3,2)\text{explore}(3,2) Start testing.
Calls modify(0)\text{modify}(0) Perform operation 11 on cave 00.
Calls query(2)\text{query}(2) Returns 00 The current light state of cave 22 is off.
Calls report(0,1)\text{report}(0,1) Found and recorded the passage (0,1)(0,1).
Calls check(0)\text{check}(0) Returns 11 All passages incident to cave 00 have been recorded.
Calls report(2,1)\text{report}(2,1) Found and recorded the passage (2,1)(2,1).
Ends and returns Prints Correct\text{Correct} Interaction ends, and the result is correct.

Notes on Provided Files

In this problem directory:

  1. grader.cpp/c and graderhelperlib.pas are the reference implementation of the interactive library provided by us.

  2. explore.h and grader.pas are header files; contestants do not need to care about their contents.

  3. template_explore.cpp/c/pas is the sample solution source code provided by us.

  4. explore1.in, explore2.in, explore3.in are sample inputs for testing.

Please back up all provided files. The judge only collects explore.c/cpp/pas in this problem directory, and any modifications to other files are ignored.

The final judging will only collect explore.cpp/c/pas. Modifying other files in your directory does not affect judging.

This problem is first subject to the same limits as traditional problems. For example, compilation errors will make the whole problem score 00 points, runtime errors, time limit exceeded, memory limit exceeded, etc. will make the corresponding test point score 00 points. You may only access variables you define and those provided by the interactive library, as well as their corresponding memory space. Attempting to access other memory may cause compilation errors or runtime errors.

Under the above conditions, for each test point, you get full score if and only if:

  1. Every function call you make is valid, and the numbers of calls to modify, query, and check do not exceed Lm,Lq,LcL_m, L_q, L_c, respectively.

  2. Since the call limit of report is MM, each call must record a new edge that exists. That is, each time you call report(x, y), it must satisfy: there is a passage connecting caves xx and yy, and before this call you have never called report(x, y) or report(y, x).

  3. The function explore you implemented returns normally.

  4. When explore returns, you have recorded all MM passages by calling report.

There are 2525 test points in total, each worth 44 points. The data size and related limits for each test point are shown in the table below. | Test Point ID | N=N= | M=M= | Lm=L_m= | Lq=L_q= | Lc=L_c= | Special Property | | :----------: | :----------: | :----------: | :----------: | :----------: | :----------: | :----------: | | 11 | 33 | 22 | 100100 | 100100 | 100100 | None | | 22 | 100100 | 10×N10\times N | 200200 | 10410^4 | 2×M2\times M | None | | 33 | 200200 | 10×N10\times N | 200200 | 4×1044\times 10^4 | 2×M2\times M | None | | 44 | 300300 | 10×N10\times N |299299 | 9×1049\times 10^4 | 2×M2\times M | None | | 55 | 500500 | 10×N10\times N | 499499 | 1.5×1051.5\times 10^5 | 2×M2\times M | None | | 66 | 5999859998 | N2\frac{N}{2} | 17×N17\times N | 17×N17\times N | 00 | AA | | 77 | 9999899998 | N2\frac{N}{2} | 18×N18\times N | 18×N18\times N | 00 | AA | | 88 | 199998199998 | N2\frac{N}{2} | 19×N19\times N | 19×N19\times N | 00 | AA | | 99 | 199998199998 | N2\frac{N}{2} | 19×N19\times N | 19×N19\times N | 00 | AA | | 1010 | 9999799997 | N1N-1 | 18×N18\times N | 18×N18\times N | 00 | BB | | 1111 | 199997199997 | N1N-1 | 19×N19\times N | 19×N19\times N | 00 | BB | | 1212 | 9999699996 | N1N-1 | 10710^7 | 10710^7 | 2×M2\times M | CC | | 1313 | 199996199996 | N1N-1 | 10710^7 | 10710^7 | 2×M2\times M | CC | | 1414 | 199996199996 | N1N-1 | 10710^7 | 10710^7 | 2×M2\times M | CC | | 1515 | 9999599995 | N1N-1 | 10710^7 | 10710^7 | 2×M2\times M | DD | | 1616 | 9999599995 | N1N-1 | 10710^7 | 10710^7 | 2×M2\times M | DD | | 1717 | 199995199995 | N1N-1 | 10710^7 | 10710^7 | 2×M2\times M | DD | | 1818 | 10041004 | 2×1032\times 10^3 | 10710^7 | 5×1045\times 10^4 | 2×M2\times M | None | | 1919 | 10041004 | 3×1033\times 10^3 | 10710^7 | $$5\times 10^4$$ | 2×M2\times M | None | | 2020 | 10041004 | 3×1033\times 10^3 | 10710^7 | 5×1045\times 10^4 | 2×M2\times M | None | | 2121 | 5×1045\times 10^4 | 2×N2\times N | 10710^7 | 10710^7 | 2×M2\times M | None | | 2222 | 10510^5 | 2×N2\times N | 10710^7 | 10710^7 | 2×M2\times M | None | | 2323 | 1.5×1051.5\times 10^5 | 2×1052\times 10^5 | 10710^7 | 10710^7 | 2×M2\times M | None | | 2424 | 2×1052\times 10^5 | 2.5×1052.5\times 10^5 | 10710^7 | 10710^7 | 2×M2\times M | None | | 2525 | 2×1052\times 10^5 | 3×1053\times 10^5 | 10710^7 | 10710^7 | 2×M2\times M | None |

Reminder again: the problem guarantees that the graph used for testing has been fully determined before the interaction starts, and will not be constructed dynamically based on the interaction with your program.

The meanings of the variables in the “Special Property” column are as follows:

A: It is guaranteed that the degree of every vertex is exactly 11.

B: It is guaranteed that for every x>0x > 0, there exists exactly one y<xy < x such that cave xx is directly connected to cave yy by a passage.

C: There exists a permutation p0,p1,,pN1p_0, p_1, \cdots , p_{N-1} of 0N10 \sim N - 1 such that for any 1i<N1 \leq i < N, there exists a passage connecting caves pi1p_{i-1} and pip_i.

D: It is guaranteed that the graph is connected.

  • Hint: Your program can distinguish the different data types above by checking the last digit of the given NN.

Translated by ChatGPT 5