Submission #1015353

# Submission time Handle Problem Language Result Execution time Memory
1015353 2024-07-06T09:02:13 Z 우민규(#10889) Towns (IOI15_towns) C++14
35 / 100
10 ms 348 KB
#include "towns.h"

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> qans;

int get_distance(int i, int j) {
  if (i > j) swap(i, j);
  if (qans[i][j] != -1) return qans[i][j];
  return qans[i][j] = getDistance(i, j);
}

int hubDistance(int n, int sub) {
  qans.assign(n, vector<int>(n, -1));
  for (int i = 0; i < n; ++i) qans[i][i] = 0;

  // get diameter of tree
  int u = 1;
  int uv = get_distance(0, u);
  for (int i = 2; i < n; ++i) {
    int di = get_distance(0, i);
    if (di > uv) {
      u = i;
      uv = di;
    }
  }
  int v = 0;
  for (int i = 0; i < n; ++i) {
    int di = get_distance(u, i);
    if (di > uv) {
      v = i;
      uv = di;
    }
  }
  // u - v w. length dd is diameter
  unordered_map<int, vector<int>> by_uh;
  int r = INT_MAX;
  for (int i = 0; i < n; ++i) {
    int iu = get_distance(i, u);
    int iv = get_distance(i, v);
    // 2*(i-h)
    int ih = (iu + iv - uv) / 2;
    int uh = iu - ih;
    int vh = iv - ih;
    r = min(r, max(uh, vh));
    by_uh[uh].push_back(i);
  }
  if (by_uh.count(r)) {
    int cu = 0, cr = 0, cv = 0;
    for (auto& [k, v] : by_uh) {
      if (k < r) cu += v.size();
      if (k == r) cr += v.size();
      if (k > r) cv += v.size();
    }
    if (cu <= n / 2 && cv <= n / 2) {
      if (cr <= n / 2) return r;
    }
  }
  if (by_uh.count(uv - r)) {
    int cu = 0, cr = 0, cv = 0;
    for (auto& [k, v] : by_uh) {
      if (k < uv - r) cu += v.size();
      if (k == uv - r) cr += v.size();
      if (k > uv - r) cv += v.size();
    }
    if (cu <= n / 2 && cv <= n / 2) {
      if (cr <= n / 2) return r;
    }
  }
  return -r;
}

#ifndef EVAL
#include "grader.cpp"
#endif

Compilation message

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:51:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   51 |     for (auto& [k, v] : by_uh) {
      |                ^
towns.cpp:51:20: warning: declaration of 'v' shadows a previous local [-Wshadow]
   51 |     for (auto& [k, v] : by_uh) {
      |                    ^
towns.cpp:28:7: note: shadowed declaration is here
   28 |   int v = 0;
      |       ^
towns.cpp:52:31: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   52 |       if (k < r) cu += v.size();
      |                               ^
towns.cpp:53:32: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   53 |       if (k == r) cr += v.size();
      |                                ^
towns.cpp:54:31: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   54 |       if (k > r) cv += v.size();
      |                               ^
towns.cpp:62:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   62 |     for (auto& [k, v] : by_uh) {
      |                ^
towns.cpp:62:20: warning: declaration of 'v' shadows a previous local [-Wshadow]
   62 |     for (auto& [k, v] : by_uh) {
      |                    ^
towns.cpp:28:7: note: shadowed declaration is here
   28 |   int v = 0;
      |       ^
towns.cpp:63:36: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   63 |       if (k < uv - r) cu += v.size();
      |                                    ^
towns.cpp:64:37: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   64 |       if (k == uv - r) cr += v.size();
      |                                     ^
towns.cpp:65:36: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
   65 |       if (k > uv - r) cv += v.size();
      |                                    ^
towns.cpp:14:28: warning: unused parameter 'sub' [-Wunused-parameter]
   14 | int hubDistance(int n, int sub) {
      |                        ~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 344 KB Output is correct
2 Correct 7 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 9 ms 348 KB Output is correct
5 Correct 10 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 348 KB Output is correct
2 Correct 6 ms 348 KB Output is correct
3 Correct 8 ms 348 KB Output is correct
4 Correct 9 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -