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;
}
int hubDistance(int N, int SUBTASK) {
int q = 0;
int n = N;
VL dist0, distd1, distd2, distl, distr, disth;
dist0 = distd1 = distd2 = distl = distr = disth = VL(n);
rep(u, n) dist0[u] = getDistance(0, u), q++;
int d1 = maxind(dist0);
rep(u, n) distd1[u] = getDistance(d1, u), q++;
int d2 = maxind(distd1);
rep(u, n) distd2[u] = getDistance(d2, u), q++;
ll R = 1e18;
rep(u, n) {
disth[u] = (distd1[u] + distd2[u] - distd1[d2])/2;
distl[u] = distd1[u]-disth[u];
distr[u] = distd2[u]-disth[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 (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;
}
}
return -R;
int cnt[4]{};
for (int x : anc) cnt[x]++;
if (SUBTASK == 4) {
if (cnt[0] <= N/2 && cnt[1] <= N/2 && cnt[2] + cnt[3] <= N/2) return +R;
if (cnt[0] + cnt[1] <= N/2 && cnt[2] <= N/2 && cnt[3] <= N/2) return +R;
return -R;
} else if (SUBTASK == 3) {
auto check = [&](int u, int v) {
return disth[u] + disth[v] != getDistance(u, v), q++;
};
VI a;
replr(HUBIND, 1, 2) {
a = VI();
rep(i, n) if (anc[i] == HUBIND) a.pb(i);
if (a.size() == 0) continue;
VI subtrees;
subtrees.pb(cnt[0]);
subtrees.pb(cnt[3]);
VI group(n);
int g = 1;
for (int i : a) if (group[i] == 0) {
group[i] = g;
subtrees.pb(1);
for (int j : a) if (group[j] == 0) {
if (check(i, j)) {
group[j] = g;
subtrees.back()++;
}
}
g++;
}
bool good = true;
for (int x : subtrees) {
if (x > N/2) good = false;
}
if (good) {
assert(q <= N * (N-1) /2);
return -R;
}
}
assert(q <= N * (N-1) /2);
return +R;
}
return -R;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:79:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
79 | return -R;
| ^~
towns.cpp:83:78: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
83 | if (cnt[0] <= N/2 && cnt[1] <= N/2 && cnt[2] + cnt[3] <= N/2) return +R;
| ^~
towns.cpp:84:78: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
84 | if (cnt[0] + cnt[1] <= N/2 && cnt[2] <= N/2 && cnt[3] <= N/2) return +R;
| ^~
towns.cpp:85:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
85 | return -R;
| ^~
towns.cpp: In lambda function:
towns.cpp:88:40: warning: value computed is not used [-Wunused-value]
88 | return disth[u] + disth[v] != getDistance(u, v), q++;
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:120:24: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
120 | return -R;
| ^~
towns.cpp:124:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
124 | return +R;
| ^~
towns.cpp:126:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
126 | return -R;
| ^~
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:54:38: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
54 | rep(u, n) distd2[u] = getDistance(d2, u), q++;
| ~~~~~~~~~~~^~~~~~~
towns.cpp:52:38: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
52 | rep(u, n) distd1[u] = getDistance(d1, u), q++;
| ~~~~~~~~~~~^~~~~~~
# | 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... |