/
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 100 points
Problem Statement
Do you know Just Odd Inventions Co., Ltd.? This company engages in nothing but just odd inventions. We abbreviate it as JOI.
In one of JOI's laboratories, there is a complicated electric circuit. The circuit consists of N nodes and M thin electric resistors. The nodes are numbered 0 through N - 1, and the resistors are numbered 0 through M - 1. Each node can be set to one of two states: high voltage or low voltage. Resistor i (0 \leq i \leq M - 1) is connected from node A_i to another node B_i. Current flows through this resistor if and only if node A_i is set to high voltage and node B_i is set to low voltage. In all other cases, no current flows through it. Also, for any two nodes, there is at most one resistor connecting them, regardless of its direction.
You are a researcher at JOI, and you are going to conduct an experiment using this circuit. The resistors in the circuit are so thin that you cannot visually determine which pairs of nodes they connect. However, there is one clue: while the voltages are being set, the temperature of the circuit rises according to the number of resistors through which current flows. Therefore, after setting the voltages, you decide to touch the circuit and read its temperature. Although you cannot measure the exact temperature of the circuit, you can perform two voltage settings and compare which one makes the circuit hotter. That is, by specifying two voltage settings, you can obtain one of the following pieces of information:
- In the first voltage setting, the number of resistors through which current flows is larger.
- In the two voltage settings, the number of resistors through which current flows is the same.
- In the second voltage setting, the number of resistors through which current flows is larger.
Your goal is to repeat such temperature comparisons and determine all resistors in the circuit, that is, all ordered pairs (a, b) such that there is a resistor from node a to node b. You are given in advance the number of nodes N and the number of resistors M. You also know that every resistor connects two distinct nodes, and that for any two nodes there is at most one resistor connecting them, regardless of direction. Under these conditions, determine the entire set of ordered pairs (a, b) corresponding to the resistors in the circuit, from the information obtained by the temperature comparisons. Note that, depending on the structure of the circuit, it may be impossible to determine the resistors uniquely no matter what comparisons are performed or how many are performed. In that case, you must report that it is impossible to determine them uniquely.
To prevent deterioration of the resistors, you are allowed to compare temperatures at most 30\,000 times.
Incidentally, what kind of invention JOI is making with this odd circuit is top secret even within the company, and nobody except the president knows it.
Given the numbers of nodes and resistors in the circuit, write a program that determines the resistors in the circuit, or reports that this is impossible, using at most 30\,000 temperature comparisons.
Implementation Details
Your submitted program must include voltage.h using the #include preprocessor directive, and must implement the following function.
-
bool solve(int N, int M)- This function is called exactly once in each execution.
- The argument
Nrepresents the number of nodes in the circuit, N. - The argument
Mrepresents the number of resistors in the circuit, M. - This function must return
falseif the resistor connections cannot be uniquely determined no matter what temperature comparisons are performed or how many are performed; otherwise, it must returntrue. - If this function returns
truewhen the resistor connections cannot be determined uniquely, your program is judged as Wrong Answer [1]. - If this function returns
falseeven though the circuit can be determined from temperature comparisons, your program is judged as Wrong Answer [2].
Your program may call the following functions.
-
int query(std::vector<int> x, std::vector<int> y)- By using this function, you can perform two voltage settings and compare their temperatures.
- The argument
xspecifies the first voltage setting, and the argumentyspecifies the second voltage setting. - The arguments
xandymust be arrays of length N consisting of 0's and 1's. - If
x[k](0 \leq k \leq N - 1) is 1, then in the first voltage setting, node k is set to high voltage; ifx[k]is 0, then node k is set to low voltage. - If
y[k](0 \leq k \leq N - 1) is 1, then in the second voltage setting, node k is set to high voltage; ify[k]is 0, then node k is set to low voltage. - The return value of this function is the result of comparing the temperatures of the circuit under the first and second voltage settings, and is one of -1, 0, or 1.
- If the return value is -1, then the number of resistors through which current flows is larger in the first voltage setting than in the second.
- If the return value is 0, then the numbers of resistors through which current flows are equal in the first and second voltage settings.
- If the return value is 1, then the number of resistors through which current flows is larger in the second voltage setting than in the first.
- If the length of
xis not N, your program is judged as Wrong Answer [3]. - If
xcontains a value other than 0 or 1, your program is judged as Wrong Answer [4]. - If the length of
yis not N, your program is judged as Wrong Answer [5]. - If
ycontains a value other than 0 or 1, your program is judged as Wrong Answer [6]. - You must not call this function more than 30\,000 times. If this function is called more than 30\,000 times, your program is judged as Wrong Answer [7].
-
void answer(int a, int b)- Use this function to report a resistor that you have identified.
- The arguments
aandbindicate that there is a resistor directed from node a to node b. - It must hold that 0 \leq a \leq N - 1 and 0 \leq b \leq N - 1. If this condition is not satisfied, your program is judged as Wrong Answer [8].
- You must not call this function two or more times with the same pair (a, b). If this condition is violated, your program is judged as Wrong Answer [9].
- You must not call this function more than M times. If this function is called more than M times, your program is judged as Wrong Answer [10].
- When the function
solvereturnstrue, the functionanswermust have been called exactly M times by then. If this condition is not satisfied, your program is judged as Wrong Answer [11]. - When the function
solvereturnstrue, for every pair (a, b) passed as arguments toanswerup to that point, there must actually exist a resistor directed from node a to node b. If this condition is not satisfied, your program is judged as Wrong Answer [12].
Important Notices
- You may implement additional helper functions or declare global variables for internal use.
- Your submitted program must not use standard input/output or any other file interaction. However, you may use standard error output for debugging.
Compilation and Execution
You can download an archive file from the contest webpage which contains the sample grader to test your program. The archive file also contains a sample source file of your program.
The sample grader is the file grader.cpp. To test your program, place the files grader.cpp, voltage.cpp, and voltage.h in the same directory, and compile them using the following command:
g++ -std=gnu++20 -O2 -o grader grader.cpp voltage.cpp
Alternatively, you can execute the compile.sh file included in the archive by running:
./compile.sh
If compilation is successful, an executable file named grader will be generated.
Note that the actual grader is different from the sample grader. The sample grader is launched as a single process, which will read input data from the standard input and write the results to the standard output.
Input for the Sample Grader
The sample grader reads input from standard input in the following format:
N M
A_0 B_0
\vdots
A_{M-1} B_{M-1}
Output of the Sample Grader
The sample grader outputs the following information to standard output (quotation marks are not included in the actual output).
- If your program is judged as one of Wrong Answer [3] – [12], the type of Wrong Answer is printed as in "
Wrong Answer [5]". - Otherwise, the number of calls to the function
queryand the return value of the functionsolveare printed as in "Accepted: 30 true". The sample grader, unlike the actual grader, does not judge whether the verdict should be Wrong Answer [1] or [2], that is, whether the return value of the functionsolveis correct.
The sample grader terminates execution as soon as any of the conditions for Wrong Answer [3] – [12] is met. If multiple incorrect conditions are met, only one of them is displayed.
Notices on Grading
The actual grader is not adaptive; it has a fixed answer from the beginning of the interaction.
Constraints
- 2 \leq N \leq 600.
- 1 \leq M \leq 1\,000.
- 0 \leq A_i \leq N - 1 (0 \leq i \leq M - 1).
- 0 \leq B_i \leq N - 1 (0 \leq i \leq M - 1).
- A_i \neq B_i (0 \leq i \leq M - 1).
- (A_i, B_i) \neq (A_j, B_j) and (A_i, B_i) \neq (B_j, A_j) (0 \leq i < j \leq M - 1).
- N, M, A_i, and B_i are integers (0 \leq i \leq M - 1).
Subtasks
- (10 points) N \leq 100, M = N - 1, B_i = A_{i+1} (0 \leq i \leq N - 3), and the N values A_0, A_1, \ldots, A_{N-2}, B_{N-2} are all distinct.
- (12 points) M = N - 1, B_i = A_{i+1} (0 \leq i \leq N - 3), and the N values A_0, A_1, \ldots, A_{N-2}, B_{N-2} are all distinct.
- (27 points) N \leq 100, A_i \neq A_j (0 \leq i < j \leq M - 1).
- (18 points) A_i \neq A_j (0 \leq i < j \leq M - 1).
- (17 points) N \leq 100.
- (16 points) No additional constraints.
Example Interaction
Below is an example of input that the sample grader reads and the corresponding function calls.
Sample Input 1
5 6 0 2 2 1 0 3 3 2 3 4 4 1

In the first call to query, the two voltage settings and the resistors through which current flows are as follows.
- In the first setting, nodes 0 and 1 are set to low voltage, and nodes 2, 3, and 4 are set to high voltage. At this time, current flows through resistors 1 and 5.
- In the second setting, nodes 3 and 4 are set to low voltage, and nodes 0, 1, and 2 are set to high voltage. At this time, current flows through resistor 2.
Since the number of resistors through which current flows is larger in the first voltage setting, the return value is -1.
This sample input satisfies the constraints for subtasks 5, 6.
Among the files available for download from the contest site: sample-01-in.txt corresponds to Sample Input 1.
Additionally, sample-02-in.txt satisfies the constraints for all subtasks, and sample-03-in.txt satisfies the constraints for subtasks 3, 4, 5, 6.
配点 : 100 点
問題文
あなたは Just Odd Inventions 社を知っているだろうか? この会社の業務は「ただ奇妙な発明 (just odd inventions)」をすることである.ここでは略して JOI 社と呼ぶ.
JOI 社のとある実験室には,複雑な電気回路がある. 回路は N 個の節点と M 本の細長い電気抵抗からなり,節点には 0 から N - 1 までの,電気抵抗には 0 から M - 1 までの番号が付けられている. 各節点は「高電圧」または「低電圧」のいずれかの状態に設定することができる. 電気抵抗 i (0 \leqq i \leqq M - 1) は節点 A_i から異なる節点 B_i に向けて繋がれており,節点 A_i が「高電圧」,節点 B_i が「低電圧」の状態にあるときにのみ電流が流れる. それ以外の場合には電流は流れない. また,任意の 2 つの節点を繋ぐ電気抵抗は,その向きに関わらず高々 1 本しか存在しない.
JOI 社の研究員であるあなたは,この回路を用いて実験を行うことになった. この回路の電気抵抗はあまりにも細長いため,電気抵抗がどの節点を結んでいるのかを目視で確認することはできない. しかし,各節点の電圧を設定している間,電流が流れている電気抵抗の本数に応じて回路の温度が上昇するという手がかりがある. そこであなたは,電圧を設定した後,回路に触れることで温度を読み取ることにした. あなたは回路の温度を正確に読み取ることはできないが,電圧の設定を 2 回行い,どちらの設定においてより温度が高くなったかを比較することはできる. つまり,電圧の設定を 2 回指定することで,以下のいずれかの情報が得られる.
- 1 回目の電圧の設定の方が,電流が流れる電気抵抗の本数が多い.
- 2 回の電圧の設定において,電流が流れる電気抵抗の本数は同じである.
- 2 回目の電圧の設定の方が,電流が流れる電気抵抗の本数が多い.
あなたの目的は,この温度の比較を繰り返すことで,どの節点からどの節点へ向かう電気抵抗が存在するかをすべて特定することである. あらかじめ,節点の数 N,電気抵抗の本数 M は与えられている. また,電気抵抗は異なる節点を繋いでいること,および任意の 2 つの節点を繋ぐ電気抵抗はその向きに関わらず高々 1 本であることも分かっている. これらの条件のもと,温度の比較によって得られた情報から,回路に存在する電気抵抗が繋ぐ節点のペア (a, b) の集合をすべて特定せよ. ただし,回路の構造によっては,いかなる比較を何回行ったとしても,電気抵抗の繋がりを一意に特定できない場合がある. その場合には,特定不能であることを報告する必要がある.
電気抵抗の劣化を防ぐため,実際に温度を比較するのは 30\,000 回までしか行うことが許されない.
なお,JOI 社がこの奇妙な回路を用いてどのような発明をしているかは,社内でも最高機密であり社長以外の誰も知らない.
回路の節点と電気抵抗の本数が与えられたとき,30\,000 回以下の温度の比較で,回路の電気抵抗を特定するかもしくは特定不能であることを報告するプログラムを作成せよ.
実装の詳細
あなたの回答プログラムは,voltage.h を #include プリプロセッサ指令で読み込み,以下の関数を実装しなければならない.
-
bool solve(int N, int M)- この関数は 1 回の実行で 1 回だけ呼び出される.
- 引数
Nは 回路の節点の数 N である. - 引数
Mは 回路の電気抵抗の本数 M である. - この関数は,いかなる温度の比較を何回行ったとしても電気抵抗の繋がりを一意に特定できない場合には
falseを,それ以外の場合にはtrueを返さなければならない. - 電気抵抗の繋がりを特定不能であるのに
trueを返した場合,不正解[1] と判定される. - 温度の比較によって回路が特定できるのに
falseを返した場合,不正解[2] と判定される.
あなたのプログラムは以下の関数を呼び出すことができる.
-
int query(std::vector<int> x, std::vector<int> y)- あなたはこの関数を用いて電圧の設定を 2 回行い,温度を比較できる.
- 引数
xは 1 回目の電圧の設定を,引数yは 2 回目の電圧の設定を指定する. - 引数
xとyは 0 と 1 からなる長さ N の配列でなければならない. -
x[k](0 \leqq k \leqq N - 1) が 1 ならば 1 回目の電圧の設定において,節点 k は「高電圧」に,x[k]が 0 ならば節点 k は「低電圧」に設定することを表す. -
y[k](0 \leqq k \leqq N - 1) が 1 ならば 2 回目の電圧の設定において,節点 k は「高電圧」に,y[k]が 0 ならば節点 k は「低電圧」に設定することを表す. - この関数の戻り値は 1 回目の電圧の設定と 2 回目の電圧の設定において,温度を比較した結果であり,-1,0,1のいずれかの値である.
- 戻り値が-1のとき,1 回目の方が,2 回目の電圧の設定より,電流が流れる電気抵抗の本数が多いことを表す.
- 戻り値が0のとき,1 回目と2 回目の電圧の設定において電流が流れる電気抵抗の本数が等しいことを表す.
- 戻り値が1のとき,2 回目の方が,1 回目の電圧の設定より,電流が流れる電気抵抗の本数が多いことを表す.
- 引数
xの長さが N でない場合,不正解[3] と判定される. - 引数
xに 0 でも 1 でもない値が含まれる場合,不正解[4] と判定される. - 引数
yの長さが N でない場合,不正解[5] と判定される. - 引数
yに 0 でも 1 でもない値が含まれる場合,不正解[6] と判定される. - この関数を 30\,000 回より多く呼び出してはならない.30\,000 回より多く呼び出した場合,不正解[7] と判定される.
-
void answer(int a, int b)- この関数を用いて,特定した電気抵抗を解答する.
- 引数
a,bは節点 a から節点 b に向けて繋がれている電気抵抗があることを表す. - 0 \leqq a \leqq N - 1 かつ 0 \leqq b \leqq N - 1 でなければならない.これが満たされない場合 不正解[8] と判定される.
- 同じ (a, b) の組を引数として 2 回以上呼び出してはならない.これが満たされない場合 不正解[9] と判定される.
- この関数を M 回より多く呼び出してはならない.M 回より多く呼び出した場合,不正解[10] と判定される.
- 関数
solveがtrueを返したとき,それまでに関数answerはちょうど M 回呼び出されている必要がある. これが満たされない場合,不正解[11] と判定される. - 関数
solveがtrueを返したとき,それまでに関数answerの引数として渡されたすべての組 (a, b) について,節点 a から節点 b へ向けて繋がれている電気抵抗が実際に存在していなければならない. これが満たされない場合,不正解[12] と判定される.
重要な注意
- 内部での使用のために他の関数を実装したり,グローバル変数を宣言するのは自由である.
- あなたの提出したプログラムは,標準入力・標準出力,あるいは他のファイルといかなる方法でもやりとりしてはならない. ただし,標準エラー出力にデバッグ情報等を出力することは許される.
コンパイル・実行の方法
作成したプログラムをテストするための,採点プログラムのサンプルが, コンテストサイトからダウンロードできるアーカイブの中に含まれている. このアーカイブには,提出しなければならないファイルのサンプルも含まれている.
採点プログラムのサンプルは 1 つのファイルからなる.
そのファイルは grader.cpp である.
作成したプログラムをテストするには,
これらのファイル grader.cpp, voltage.cpp, voltage.h を同じディレクトリに置き,次のようにコマンドを実行する.
g++ -std=gnu++20 -O2 -o grader grader.cpp voltage.cpp
なお,アーカイブの中に含まれている compile.sh というファイルを代わりに実行してもよい.その場合,次のようにコマンドを実行する.
./compile.sh
コンパイルが成功すれば,grader という実行ファイルが生成される.
実際の採点プログラムは,採点プログラムのサンプルとは異なることに注意すること. 採点プログラムのサンプルは単一のプロセスとして起動する. このプログラムは,標準入力から入力を読み込み,標準出力に結果を出力する.
採点プログラムのサンプルの入力
採点プログラムのサンプルは標準入力から以下の形式で入力を読み込む.
N M
A_0 B_0
\vdots
A_{M-1} B_{M-1}
採点プログラムのサンプルの出力
採点プログラムのサンプルは標準出力へ以下の情報を出力する (引用符は実際には出力されない).
- 不正解 [3] ~ [12] のいずれかの場合,不正解の種類が "
Wrong Answer [5]" のように出力される. - そうでない場合,関数
queryの呼び出し回数と,関数solveの戻り値が "Accepted: 30 true" のように出力される. 採点プログラムのサンプルは,実際の採点プログラムと違って不正解 [1], [2] であるか,つまり関数solveの戻り値が正しいかを判定しないことに注意せよ.
採点プログラムのサンプルは,不正解 [3] ~ [12] のいずれかの不正解の条件が満たされた時点で実行を終了する. 実行するプログラムが不正解 [3] ~ [12] のうち,複数の条件を満たした場合,表示される不正解の種類はそれらのうち 1 つのみである.
採点に関する注意
実際の採点プログラムは適応的 (adaptive) ではなく,やりとりの初めから固定された答えを持つ.
制約
- 2 \leqq N \leqq 500.
- 1 \leqq M \leqq 1\,000.
- 0 \leqq A_i \leqq N - 1 (0 \leqq i \leqq M - 1).
- 0 \leqq B_i \leqq N - 1 (0 \leqq i \leqq M - 1).
- A_i \neq B_i (0 \leqq i \leqq M - 1).
- (A_i, B_i) \neq (A_j, B_j) かつ (A_i, B_i) \neq (B_j, A_j) (0 \leqq i < j \leqq M - 1).
- N, M, A_i, B_i は整数である (0 \leqq i \leqq M - 1).
小課題
- (10 点) N \leqq 100,M = N - 1,B_i = A_{i+1} (0 \leqq i \leqq N - 3),N 個の値 A_0, A_1, \ldots ,A_{N-2}, B_{N-2} はすべて異なる.
- (12 点) M = N - 1,B_i = A_{i+1} (0 \leqq i \leqq N - 3),N 個の値 A_0, A_1, \ldots ,A_{N-2}, B_{N-2} はすべて異なる.
- (27 点) N \leqq 100,A_i \neq A_j (0 \leqq i < j \leqq M - 1).
- (18 点) A_i \neq A_j (0 \leqq i < j \leqq M - 1).
- (17 点) N \leqq 100.
- (16 点) 追加の制約はない.
やりとりの例
採点プログラムのサンプルが読み込む入力の例と,それに対応する関数の呼び出しの例を以下に示す.
入力例 1
5 6 0 2 2 1 0 3 3 2 3 4 4 1

1 回目の \texttt{query} の呼び出しにおける 2 回の電圧の設定と,電流が流れる電気抵抗は以下の通りである.
- 1 回目は節点 0, 1 を「低電圧」に,節点 2, 3, 4 を「高電圧」に設定する.このとき,電気抵抗 1, 5 に電流が流れる.
- 2 回目は節点 3, 4 を「低電圧」に,節点 0, 1, 2 を「高電圧」に設定する.このとき,電気抵抗 2 に電流が流れる.
1 回目の電圧の設定の方が電流が流れる電気抵抗の本数が多いため,戻り値は -1 である.
この入力例は小課題 5, 6 の制約を満たす.
コンテストサイトからダウンロードできるファイルのうち,sample-01-in.txt は入力例 1 に対応する.
また,sample-02-in.txt はすべての小課題の制約を満たし,sample-03-in.txt は小課題 3, 4, 5, 6 の制約を満たす.