Lisa Simpson, a character from the animated series "The Simpsons," finds herself at a vertex of a connected undirected graph. She can move between vertices connected by an edge, but she can only see the vertex she is currently in and its directly connected neighbors. Without pen and paper, Lisa uses pebbles to explore the graph. She can place up to pebbles on a vertex, which she believes is sufficient. The graph has between and vertices.
The problem consists of two subproblems:
In the first subproblem, there are initially no pebbles on any vertex, and Lisa wants to determine the total number of vertices in the graph.
In the second subproblem, there is one pebble placed on some vertex (different from the starting one), and none in the others. Lisa wants to find the shortest path length (by edges) between the starting vertex and the vertex with the pebble, assuming each edge has unit length.
You need to write a procedure graph that, based on the number of pebbles in the current vertex and all its neighbors, decides the next step for Lisa: how many pebbles to leave or take from the current vertex, and which adjacent vertex to move to next; or, when Lisa is ready to answer the problem, returns this answer.
You must submit a file containing the implementation of the procedure graph, as described below, and (if necessary) additional code required for the correct execution of this procedure, but without the main function body (e.g., main in C++ or begin/end block in Pascal). During testing, your file will be combined with a special program body written by the jury. This main body will appropriately call your procedure and check the correctness of the implemented algorithm. Your implementation must conform to the following signatures:
C++:
void graph(int subproblem, int neighborsCount, int neighbors[], int ¤t, int &whereToGo, int &answer);Pascal:
procedure graph(subproblem, neighborsCount: longint; var neighbors: array of longint; var current, whereToGo, answer: longint);subproblem — subproblem number: for counting vertices, for finding the shortest path. The subproblem number does not change for calls on the same graph.
neighborsCount — the number of adjacent vertices, i.e., vertices connected by an edge to the current vertex.
neighbors — an array with exactly neighborsCount elements (-indexed): the numbers of pebbles in the neighboring vertices. The order of neighbors may vary arbitrarily between different calls to the procedure, even if the current vertex is the same. The content of this array may be modified within the procedure if necessary, but this does not affect the actual number of pebbles in the corresponding vertices: Lisa can change the number of pebbles only in the vertex she is currently in.
current — the number of pebbles in the current vertex. This number may be modified within the procedure (increased or decreased, but must not become negative or exceed ). This also changes the actual number of pebbles in the corresponding vertex of the graph.
whereToGo — its initial value (the argument as received from the main program) is . If Lisa continues exploring the graph, you must update this value, setting it to the number of pebbles in the next vertex Lisa will move to (note: this is a value, not an index in the neighbors array!). If there are several elements in neighbors with the same value, Lisa will move to one of the corresponding vertices, but the procedure cannot control which one. If Lisa is ready to answer, you should leave the initial value unchanged (and must not modify it).
answer — its initial value (the argument as received from the main program) is also . If Lisa is ready to answer, set this value to the answer. If Lisa is not ready, you should leave the initial value unchanged (and must not modify it).
At each step, the procedure should determine Lisa's actions based solely on the data passed at this step. Previous interaction between the procedure and main program must not affect the result. The procedure must also be deterministic: for the same input data, it must always return the same result.
An example main program (graph_tester.cpp / graph_tester.pas) is provided to run your procedure on a graph specified in your input file, and outputs the answer returned by your procedure to the output file. To use this test program, place it in the same directory as your graph.cpp / graph.pas file which contains your procedure, and create a graph.dat file in the same directory with the structure described below. Note that the main program body used for evaluation will differ from the example provided to you.
The first line contains four integers. The first three are the number of vertices , the number of edges , and the starting vertex number. Vertices are numbered from to . The fourth integer is if you are testing subproblem , or is the number of the vertex containing one pebble if you are testing subproblem . The next lines describe the edges of the graph: each line contains two numbers, the numbers of the connected vertices (in any order).
The output file contains a single line, which is either the answer returned by the procedure, or an error message in the event that any technical requirements were violated in any step.
Example 1
If your procedure violates the requirements during testing on the server, the error message could be a runtime error or a compilation error. Memory limit is not specified but you may expect at least MB. Use of global variables is allowed.
The test set consists of four blocks with additional conditions:
points: subproblem number , the graph is a tree
points: subproblem number , the graph is not necessarily a tree
points: subproblem number , the graph is a tree
points: subproblem number , the graph is not necessarily a tree