#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
//void build(std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> b);
ll dx[4] = {2,-2,0,0};
ll dy[4] = {0,0,2,-2};
map<ii,ll>mp;
ll p[200005];
ll find_par(ll a){
if(p[a] == a){
return a;
}
return p[a] = find_par(p[a]);
}
void enose(ll a,ll b){
ll p1 = find_par(a);
ll p2 = find_par(b);
p[p1] = p2;
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
ll n = x.size();
f(i,0,n){
mp[ii(x[i],y[i])] = i+1;
p[i+1] = i+1;
}
vector<int>u,v,a,b;
f(i,0,n){
f(j,0,4){
ll X = x[i] + dx[j];
ll Y = x[i] + dy[j];
ll pos = mp[ii(X,Y)];
if(pos && find_par(i+1) != find_par(pos)){
enose(i+1,pos);
u.pb(i+1);
v.pb(pos);
}
}
}
a.assign(n-1,0);
b.assign(n-1,0);
if((ll)u.size() != n-1){
return 0;
}
mp.clear();
map<ii,vll>ex;
set<iii>s;
f(i,0,n-1){
ll A = (x[u[i]] + x[v[i]]) / 2;
ll B = (y[u[i]] + y[v[i]]) / 2;
if(A % 2 == 0){
mp[ii(A+1,B)]++;
mp[ii(A-1,B)]++;
ex[ii(A+1,B)].pb(i);
ex[ii(A-1,B)].pb(i);
}
else{
mp[ii(A,B+1)]++;
mp[ii(A,B-1)]++;
ex[ii(A,B+1)].pb(i);
ex[ii(A,B-1)].pb(i);
}
}
for(auto x:mp){
s.insert(iii(x.S,x.F));
}
while(!s.empty()){
auto it = s.begin();
for(auto z:ex[(*it).S]){
if(a[z] == 0){
a[z] = (*it).S.F;
b[z] = (*it).S.S;
break;
}
}
s.erase((*it));
}
build(u,v,a,b);
return 1;
}
/*
static void check(bool cond, string message) {
if (!cond) {
printf("%s\n", message.c_str());
fclose(stdout);
exit(0);
}
}
static int n;
static bool build_called;
static int m;
static vector<int> _u, _v, _a, _b;
void build(vector<int> u, vector<int> v, vector<int> a, vector<int> b) {
check(!build_called, "build is called more than once");
build_called = true;
m = u.size();
check(int(v.size()) == m, "u.size() != v.size()");
check(int(a.size()) == m, "u.size() != a.size()");
check(int(b.size()) == m, "u.size() != b.size()");
_u = u;
_v = v;
_a = a;
_b = b;
}
int main() {
assert(scanf("%d", &n) == 1);
vector<int> x(n), y(n);
for (int i = 0; i < n; i++) {
assert(scanf("%d%d", &x[i], &y[i]) == 2);
}
fclose(stdin);
build_called = false;
const int possible = construct_roads(x, y);
check(possible == 0 || possible == 1, "Invalid return value of construct_roads()");
if (possible == 1) {
check(build_called, "construct_roads() returned 1 without calling build()");
} else {
check(!build_called, "construct_roads() called build() but returned 0");
}
printf("%d\n", possible);
if (possible == 1) {
printf("%d\n", m);
for (int j = 0; j < m; j++) {
printf("%d %d %d %d\n", _u[j], _v[j], _a[j], _b[j]);
}
}
fclose(stdout);
return 0;
}
*/
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:98:5: error: 'build' was not declared in this scope
98 | build(u,v,a,b);
| ^~~~~