// #include "citymapping.h"
#include <bits/stdc++.h>
#include "citymapping.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int MAXN = 1000;
int t = 0;
mt19937 rng(6);
ll dist[2][MAXN];
class bucket {
public:
vector<int> elems;
int path_elem;
int min_elem;
ll mn_dist;
bool m;
bucket() {}
bucket(int path, bool mn = 1) {
min_elem = -1;
path_elem = path;
m = mn;
mn_dist = 0;
}
void add(int i, ll d) {
if (m && (min_elem == -1 || mn_dist > d)) {
min_elem = i;
mn_dist = d;
}
elems.push_back(i);
}
};
void make_ans(vector<int> &vals, int a[], int b[], int w[]) {
if (vals.size() < 2) return;
if (vals.size() == 2) {
a[t] = vals[0];
b[t] = vals[1];
w[t] = get_distance(vals[0], vals[1]);
t++;
return;
}
int n = vals.size();
int f = rng()%n;
// int mx = -1;
for (int i = 0; i < n; i++) {
if (i == f) continue;
dist[0][i] = get_distance(vals[i], vals[f]);
// if (mx == -1 || dist[0][i] > dist[0][mx]) mx = i;
}
mx = f;
while (mx == f) mx = rng()%n;
dist[0][f] = 0;
for (int i = 0; i < n; i++) {
if (i == f || i == mx) continue;
dist[1][i] = get_distance(vals[i], vals[mx]);
}
dist[1][mx] = 0;
dist[1][f] = dist[0][mx];
vector<pli> path;
map<ll, bucket> buckets;
for (int i = 0; i < n; i++) {
if (dist[0][i]+dist[1][i] == dist[0][mx]) {
path.push_back(pli(dist[0][i], i));
buckets[dist[1][i]-dist[0][i]] = bucket(i, (i != f && i != mx));
}
}
sort(path.begin(), path.end());
for (int i = 0; i < path.size()-1; i++) {
a[t] = vals[path[i].second];
b[t] = vals[path[i+1].second];
w[t] = dist[0][path[i+1].second]-dist[0][path[i].second];
t++;
}
for (int i = 0; i < n; i++) {
if (dist[0][i]+dist[1][i] != dist[0][mx] || i == mx || i == f)
buckets[dist[1][i]-dist[0][i]].add(vals[i], dist[0][i]);
}
for (auto it: buckets) {
bucket bu = it.second;
if (bu.m && bu.min_elem != -1) {
a[t] = vals[bu.path_elem];
b[t] = bu.min_elem;
w[t] = bu.mn_dist-dist[0][bu.path_elem];
// assert(bu.mn_dist > 0);
assert(w[t] >= 0);
t++;
}
}
for (auto it: buckets) {
make_ans(it.second.elems, a, b, w);
}
}
void find_roads(int n, int q, int a[], int b[], int w[]) {
vector<int> v(n);
iota(v.begin(), v.end(), 1);
make_ans(v, a, b, w);
assert(t == n-1);
// for (int i = 0; i < t; i++) assert(w[t] >= 0);
return;
}
Compilation message
citymapping.cpp: In function 'void make_ans(std::vector<int>&, int*, int*, int*)':
citymapping.cpp:56:2: error: 'mx' was not declared in this scope
56 | mx = f;
| ^~
citymapping.cpp:74:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < path.size()-1; i++) {
| ~~^~~~~~~~~~~~~~~