#P5473. [NOI2019] I 君的探险
[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 large caves and 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 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 vertices and edges (a simple graph means there is at most one directly connected edge between any two vertices). The caves are numbered from . 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:
-
Given an index , the mechanism will toggle the light state of cave and all caves that are directly connected to cave by a passage. That is, lights that were on will be turned off, and lights that were off will be turned on.
-
Given an index , the mechanism will display the current light state of cave .
-
Given two indices , it means you confirm that there is a passage connecting cave and cave , and you ask the mechanism to record it.
-
Given an index , the mechanism will determine whether all passages incident to cave 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 , respectively. Your task is to write a program to help Mr. I decide how to use the mechanism properly, so that all 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 and are the numbers of caves and passages. You can interact with the interactive library by calling the following four functions:
modify(x)
-
This function makes the mechanism perform operation with the given index .
-
You must ensure . This function has no return value.
query(x)
-
This function makes the mechanism perform operation with the given index .
-
You must ensure . This function returns or , meaning the light source of cave is currently off () or on ().
report(x, y)
-
This function makes the mechanism perform operation with the given indices .
-
You must ensure and . This function has no return value.
check(x)
-
This function makes the mechanism perform operation with the given index .
-
You must ensure . This function returns or , where it returns if and only if all passages incident to cave 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.
- For C++ / C contestants
- Please make sure your program starts with
#include "explore.h"。
- The interface of the function
exploreyou 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);
- 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
_explorerather thanexplore. Usingexplorewill 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.
- 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
- For
Pascalcontestants:
- You need to compile the executable in this problem directory using the following command:
fpc grader.pas -o"explore" -O2
- For the compiled executable:
- The executable will read input from standard input in the following format:
The first line contains three integers . The second line contains two integers , with the meanings as described above.
The next lines each contain two integers , describing a passage connecting cave and cave .
- After reading is finished, the interactive library will call the function
exploreexactly 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 outputCorrectand 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 , the number of calls to query(x) must not exceed , and the number of calls to check(x) must not exceed .
The two integers on the second line of the testdata are the number of caves and the number of passages, i.e. .
The number of calls to report(x, y) must not exceed , so in this example it must not exceed calls.
Below is a correct interaction process:
| Contestant Program | Interactive Library | Explanation |
|---|---|---|
| Calls | Start testing. | |
| Calls | Perform operation on cave . | |
| Calls | Returns | The current light state of cave is off. |
| Calls | Found and recorded the passage . | |
| Calls | Returns | All passages incident to cave have been recorded. |
| Calls | Found and recorded the passage . | |
| Ends and returns | Prints | Interaction ends, and the result is correct. |
Notes on Provided Files
In this problem directory:
-
grader.cpp/candgraderhelperlib.pasare the reference implementation of the interactive library provided by us. -
explore.handgrader.pasare header files; contestants do not need to care about their contents. -
template_explore.cpp/c/pasis the sample solution source code provided by us. -
explore1.in,explore2.in,explore3.inare 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 points, runtime errors, time limit exceeded, memory limit exceeded, etc. will make the corresponding test point score 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:
-
Every function call you make is valid, and the numbers of calls to
modify,query, andcheckdo not exceed , respectively. -
Since the call limit of
reportis , each call must record a new edge that exists. That is, each time you callreport(x, y), it must satisfy: there is a passage connecting caves and , and before this call you have never calledreport(x, y)orreport(y, x). -
The function
exploreyou implemented returns normally. -
When
explorereturns, you have recorded all passages by callingreport.
There are test points in total, each worth points. The data size and related limits for each test point are shown in the table below. | Test Point ID | | | | | | Special Property | | :----------: | :----------: | :----------: | :----------: | :----------: | :----------: | :----------: | | | | | | | | None | | | | | | | | None | | | | | | | | None | | | | | | | | None | | | | | | | | None | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | None | | | | | | $$5\times 10^4$$ | | None | | | | | | | | None | | | | | | | | None | | | | | | | | None | | | | | | | | None | | | | | | | | None | | | | | | | | 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 .
B: It is guaranteed that for every , there exists exactly one such that cave is directly connected to cave by a passage.
C: There exists a permutation of such that for any , there exists a passage connecting caves and .
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 .
Translated by ChatGPT 5
京公网安备 11011102002149号