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 <bits/stdc++.h>
using namespace std;
struct UF {
map<int, int> chefs;
UF(vector<int> elems) {
for (int i: elems) chefs[i] = i;
}
int find(int x) {
if (chefs[x] == x) return x;
return chefs[x] = find(chefs[x]);
}
void unite (int a, int b) {
a = find(a);
b = find(b);
chefs[a] = b;
}
};
int hubDistance(int N, int sub) {
int d1=0, v1 = 0;
for (int i = 1; i<N; i++) {
int d = getDistance(0, i);
if (d>d1) {
d1 = d;
v1 = i;
}
}
vector<int> dists1(N);
int d2=0, v2=v1;
for (int i = 0; i<N; i++) {
int d = getDistance(v1, i);
dists1[i] = d;
if (d>d2) {
d2 = d;
v2 = i;
}
}
vector<int> dists2(N);
for (int i = 0; i<N; i++) {
int d = getDistance(v2, i);
dists2[i] = d;
}
map<int, vector<int>> joinedDiam;
int R = 1e9;
for (int i = 0; i<N; i++) {
int rem = (dists1[i]+dists2[i]-d2)/2;
joinedDiam[dists1[i]-rem].push_back(i);
R = min(R, max(dists1[i]-rem, dists2[i]-rem));
}
vector<int> possHub;
for (int i = 0; i<N; i++) {
int rem = (dists1[i]+dists2[i]-d2)/2;
if (max(dists1[i]-rem, dists2[i]-rem) == R) possHub.push_back(dists1[i]-rem);
}
int mult = -1;
for (int hub: possHub) {
int h1=0, h2=0;
bool poss = 1;
for (auto i: joinedDiam) {
if (i.first < hub) h1+=i.second.size();
if (i.first > hub) h2+=i.second.size();
if (i.first == hub) if ((int)i.second.size() > N/2) {
UF u(i.second);
for (int a: i.second) {
for (int b: i.second) {
if (a == b) continue;
int da = (dists1[a]+dists2[a]-d2)/2;
int db = (dists1[b]+dists2[b]-d2)/2;
int d = getDistance(a,b);
if (d != da+db) u.unite(a,b);
}
}
map<int, int> count;
for (auto i: u.chefs) count[i.second]++;
int Max = 0;
for (auto i: count) Max = max(Max, i.second);
if (Max > N/2) poss = 0;
}
}
if (h1 > N/2 || h2 > N/2) poss = 0;
if (poss) mult = 1;
}
return R*mult;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:82:41: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
82 | if (i.first < hub) h1+=i.second.size();
| ^
towns.cpp:83:41: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
83 | if (i.first > hub) h2+=i.second.size();
| ^
towns.cpp:100:15: warning: declaration of 'i' shadows a previous local [-Wshadow]
100 | for (auto i: u.chefs) count[i.second]++;
| ^
towns.cpp:81:13: note: shadowed declaration is here
81 | for (auto i: joinedDiam) {
| ^
towns.cpp:102:15: warning: declaration of 'i' shadows a previous local [-Wshadow]
102 | for (auto i: count) Max = max(Max, i.second);
| ^
towns.cpp:81:13: note: shadowed declaration is here
81 | for (auto i: joinedDiam) {
| ^
towns.cpp:29:28: warning: unused parameter 'sub' [-Wunused-parameter]
29 | 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... |