Submission #823749

#TimeUsernameProblemLanguageResultExecution timeMemory
823749becaidoFountain Parks (IOI21_parks)C++17
100 / 100
466 ms42664 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "parks.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

#ifdef WAIMAI
void build(vector<int> u, vector<int> v, vector<int> a, vector<int> b);
#endif

const int SIZE = 2e5 + 5;

int to[SIZE], h[SIZE];
int dsu(int x) {
    return x == to[x] ? x : (to[x] = dsu(to[x]));
}
void merge(int a, int b) {
    a = dsu(a), b = dsu(b);
    if (a == b) return;
    if (h[a] < h[b]) swap(a, b);
    to[b] = a;
    h[a] += (h[a] == h[b]);
}

int construct_roads(vector<int> x, vector<int> y) {
    int n = x.size();
    map<pair<int, int>, int> mp;
    FOR (i, 0, n - 1) mp[{x[i], y[i]}] = i;
    vector<pair<int, int>> e;
    FOR (i, 0, n - 1) {
        if (mp.count({x[i] + 2, y[i]})) {
            int j = mp[{x[i] + 2, y[i]}];
            e.pb(i, j);
        }
        if (mp.count({x[i], y[i] + 2})) {
            int j = mp[{x[i], y[i] + 2}];
            e.pb(i, j);
        }
    }
    sort(e.begin(), e.end(), [&](auto l, auto r) {
        return make_tuple(x[l.F], x[l.S], y[l.F], y[l.S]) < make_tuple(x[r.F], x[r.S], y[r.F], y[r.S]);
    });
    vector<int> u, v, mx, my;
    set<pair<int, int>> s;
    iota(to, to + n, 0);
    for (auto [a, b] : e) if (dsu(a) != dsu(b)) {
        debug(a, b, x[a], y[a], x[b], y[b]);
        if (x[a] == x[b]) {
            int cx = x[a] + ((x[a] + y[a]) % 4 ? 1 : -1);
            int cy = (y[a] + y[b]) / 2;
            if (!s.count({cx, cy})) {
                s.emplace(cx, cy);
                merge(a, b);
                u.pb(a), v.pb(b);
                mx.pb(cx), my.pb(cy);
            }
        } else {
            int cx = (x[a] + x[b]) / 2;
            int cy = y[a] + ((x[a] + y[a]) % 4 ? -1 : 1);
            if (!s.count({cx, cy})) {
                s.emplace(cx, cy);
                merge(a, b);
                u.pb(a), v.pb(b);
                mx.pb(cx), my.pb(cy);
            }
        }
    }
    if (u.size() != n - 1) return 0;
    build(u, v, mx, my);
    return 1;
}

#ifdef WAIMAI
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;
}
#endif

Compilation message (stderr)

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:16:20: warning: statement has no effect [-Wunused-value]
   16 | #define debug(...) 7122
      |                    ^~~~
parks.cpp:66:9: note: in expansion of macro 'debug'
   66 |         debug(a, b, x[a], y[a], x[b], y[b]);
      |         ^~~~~
parks.cpp:87:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   87 |     if (u.size() != n - 1) return 0;
      |         ~~~~~~~~~^~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...