## IOI '17 P5 - Simurgh

View as PDF

Points: 40 (partial)
Time limit: 1.4s
Memory limit: 1G

Problem type
Allowed languages
C, C++

According to ancient Persian legends in Shahnameh, Zal, the legendary Persian hero, is madly in love with Rudaba, the princess of Kabul. When Zal asked for Rudaba's hand in marriage, her father gave him a challenge.

In Persia there are cities, labeled from to , and two-way roads, labeled from to . Each road connects a pair of distinct cities. Each pair of cities is connected by at most one road. Some of the roads are royal roads used for travels by royals. Zal's task is to determine which of the roads are the royal roads.

Zal has a map with all the cities and the roads in Persia. He does not know which of the roads are royal, but he can get help from Simurgh, the benevolent mythical bird who is Zal's protector. However, Simurgh does not want to reveal the set of royal roads directly. Instead, she tells Zal that the set of all royal roads is a golden set. A set of roads is a golden set if and only if:

• it has exactly roads, and
• for every pair of cities, it is possible to reach one from the other by traveling only along the roads of this set.

Furthermore, Zal can ask Simurgh some questions. For each question:

1. Zal chooses a golden set of roads, and then
2. Simurgh tells Zal how many of the roads in the chosen golden set are royal roads.

Your program should help Zal find the set of royal roads by asking Simurgh at most questions. The grader will play the role of Simurgh.

#### Implementation details

You should implement the following procedure:

int[] find_roads(int n, int[] u, int[] v)
• : number of cities,
• and : arrays of length . For all , and are the cities connected by road .
• This procedure should return an array of length containing the labels of the royal roads (in an arbitrary order).

Your solution can make at most calls to the following grader procedure:

int count_common_roads(int[] r)
• : array of length containing the labels of roads in a golden set (in an arbitrary order).
• This procedure returns the number of royal roads in .

#### Example

find_roads(4, [0, 0, 0, 1, 1, 2], [1, 2, 3, 2, 3, 3])

In this example there are cities and roads. We denote by a road connecting cities and . The roads are labeled from to in the following order: and . Every golden set has roads.

Assume that the royal roads are the roads labeled , , and , that is, the roads and . Then:

• count_common_roads([0, 1, 2]) returns . This query is about roads labeled , , and , that is, roads and . Two of them are royal roads.
• count_common_roads([5, 1, 0]) returns . This query is about the set of all royal roads.

The procedure find_roads should return or any other array of length that contains these three elements.

Note that the following calls are not allowed:

• count_common_roads([0, 1]): here the length of is not .
• count_common_roads([0, 1, 3]): here does not describe a golden set, because it is impossible to travel from city to only using the roads .

#### Constraints

• for all
• For all , road connects two different cities (i.e., ).
• There is at most one road between each pair of cities.
• It is possible to travel between any pair of cities through the roads.
• The set of all royal roads is a golden set.
• find_roads should call count_common_roads at most times. In each call, the set of roads specified by should be a golden set.

1. (13 points)
2. (17 points)
3. (21 points)
4. (19 points) and there is a road between every pair of cities
5. (30 points)

• line :
• line for all :
• line :

Here, are the labels of the royal roads.

The sample grader outputs YES, if find_roads calls count_common_roads at most times, and returns the correct set of royal roads. Otherwise, it outputs NO.

Beware that the procedure count_common_roads in the sample grader does not check whether has all properties of a golden set. Instead, it counts and returns the number of labels of royal roads in the array . However, if the program you submit calls count_common_roads with a set of labels that does not describe a golden set, the grading verdict will be Wrong Answer.

#### Technical note

The procedure count_common_roads in C++ and Pascal uses the pass by reference method for efficiency reasons. You can still call the procedure in the usual way. The grader is guaranteed not to change the value of .