#include "towns.h"
#include<bits/stdc++.h>
using namespace std;
int f1, f2;
map<pair<int,int>,int> res;
int getDist(int a, int b){
	if (a == b) return 0;
	if (a > b) swap(a, b);
	if (res.find({a, b}) == res.end()) res[{a, b}] = getDistance(a, b);
	return res[{a, b}];
}
bool eq(int a, int b){
	return getDist(f1, a)+getDist(f1, b)-getDist(a, b);
}
int hubDistance(int N, int sub){
	res.clear();
	int bsf = 0;
	for (int i=1; i<N; i++){
		int d = getDist(0, i);
		if (d > bsf){
			bsf = d;
			f1 = i;
		}
	}
	bsf = 0;
	for (int i=0; i<N; i++){
		if (i == f1) continue;
		int d = getDist(f1, i);
		if (d > bsf){
			bsf = d;
			f2 = i;
		}
	}
	int diam = getDist(f1, f2);
	map<int,vector<int>> m;
	int R = pow(10, 9);
	for (int i=0; i<N; i++){
		int jp = (getDist(f1, 0)+getDist(f1, i)-getDist(0, i))/2;
		m[jp].push_back(i);
		R = min(R, max(jp, diam-jp));
	}
	int xl = 0, xm, xr = N;
	bool bal = false;
	for (auto [jp, v] : m){
		xr -= v.size();
		xm = v.size();
		if ((jp == R || diam-jp == R) && xl <= N/2 && xr <= N/2){
			if (xm <= N/2) bal = true;
			/*else {
				vector<pair<int,int>> w, d;
				for (int x : v) w.push_back({x, 1});
				while (w.size() > 1){
					vector<pair<int,int>> nw;
					if (w.size() % 2){
						nw.push_back(w.back());
						w.pop_back();
					}
					for (int i=0; i<(int)w.size(); i+=2){
						if (eq(w[i].first, w[i+1].second) == 2*jp){
							d.push_back(w[i]);
							d.push_back(w[i+1]);
						}
						else nw.push_back({w[i].first, w[i].second+w[i+1].second});
					}
					w = nw;
				}
				if (w.size()){
					int bc = w[0].second;
					for (pair<int,int> dead : d){
						if (eq(w[0].first, dead.first) != 2*jp) bc += dead.second;
					}
					if (bc <= N/2) bal = true;
				}
				else bal = true;
			}*/
		}
		xl += v.size();
	}
	if (bal) return R;
	else return -R;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |