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>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef std::pair<int,int> pii;
typedef long long ll;
using namespace std;
const int N = 120;
int dis0[N], disa[N];//, disb[N];
int dis[N][N];
int cnt;
namespace dsu {
int par[N];
int sz[N];
void init() {
fill(par, par+N, -1);
fill(sz, sz+N, 1);
}
int rt(int v) { return par[v] == -1? v: (par[v] = rt(par[v])); }
void unite(int v, int u) {
//v = rt(v);
//u = rt(u);
//if (sz[v] < sz[u])
// swap(v, u);
sz[v] += sz[u];
par[u] = v;
}
}
int get(int i, int j)
{
if (i == j)
return 0;
if (i > j)
swap(i, j);
if (!dis[i][j]) {
++cnt;
dis[i][j] = getDistance(i, j);
}
return dis[i][j];
}
int get2(int a, int v, int b)
{
return (get(a, v) + get(a, b) - get(v, b))/2;
}
int find_hub(int a, int b, int n, int r)
{
if (get(a, b) == r*2)
return r;
bool x = 0, y = 0;
vector<int> X, Y;
Loop (i,0,n) {
if (get2(a, i, 0) == r)
x = 1;
if (get2(a, i, 0) == get(a, b) - r)
y = 1;
int x = get2(a, i, 0);
int y = get(a, b) - x;
(x < y? Y: X).push_back(i);
}
if (!x)
return get(a, b) - r;
if (!y)
return r;
if (X.size()*2 > n)
return r;
if (X.size()*2 == n)
return -1;
return get(a, b) - r;
}
mt19937_64 rd(time(0));
int sel(const vector<int> &val)
{
int sum = accumulate(val.begin(), val.end(), 0);
int x = rd() % sum;
Loop (i,0,val.size()) {
if (x < val[i])
return i;
x -= val[i];
}
assert(false);
}
int hubDistance(int n, int sub) {
memset(dis, 0, sizeof(dis));
cnt = 0;
Loop (i,0,n)
dis0[i] = get(0, i);
int a = max_element(dis0, dis0+n) - dis0;
Loop (i,0,n)
disa[i] = get(a, i);
int b = max_element(disa, disa+n) - disa;
//Loop (i,0,n)
// disb[i] = get(b, i);
int ans = 1e9;
Loop (i,0,n) {
int x = get2(a, i, 0);
int y = disa[b] - x;
ans = min(ans, max(x, y));
}
int v = find_hub(a, b, n, ans);
if (v == -1)
return ans;
vector<int> L, M, R;
Loop (i,0,n) {
(get2(a, i, 0) < v? L:
get2(a, i, 0) > v? R:
M).push_back(i);
}
if (L.size()*2 > n || R.size()*2 > n)
return -ans;
if (M.size()*2 <= n)
return ans;
//cout << a << ' ' << b << '\n';
//for (int x : M)
// cout << x << ' ';
//cout << '\n';
//for (int x : M) {
// int sz = 1;
// for (int y : M) {
// if (x == y || get2(x, a, b) + get2(y, a, b) == get(x, y))
// continue;
// sz++;
// }
// if (sz*2 > n)
// return -ans;
//}
dsu::init();
//for (int x : M) for (int y : M) {
// x = dsu::rt(x);
// y = dsu::rt(y);
// if (x == y || get2(x, a, b) + get2(y, a, b) == get(x, y))
// continue;
// dsu::unite(x, y);
// //cout << x << ' ' << y << " -> " << dsu::sz[x] << '\n';
// if (dsu::sz[x]*2 > n)
// return -ans;
//}
for (int dard = 0; sub >= 3 && dard < 200000 && cnt < 5*n; ++dard) {
vector<int> vec, sz;
for (int v : M) {
if (dsu::par[v] != -1)
continue;
vec.push_back(v);
sz.push_back(dsu::sz[v] * dsu::sz[v]);
}
//int x = dsu::rt(M[rd()%M.size()]);
int y = dsu::rt(M[rd()%M.size()]);
int x = sel(sz);
x = vec[x];
//vector<int> vec;
//for (int v : M) {
// int r = rt(v);
// if (r != x)
// vec.push_back(r);
//}
//int y = vec[rd()%vec.size()];
if (x == y || get2(x, a, 0) + get2(y, a, 0) == get(x, y))
continue;
dsu::unite(x, y);
//cout << x << ' ' << y << " -> " << dsu::sz[x] << '\n';
if (dsu::sz[x]*2 > n)
return -ans;
}
return ans;
}
Compilation message (stderr)
towns.cpp: In function 'int find_hub(int, int, int, int)':
towns.cpp:58:15: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
58 | if (get2(a, i, 0) == r)
| ^
towns.cpp:60:15: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
60 | if (get2(a, i, 0) == get(a, b) - r)
| ^
towns.cpp:62:7: warning: declaration of 'x' shadows a previous local [-Wshadow]
62 | int x = get2(a, i, 0);
| ^
towns.cpp:55:7: note: shadowed declaration is here
55 | bool x = 0, y = 0;
| ^
towns.cpp:62:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
62 | int x = get2(a, i, 0);
| ^
towns.cpp:63:7: warning: declaration of 'y' shadows a previous local [-Wshadow]
63 | int y = get(a, b) - x;
| ^
towns.cpp:55:14: note: shadowed declaration is here
55 | bool x = 0, y = 0;
| ^
towns.cpp:64:27: warning: conversion from 'll' {aka 'long long int'} to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
64 | (x < y? Y: X).push_back(i);
| ^
towns.cpp:70:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
70 | if (X.size()*2 > n)
| ~~~~~~~~~~~^~~
towns.cpp:72:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
72 | if (X.size()*2 == n)
| ~~~~~~~~~~~^~~~
towns.cpp: In function 'int sel(const std::vector<int>&)':
towns.cpp:81:15: warning: conversion from 'std::mersenne_twister_engine<long unsigned int, 64, 312, 156, 31, 13043109905998158313, 29, 6148914691236517205, 17, 8202884508482404352, 37, 18444473444759240704, 43, 6364136223846793005>::result_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
81 | int x = rd() % sum;
| ~~~~~^~~~~
towns.cpp:3:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
| ^
towns.cpp:82:2: note: in expansion of macro 'Loop'
82 | Loop (i,0,val.size()) {
| ^~~~
towns.cpp:84:11: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
84 | return i;
| ^
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:94:20: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
94 | dis0[i] = get(0, i);
| ^
towns.cpp:95:36: warning: conversion from 'long int' to 'int' may change value [-Wconversion]
95 | int a = max_element(dis0, dis0+n) - dis0;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
towns.cpp:97:20: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
97 | disa[i] = get(a, i);
| ^
towns.cpp:98:36: warning: conversion from 'long int' to 'int' may change value [-Wconversion]
98 | int b = max_element(disa, disa+n) - disa;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
towns.cpp:103:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
103 | int x = get2(a, i, 0);
| ^
towns.cpp:112:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
112 | (get2(a, i, 0) < v? L:
| ^
towns.cpp:113:12: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
113 | get2(a, i, 0) > v? R:
| ^
towns.cpp:114:36: warning: conversion from 'll' {aka 'long long int'} to 'std::vector<int>::value_type' {aka 'int'} may change value [-Wconversion]
114 | M).push_back(i);
| ^
towns.cpp:116:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
116 | if (L.size()*2 > n || R.size()*2 > n)
| ~~~~~~~~~~~^~~
towns.cpp:116:35: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
116 | if (L.size()*2 > n || R.size()*2 > n)
| ~~~~~~~~~~~^~~
towns.cpp:118:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
118 | if (M.size()*2 <= n)
| ~~~~~~~~~~~^~~~
towns.cpp:147:12: warning: declaration of 'v' shadows a previous local [-Wshadow]
147 | for (int v : M) {
| ^
towns.cpp:107:6: note: shadowed declaration is here
107 | int v = find_hub(a, b, n, ans);
| ^
# | 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... |