Problem: ioi15_f
Categories: graph, search
Difficulty: hard
Time limit: 2000 ms | Memory limit: 1024 MB

There are $N$ small towns in Kazakhstan, numbered from $0$ through $N - 1$. There is also an unknown number of large cities. The small towns and large cities of Kazakhstan are jointly called settlements.

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 $a$ and $b$ there is a unique way in which one can go from $a$ to $b$ using the highways, as long as no highway is used more than once.

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 11 small towns and 7 large cities. Small towns are depicted as circles and labeled by integers, large cities are depicted as squares and labeled by letters.

![](https://ioi.contest.codeforces.com/espresso/d9ad919321a7cefe2c132ba7f744c8f8e156c096.png)

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 $C$ we can measure the distance $r(C)$ to the small town that is the farthest away from that city. A large city $C$ is a hub if the distance $r(C)$ is the smallest among all large cities. The distance between a hub and a small town that is farthest away from the hub will be denoted by $R$. Thus, $R$ is the smallest of all values $r(C)$.

In the above example the farthest small town from city $a$ is town 8, and the distance between them is $r(a) = 1 + 4 + 12 = 17$. For city $g$ we also have $r(g) = 17$. (One of the small towns that are farthest away from $g$ is town 6.) The only hub in the above example is city $f$, with $r(f) = 16$. Hence, in the above example $R$ is 16.

Removing a hub divides the network into multiple connected pieces. A hub is balanced if each of those pieces contains at most $\lfloor \frac{N}{2} \rfloor$ small towns. (We stress that we do not count the large cities.) Note that $\lfloor x \rfloor$ denotes the largest integer which is not greater than $x$.

In our example, city $f$ is a hub. If we remove city $f$, the network will break into four connected pieces. These four pieces consist of the following sets of small towns: {$0, 1, 10$}, {$2, 3$}, {$4,5,6,7$}, and {$8, 9$}. None of these pieces has more than $\lfloor \frac{11}{2} \rfloor = 5$ small towns, hence city $f$ is a balanced hub.

Task

Initially, the only information you have about the network of settlements and highways is the number $N$ of small towns. You do not know the number of large cities. You also do not know anything about the layout of highways in the country. You can only obtain new information by asking queries about distances between pairs of small towns.

Your task is to determine:

* In all subtasks: the distance $R$.
* 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 40. For each test case the grader will call your function 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 Scoring section).
  + If $sub$ is 1 or 2, the function can return either $R$ or $-R$.
  + if $sub$ is greater than 2, if there exists a balanced hub then the function must return $R$, otherwise it must return $-R$.

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 $i$ and $j$. Note that if $i$ and $j$ are equal, the function returns 0. It also returns 0 when the arguments are invalid.

Input

The sample grader reads the input in the following format:

* line 1: Subtask number and the number of test cases.
* line 2: $N_1$, the number of small towns in the first test case.
* following $N_1$ lines: The $j$-th number $(1\le j \le N_1)$ in the $i$-th of these lines $(1 \le i \le N_1)$ is the distance between small towns $i - 1$ and $j - 1$.
* The next test cases follow. They are given in the same format as the first test case.

Output

For each test case, the sample grader prints the return value of hubDistance and the number of calls made on separate lines.

Scoring

In each test case:

* $N$ is between $6$ and $110$ inclusive.
* The distance between any two distinct small towns is between $1$ and $1\,000\,000$ 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 | $\frac{N(N-1)}{2}$ | NO | — |
| 2 | 12 | $\lceil \frac{7N}{2} \rceil$ | NO | — |
| 3 | 13 | $\frac{N(N-1)}{2}$ | YES | — |
| 4 | 10 | $\lceil \frac{7N}{2} \rceil$ | YES | Each large city is connected to exactly three settlements |
| 5 | 13 | $5n$ | YES | — |
| 6 | 39 | $\lceil \frac{7N}{2} \rceil$ | YES | — |

Note that $\lceil x \rceil$ denotes the smallest integer which is greater than or equal to $x$.

Note

Note that the subtask number is a part of the input. The sample grader changes its behavior according to the subtask number.

The input file corresponding to the example above is:

```
1 1  
11  
0 17 18 20 17 12 20 16 23 20 11  
17 0 23 25 22 17 25 21 28 25 16  
18 23 0 12 21 16 24 20 27 24 17  
20 25 12 0 23 18 26 22 29 26 19  
17 22 21 23 0 9 21 17 26 23 16  
12 17 16 18 9 0 16 12 21 18 11  
20 25 24 26 21 16 0 10 29 26 19  
16 21 20 22 17 12 10 0 25 22 15  
23 28 27 29 26 21 29 25 0 21 22  
20 25 24 26 23 18 26 22 21 0 19  
11 16 17 19 16 11 19 15 22 19 0
```

This format is quite different from specifying the list of highways. Note that you are allowed to modify sample graders, so that they use a different input format.

Interactor / grader files:
grader.cpp: #include "towns.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "graderlib.c"

int main() {
    FILE *f;
    f = freopen("towns.in","r",stdin);
    assert(f != NULL);
    f = freopen("towns.out","w",stdout);
    assert(f != NULL);
    int ncase, R, N;
    int subtask;
    int ret;
    ret = scanf("%d%d",&subtask,&ncase);
    assert(ret == 2);
    for (int i = 0; i < ncase; i++) {
        ret = scanf("%d",&N);
	assert(ret == 1);
        _ini_query(N,subtask);
        R=hubDistance(N,subtask);
        printf("%d\n",R);
    }
    return 0;
}

towns.cpp: #include "towns.h"

int hubDistance(int N, int sub) {
	int R = getDistance(0,1);
	return R;
}

graderlib.c: #include <stdio.h>
#include <stdlib.h>

static int _N;
static int _dist[110][110];
static int _quota, _user_query;

void _ini_query(int N, int k) {
    int ret;
    _N = N;
    for (int i = 0; i < _N; i++) 
        for (int j = 0; j < _N; j++)  {
	    ret = scanf("%d", &_dist[i][j]);
            assert(ret == 1);
        }
    if (k == 1 || k == 3) _quota = _N * (_N - 1) / 2;
    else if (k == 2 || k == 4 || k == 6) _quota = (7 * _N + 1) / 2;
    else _quota = 5 * _N;
    _user_query = 0;
}


int getDistance(int i, int j) {
    _user_query++;
    if (_user_query > _quota) {
        printf("0\n");
        exit(0);
    }
    if (i < 0 || i >= _N) return 0;
    if (j < 0 || j >= _N) return 0;
    return _dist[i][j];
}


towns.h: #ifndef towns_h
#define towns_h

int getDistance(int i, int j);
int hubDistance(int N, int sub);

#endif