This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "towns.h"
int maxind(VL& a) {
int ret;
ll mx = -1e18;
rep(i, a.size()) if (a[i] > mx) {
mx = a[i];
ret = i;
}
return ret;
}
map<PII, int> distances;
int getdist(int u, int v) {
if (u == v) return 0;
if (u > v) swap(u, v);
if (!distances.count({u, v})) distances[{u, v}] = getDistance(u, v);
return distances[{u, v}];
}
int hubDistance(int N, int SUBTASK) {
distances = map<PII, int>();
int n = N;
/*rep(u, n) rep(v, n) getdist(u, v);*/
VL dist0, distd1, distl, distr, disth;
dist0 = distd1 = distl = distr = disth = VL(n);
rep(u, n) dist0[u] = getdist(0, u);
int d1 = maxind(dist0);
rep(u, n) distd1[u] = getdist(d1, u);
int d2 = maxind(distd1);
ll R = 1e18;
rep(u, n) {
disth[u] = (distd1[u] + dist0[u] - dist0[d1])/2;
if (disth[u] == dist0[u]) disth[u] = -1;
else {
distl[u] = distd1[u]-disth[u];
distr[u] = distd1[d2] - distl[u];
setmin(R, max(distl[u], distr[u]));
}
}
VI anc(n);
/* 0, on the left
* 1, first hub
* 2, second hub
* 3, on the right
*/
rep(u, n) {
if (disth[u] == -1) {
anc[u] = 3;
} else if (max(distl[u], distr[u]) == R) {
if (distl[u] <= distr[u]) anc[u] = 1;
else anc[u] = 2;
} else {
if (distl[u] <= distr[u]) anc[u] = 0;
else anc[u] = 3;
}
}
int cnt[4]{};
for (int x : anc) cnt[x]++;
auto check = [&](int u, int v) {
return disth[u] + disth[v] != getdist(u, v);
};
VI a;
replr(HUBIND, 1, 2) {
a = VI();
rep(i, n) if (anc[i] == HUBIND) a.pb(i);
if (a.size() == 0) continue;
if (HUBIND == 1) {
if (cnt[0] > N/2) continue;
if (cnt[2] + cnt[3] > N/2) continue;
} else {
if (cnt[0] + cnt[1] > N/2) continue;
if (cnt[3] > N/2) continue;
}
stack<int> st;
int c = 0;
for (int i : a) {
if (!st.size()) st.push(i);
else {
int m = st.top();
if (check(i, m)) c++;
else {
st.push(i);
if (c) {
c--;
st.push(m);
}
}
}
}
int m = st.top();
int ans = c+1;
st.pop();
if (st.size()) st.pop();
while (st.size()) {
if (check(st.top(), m)) {
ans++;
st.pop();
if (st.size()) st.pop();
else c++;
} else {
st.pop();
c--;
if (c == -1) return +R;
}
}
if (c == 0) return +R;
if (ans <= n/2) return +R;
}
return -R;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:139:37: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
139 | if (c == -1) return +R;
| ^~
towns.cpp:142:28: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
142 | if (c == 0) return +R;
| ^~
towns.cpp:143:32: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
143 | if (ans <= n/2) return +R;
| ^~
towns.cpp:145:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
145 | return -R;
| ^~
towns.cpp:52:28: warning: unused parameter 'SUBTASK' [-Wunused-parameter]
52 | int hubDistance(int N, int SUBTASK) {
| ~~~~^~~~~~~
towns.cpp: In function 'int maxind(VL&)':
towns.cpp:41:12: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
41 | return ret;
| ^~~
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:65:52: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | disth[u] = (distd1[u] + dist0[u] - dist0[d1])/2;
| ^
towns.cpp:69:33: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
69 | distr[u] = distd1[d2] - distl[u];
| ^
# | 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... |