#include "towns.h"
#include<bits/stdc++.h>
using namespace std;
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}];
}
int hubDistance(int N, int sub){
res.clear();
vector<int> d1(N, 0), d2(N, 0);
int bsf = 0, f1, f2;
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);
d1[i] = d;
if (d > bsf){
bsf = d;
f2 = i;
}
}
int R = pow(10, 9);
map<int,vector<int>> m;
for (int i=0; i<N; i++){
if (i == f1 || i == f2) continue;
int d = getDist(f2, i);
d2[i] = d;
int l = (d1[i]+d2[i]-bsf)/2;
R = min(R, max(d1[i], d2[i])-l);
m[d1[i]-l].push_back(i);
}
bool bal = false;
int nh = 0;
for (auto it=m.begin(); it != m.end(); it++){
if (it->first == R || it->first == bsf-R){
nh++;
int x1 = 1, x2 = it->second.size(), x3 = 1;
for (auto jt=m.begin(); jt != it; jt++) x1 += jt->second.size();
for (auto jt=next(it); jt != m.end(); jt++) x3 += jt->second.size();
if (x1 <= N/2 && x2 <= N/2 && x3 <= N/2) bal = true;
}
}
if (bal == true) return R;
if (nh == 2) return -R;
for (auto it=m.begin(); it != m.end(); it++){
if (it->first == R || it->first == bsf-R){
int x1 = 1, x2 = 0, x3 = 1;
for (auto jt=m.begin(); jt != it; jt++) x1 += jt->second.size();
for (auto jt=next(it); jt != m.end(); jt++) x3 += jt->second.size();
vector<int> l1, l2;
for (int v : it->second){
if (l1.empty()) l1.push_back(v);
if (d1[v]+d1[l1.back()]-getDist(v, l1.back()) > 2*it->first) l2.push_back(v);
else {
l1.push_back(v);
if (!l2.empty()){
l1.push_back(l2.back());
l2.pop_back();
}
}
}
for (int v : it->second){
if (d1[v]+d1[l1.back()]-getDist(v, l1.back()) > 2*it->first) x2++;
}
//cout << x1 << " " << x2 << " " << x3 << endl;
if (x1 <= N/2 && x2 <= N/2 && x3 <= N/2) bal = true;
}
}
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... |