This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "towns.h"
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <map>
constexpr int maxn = 300;
int dp[maxn][maxn], c[maxn];
std::map<int, std::vector<int>> filhos;
int get(int i, int j) {
if(i > j) std::swap(i, j);
return dp[i][j] != -1 ? dp[i][j] : dp[i][j] = getDistance(i, j);
}
struct DSU
{
int pai[maxn], peso[maxn];
void init() { for(int i = 0; i < maxn; i++) pai[i] = i, peso[i] = 1; }
int find(int x) { return pai[x] == x?x:pai[x]=find(pai[x]); }
void join(int a, int b) {
a = find(a), b = find(b);
if(a == b) return;
if(peso[a] < peso[b])
std::swap(a, b);
pai[b] = a;
peso[a] += peso[b];
}
int sz(int x) { return peso[find(x)]; }
} dsu;
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int hubDistance(int N, int sub) {
memset(dp, -1, sizeof dp);
memset(c, 0, sizeof c);
filhos.clear();
dsu.init();
int a = 0, b = 0, dist = 0;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < N; j++) {
if(a != j) {
int v = get(a, j);
if(v > dist) b = j, dist = v;
}
}
if(!i) std::swap(a, b);
}
b = 0;
int R = 0x3f3f3f3f;
for(int i = 0; i < N; i++) {
if(i == a || i == b) continue;
int d1 = get(a, i), d2 = get(b, i);
c[i] = (d1 + d2 - get(a, b)) >> 1;
R = min(R, max(d1 - c[i], dist - d1 + c[i]));
filhos[d1-c[i]].push_back(i);
}
std::vector<int> lista, bucket;
int sz = 0; bool ok = 0;
for(auto it : filhos) {
if(it.first != R && it.first != dist - R) {
sz += (int)(it.second).size();
continue;
}
if(sz <= (N >> 1) && N - sz - (int)(it.second).size() <= (N >> 1)) ok = 1;
else continue;
std::vector<int> v = (it.second);
int n = (int)v.size();
if(n <= (N >> 1)) { sz += n; continue; }
for(int i = 0; i < n; i++) {
if(!lista.size()) lista.push_back(v[i]);
else if(get(v[i], lista.back()) != c[v[i]] + c[lista.back()])
bucket.push_back(v[i]);
else {
lista.push_back(v[i]);
if(bucket.size()) lista.push_back(bucket.back()),
bucket.pop_back();
}
}
// puts("lista");
// for(int x : lista)
// printf("%d\n", x);
// puts("bucket");
// for(int x : bucket)
// printf("%d\n", x);
int pivo = lista.back(); lista.pop_back();
while(bucket.size() && lista.size()) {
if(get(pivo, lista.back()) != c[lista.back()] + c[pivo])
lista.pop_back(), bucket.pop_back();
else {
lista.pop_back(); if(lista.size()) lista.pop_back();
}
}
// puts("bucket");
// for(int x : bucket)
// printf("%d\n", x);
ok &= bucket.size() < N - n;
if(ok) return R;
sz += n;
}
return -R;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:104:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
104 | ok &= bucket.size() < N - n;
| ~~~~~~~~~~~~~~^~~~~~~
towns.cpp:39:28: warning: unused parameter 'sub' [-Wunused-parameter]
39 | int hubDistance(int N, int sub) {
| ~~~~^~~
# | 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... |