이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "towns.h"
#include <vector>
using namespace std;
int hubDistance(int N, int sub) {
  vector<vector<int>> dist(N, vector<int>(N, -1));
  auto getDist = [&dist](int a, int b) {
    if (dist[a][b] == -1)
      dist[a][b] = dist[b][a] = getDistance(a, b);
    return dist[a][b];
  };
  int D1, D2, dist_max = 0;
  for (int i = 1; i < N; ++i)
    if (getDist(0, i) > dist_max) {
      dist_max = getDist(0, i);
      D1 = i;
    }
  dist_max = 0;
  for (int i = 0; i < N; ++i)
    if (i != D1 && getDist(D1, i) > dist_max) {
      dist_max = getDist(D1, i);
      D2 = i;
    }
  int R = getDist(D1, D2);
  for (int i = 0; i < N; ++i)
    if (i != D1 && i != 0) {
      int dist_i = (getDist(i, D1) - getDist(i, 0) + getDist(D1, 0)) / 2;
      R = min(R, max(dist_i, getDist(D1, D2) - dist_i));
    }
  vector<int> leaves1, leaves2;
  int cnt1 = 0, cnt2 = 0;
  int st = 1, dr = 1;
  for (int i = 0; i < N; ++i)
    if (i != D1 && i != D2) {
      int dist_i = (getDist(i, D1) - getDist(i, 0) + getDist(D1, 0)) / 2;
      if (R == max(dist_i, getDist(D1, D2) - dist_i)) {
        if (R == dist_i) {
          ++cnt1;
          leaves1.push_back(i);
        } else {
          ++cnt2;
          leaves2.push_back(i);
        }
      } else if (dist_i < getDist(D1, D2) - dist_i) {
        ++st;
      } else {
        ++dr;
      }
    }
  //if (dr == 0)
  //  leaves2.push_back(D2);
  //else
  //++dr;
  if (st > N / 2 || dr > N / 2)
    return -R;
  vector<int> leaves;
  bool primul;
  if (leaves1.size() > leaves2.size()) {
    leaves = leaves1;
    primul = true;
    if (st + leaves2.size() > N / 2)
      return -R;
  } else {
    leaves = leaves2;
    primul = false;
    if (dr + leaves1.size() > N / 2)
      return -R;
  }
  auto sameSubtree = [&dist, &D1, &D2, &getDist](int a, int b, int _R) {
    int dist_lca = (getDist(a, D1) + getDist(b, D1) - getDist(a, b)) / 2;
    if (dist_lca <= _R)
      return false;
    return true;
  };
  int candidat, ap = 0;
  for (auto i: leaves)
    if (ap == 0) {
      candidat = i;
      ap = 1;
    } else {
      if (primul) {
        if (sameSubtree(i, candidat, R))
          ++ap;
        else
          --ap;
      } else {
        if (sameSubtree(i, candidat, getDist(D1, D2) - R))
          ++ap;
        else
          --ap;
      }
    }
  ap = 0;
  for (auto i: leaves)
    if (sameSubtree(i, candidat, R))
      ++ap;
  if (ap > N / 2)
    return -R;
  return R;
}
컴파일 시 표준 에러 (stderr) 메시지
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:62:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |     if (st + leaves2.size() > N / 2)
      |         ~~~~~~~~~~~~~~~~~~~~^~~~~~~
towns.cpp:67:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   67 |     if (dr + leaves1.size() > N / 2)
      |         ~~~~~~~~~~~~~~~~~~~~^~~~~~~
towns.cpp:6:28: warning: unused parameter 'sub' [-Wunused-parameter]
    6 | int hubDistance(int N, int sub) {
      |                        ~~~~^~~
towns.cpp:9:15: warning: 'D1' may be used uninitialized in this function [-Wmaybe-uninitialized]
    9 |     if (dist[a][b] == -1)
      |               ^
towns.cpp:10:44: warning: 'D2' may be used uninitialized in this function [-Wmaybe-uninitialized]
   10 |       dist[a][b] = dist[b][a] = getDistance(a, b);
      |                                 ~~~~~~~~~~~^~~~~~| # | 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... |