There are
All the settlements of Kazakhstan are connected by a single network of bidirectional highways. Each highway connects two distinct settlements, and each pair of settlements is directly connected by at most one highway. For each pair of settlements
It is known that each small town is directly connected to a single other settlement, and each large city is directly connected to three or more settlements.
The following figure shows a network of
Every highway has a positive integer length. The distance between two settlements is the minimum sum of the lengths of the highways one needs to travel in order to get from one settlement to the other.
For each large city
In the above example the farthest small town from city
Removing a hub divides the network into multiple connected pieces. A hub is balanced if each of those pieces contains at most
In our example, city
Task
Initially, the only information you have about the network of settlements and highways is the number
Your task is to determine:
- In all subtasks: the distance
. - In subtasks 3 to 6: whether there is a balanced hub in the network.
You need to implement the function hubDistance
. The grader will evaluate multiple test cases in a single run. The number of test cases per run is at most hubDistance
exactly once. Make sure that your function initializes all necessary variables every time it is called.
int hubDistance(int N, int sub)
N
: the number of small towns.sub
: the subtask number (explained in the Subtasks section).- If
sub
is 1 or 2, the function can return either or . - If
sub
is greater than 2, if there exists a balanced hub then the function must return , otherwise it must return .
int getDistance(int i, int j)
Your function hubDistance
can obtain information about the network of highways by calling the grader function getDistance(i, j)
. This function returns the distance between the small towns
Subtasks
In each test case:
is between and inclusive.- The distance between any two distinct small towns is between
and inclusive.
The number of queries your program may make is limited. The limit varies by subtask, as given in the table below. If your program tries to exceed the limit on the number of queries, it will be terminated and it will be assumed to have given an incorrect answer.
subtask | points | number of queries | find balanced hub | additional constraints |
---|---|---|---|---|
1 | 13 | NO | none | |
2 | 12 | NO | none | |
3 | 13 | YES | none | |
4 | 10 | YES | each large city is connected to exactly three settlements | |
5 | 13 | YES | none | |
6 | 39 | YES | none |
Note that
Comments