This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// moreflags=grader.cpp
// really, who includes .c files in a .cpp one?
// ...
// definitely not an excuse for not learning classical algorithms properly.
#include "towns.h"
#include<algorithm>
#include<climits>
#include<map>
#include<vector>
#if not LOCAL
#define NDEBUG
#endif
#include<cassert>
int hubDistance(int number, int sub) {
int const A=0;
std::vector<int> distanceA(number);
for(int other=1; other<number; ++other)
distanceA[other]=getDistance(A, other);
int const B=int(std::max_element(begin(distanceA), end(distanceA))-distanceA.begin());
std::vector<int> distanceB(number);
auto const diameter=distanceB[A]=distanceA[B];
for(int other=1; other<number; ++other) if(other!=B)
distanceB[other]=getDistance(B, other);
// distance from B -> [(distance from path AB, node index)...]
std::map<int, std::vector<std::pair<int, int>>> groups;
for(int node=0; node<number; ++node){
auto const first=distanceA[node], sec=distanceB[node];
assert((first+sec+diameter)%2==0);
groups[(sec+diameter-first)>>1].push_back({(first+sec-diameter)>>1, node});
}
assert((groups.at(0)==std::vector<std::pair<int, int>>{{0, B}}));
assert((groups.at(diameter)==std::vector<std::pair<int, int>>{{0, A}}));
// ^ technically have side effects (memory allocation and deallocation)
int radius=INT_MAX, hubDistanceB; bool LRbalanced=false;
{
int sumLeftGroupSize=0; // exclude current group. Only count leaves
int lastGroupSize, lastr, lastDistanceB;
for(auto const& [distanceB, group]: groups){
auto const r=std::max(distanceB, diameter-distanceB); // only correct for nodes on some diameter
if(distanceB*2>=diameter){
// node and lastNode are on some diameter
// but the next node might not be
// compare node and lastNode. One of them is the hub
radius=std::min(lastr, r);
bool const lastLRBalanced=
std::max(sumLeftGroupSize-lastGroupSize, number-sumLeftGroupSize)
<=number/2;
bool const curLRBalanced=
std::max(sumLeftGroupSize, number-sumLeftGroupSize-(int)group.size())
<=number/2;
assert(not (lastLRBalanced and curLRBalanced));
if(radius==lastr){
assert(not LRbalanced);
hubDistanceB=lastDistanceB; LRbalanced=lastLRBalanced;
}
if(radius==r and curLRBalanced>=LRbalanced){ // if there are two, use the LRbalanced one
hubDistanceB=distanceB; LRbalanced=curLRBalanced;
}
break;
}
lastGroupSize=(int)group.size(); lastr=r; lastDistanceB=distanceB;
sumLeftGroupSize+=(int)group.size();
}
}
assert(radius!=INT_MAX);
if(sub<3){
return radius;
}
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:43:36: warning: declaration of 'distanceB' shadows a previous local [-Wshadow]
43 | for(auto const& [distanceB, group]: groups){
| ^
towns.cpp:23:19: note: shadowed declaration is here
23 | std::vector<int> distanceB(number);
| ^~~~~~~~~
towns.cpp:39:22: warning: variable 'hubDistanceB' set but not used [-Wunused-but-set-variable]
39 | int radius=INT_MAX, hubDistanceB; bool LRbalanced=false;
| ^~~~~~~~~~~~
towns.cpp:18:35: warning: control reaches end of non-void function [-Wreturn-type]
18 | std::vector<int> distanceA(number);
| ^
towns.cpp:42:22: warning: 'lastr' may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | int lastGroupSize, lastr, lastDistanceB;
| ^~~~~
# | 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... |