Submission #560316

# Submission time Handle Problem Language Result Execution time Memory
560316 2022-05-11T09:26:02 Z kartel Fountain Parks (IOI21_parks) C++17
45 / 100
1684 ms 229656 KB
#include <bits/stdc++.h>
//#include "grader.cpp"
#include "parks.h"
#define F first
#define S second
#define pb push_back
#define sz(x) (int)x.size()
#define el "\n"
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;

const int N = 1e6 + 500;

const int steps[4][2] = {{0, 2}, {2, 0}, {0, -2}, {-2, 0}};

int pr[N];
int cmp[2 * N], to[2 * N];
vector <int> g[2 * N], gr[2 * N], ord;
bool mk[2 * N];

int f(int v) {return (pr[v] == v ? v : pr[v] = f(pr[v]));}
void link(int v, int u) {
    v = f(v); u = f(u);
    pr[u] = v;
}

int nt(int x) {return (x < N ? x + N : x - N);}

void add_impl(int a, int b) {
    int na = nt(a), nb = nt(b);

    g[na].pb(b); gr[b].pb(na);
    g[nb].pb(a); gr[a].pb(nb);
}

void dfs(int v) {
    mk[v] = 1;
    for (auto u : g[v]) {
        if (mk[u]) {
            continue;
        }
        dfs(u);
    }
    ord.pb(v);
}

int construct_roads(vector <int> x, vector <int> y) {
    int n = sz(x);
    map <pair <int, int>, int> mp, used;
    vector <pair <int, int> > r;

    for (int i = 0; i < n; i++) {
        mp[{x[i], y[i]}] = i;
        pr[i] = i;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 4; j++) {
            int cx = x[i] + steps[j][0];
            int cy = y[i] + steps[j][1];

            if (mp.count({cx, cy}) && !used[{i, mp[{cx, cy}]}] && f(i) != f(mp[{cx, cy}])) {
                int to = mp[{cx, cy}];
                link(i, to);
                r.pb({i, to});
                used[{to, i}] = 1;
                used[{i, to}] = 1;
            }
        }
    }
    sort(all(r));
    r.resize(unique(all(r)) - r.begin());

    for (int i = 0; i < n; i++) {
        if (f(i) != f(0)) {
            return 0;
        }
    }
    vector <int> u, v, A, B;
    map <pair <int, int>, vector <int> > e;

    for (int i = 0; i < sz(r); i++) {
        int a = r[i].F, b = r[i].S;
        u.pb(a); v.pb(b);
        if (abs(x[a] - x[b]) == 0) {
            int lx = x[a];
            int ly = min(y[a], y[b]);

            e[{lx - 1, ly + 1}].pb(i);
            e[{lx + 1, ly + 1}].pb(nt(i));
        } else {
            int lx = min(x[a], x[b]);
            int ly = y[a];

            e[{lx + 1, ly + 1}].pb(i);
            e[{lx + 1, ly - 1}].pb(nt(i));
        }
    }
    for (auto [p, v] : e) {
        int x = p.F, y = p.S;
        if (sz(v) == 1) {
            continue;
        }
        assert(sz(v) == 2);
        int a = v[0], b = v[1];

        add_impl(a, b);
    }

    for (int i = 0; i < 2 * N; i++) {
        cmp[i] = -1;
        if (mk[i]) {
            continue;
        }
        dfs(i);
    }
    reverse(all(ord));

    int C = 0;
    for (auto v : ord) {
        function <void(int)> dfsr = [&](int v) {
            if (cmp[v] != -1) {
                return;
            }
            cmp[v] = C;
            for (auto u : gr[v]) {
                dfsr(u);
            }
        };

        dfsr(v);
        C++;
    }

    for (int i = 0; i < N; i++) {
        if (cmp[i] == cmp[nt(i)]) {
            return 0;
        }
        to[i] = (cmp[i] > cmp[nt(i)]);
    }

    for (int i = 0; i < sz(r); i++) {
        int a = r[i].F, b = r[i].S;
        if (abs(x[a] - x[b]) == 0) {
            int lx = x[a];
            int ly = min(y[a], y[b]);

            if (to[i]) {
                A.pb(lx + 1);
            } else {
                A.pb(lx - 1);
            }
            B.pb(ly + 1);
        } else {
            int lx = min(x[a], x[b]);
            int ly = y[a];

            if (to[i]) {
                B.pb(ly - 1);
            } else {
                B.pb(ly + 1);
            }
            A.pb(lx + 1);
        }
    }

    build(u, v, A, B);
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:101:13: warning: unused variable 'x' [-Wunused-variable]
  101 |         int x = p.F, y = p.S;
      |             ^
parks.cpp:101:22: warning: unused variable 'y' [-Wunused-variable]
  101 |         int x = p.F, y = p.S;
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 74 ms 115896 KB Output is correct
2 Correct 77 ms 115864 KB Output is correct
3 Correct 45 ms 94172 KB Output is correct
4 Correct 76 ms 115864 KB Output is correct
5 Correct 78 ms 115888 KB Output is correct
6 Correct 45 ms 94288 KB Output is correct
7 Correct 45 ms 94220 KB Output is correct
8 Correct 45 ms 94284 KB Output is correct
9 Correct 581 ms 165720 KB Output is correct
10 Correct 113 ms 120732 KB Output is correct
11 Correct 235 ms 142568 KB Output is correct
12 Correct 117 ms 123280 KB Output is correct
13 Correct 130 ms 104136 KB Output is correct
14 Correct 48 ms 94476 KB Output is correct
15 Correct 48 ms 94736 KB Output is correct
16 Correct 610 ms 165740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 115896 KB Output is correct
2 Correct 77 ms 115864 KB Output is correct
3 Correct 45 ms 94172 KB Output is correct
4 Correct 76 ms 115864 KB Output is correct
5 Correct 78 ms 115888 KB Output is correct
6 Correct 45 ms 94288 KB Output is correct
7 Correct 45 ms 94220 KB Output is correct
8 Correct 45 ms 94284 KB Output is correct
9 Correct 581 ms 165720 KB Output is correct
10 Correct 113 ms 120732 KB Output is correct
11 Correct 235 ms 142568 KB Output is correct
12 Correct 117 ms 123280 KB Output is correct
13 Correct 130 ms 104136 KB Output is correct
14 Correct 48 ms 94476 KB Output is correct
15 Correct 48 ms 94736 KB Output is correct
16 Correct 610 ms 165740 KB Output is correct
17 Runtime error 115 ms 190932 KB Execution killed with signal 6
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 74 ms 115896 KB Output is correct
2 Correct 77 ms 115864 KB Output is correct
3 Correct 45 ms 94172 KB Output is correct
4 Correct 76 ms 115864 KB Output is correct
5 Correct 78 ms 115888 KB Output is correct
6 Correct 45 ms 94288 KB Output is correct
7 Correct 45 ms 94220 KB Output is correct
8 Correct 45 ms 94284 KB Output is correct
9 Correct 581 ms 165720 KB Output is correct
10 Correct 113 ms 120732 KB Output is correct
11 Correct 235 ms 142568 KB Output is correct
12 Correct 117 ms 123280 KB Output is correct
13 Correct 130 ms 104136 KB Output is correct
14 Correct 48 ms 94476 KB Output is correct
15 Correct 48 ms 94736 KB Output is correct
16 Correct 610 ms 165740 KB Output is correct
17 Runtime error 115 ms 190932 KB Execution killed with signal 6
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 74 ms 115896 KB Output is correct
2 Correct 77 ms 115864 KB Output is correct
3 Correct 45 ms 94172 KB Output is correct
4 Correct 76 ms 115864 KB Output is correct
5 Correct 78 ms 115888 KB Output is correct
6 Correct 45 ms 94288 KB Output is correct
7 Correct 45 ms 94220 KB Output is correct
8 Correct 45 ms 94284 KB Output is correct
9 Correct 581 ms 165720 KB Output is correct
10 Correct 113 ms 120732 KB Output is correct
11 Correct 235 ms 142568 KB Output is correct
12 Correct 117 ms 123280 KB Output is correct
13 Correct 130 ms 104136 KB Output is correct
14 Correct 48 ms 94476 KB Output is correct
15 Correct 48 ms 94736 KB Output is correct
16 Correct 610 ms 165740 KB Output is correct
17 Correct 82 ms 115880 KB Output is correct
18 Correct 80 ms 115888 KB Output is correct
19 Correct 43 ms 94308 KB Output is correct
20 Correct 1520 ms 229656 KB Output is correct
21 Correct 1508 ms 223124 KB Output is correct
22 Correct 1470 ms 223328 KB Output is correct
23 Correct 1125 ms 200712 KB Output is correct
24 Correct 394 ms 112488 KB Output is correct
25 Correct 1064 ms 138932 KB Output is correct
26 Correct 918 ms 138940 KB Output is correct
27 Correct 1379 ms 215568 KB Output is correct
28 Correct 1370 ms 215600 KB Output is correct
29 Correct 1413 ms 215660 KB Output is correct
30 Correct 1458 ms 215424 KB Output is correct
31 Correct 76 ms 115892 KB Output is correct
32 Correct 134 ms 123196 KB Output is correct
33 Correct 175 ms 103764 KB Output is correct
34 Correct 1413 ms 228228 KB Output is correct
35 Correct 66 ms 96460 KB Output is correct
36 Correct 192 ms 105668 KB Output is correct
37 Correct 430 ms 117108 KB Output is correct
38 Correct 537 ms 157380 KB Output is correct
39 Correct 790 ms 172628 KB Output is correct
40 Correct 1054 ms 188892 KB Output is correct
41 Correct 1337 ms 204316 KB Output is correct
42 Correct 1684 ms 219836 KB Output is correct
43 Correct 77 ms 115892 KB Output is correct
44 Correct 76 ms 115940 KB Output is correct
45 Correct 77 ms 115884 KB Output is correct
46 Correct 44 ms 94284 KB Output is correct
47 Correct 46 ms 94236 KB Output is correct
48 Correct 81 ms 116060 KB Output is correct
49 Correct 83 ms 115960 KB Output is correct
50 Correct 77 ms 115892 KB Output is correct
51 Correct 81 ms 115896 KB Output is correct
52 Correct 52 ms 94288 KB Output is correct
53 Correct 80 ms 115892 KB Output is correct
54 Correct 50 ms 94644 KB Output is correct
55 Correct 54 ms 94912 KB Output is correct
56 Correct 650 ms 166840 KB Output is correct
57 Correct 1025 ms 189680 KB Output is correct
58 Correct 1039 ms 189100 KB Output is correct
59 Correct 43 ms 94284 KB Output is correct
60 Correct 77 ms 115896 KB Output is correct
61 Correct 43 ms 94272 KB Output is correct
62 Correct 1284 ms 215456 KB Output is correct
63 Correct 1283 ms 215412 KB Output is correct
64 Correct 1282 ms 215060 KB Output is correct
65 Correct 52 ms 95100 KB Output is correct
66 Correct 59 ms 96040 KB Output is correct
67 Correct 643 ms 166592 KB Output is correct
68 Correct 1088 ms 192008 KB Output is correct
69 Correct 1526 ms 217480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 115896 KB Output is correct
2 Correct 77 ms 115864 KB Output is correct
3 Correct 45 ms 94172 KB Output is correct
4 Correct 76 ms 115864 KB Output is correct
5 Correct 78 ms 115888 KB Output is correct
6 Correct 45 ms 94288 KB Output is correct
7 Correct 45 ms 94220 KB Output is correct
8 Correct 45 ms 94284 KB Output is correct
9 Correct 581 ms 165720 KB Output is correct
10 Correct 113 ms 120732 KB Output is correct
11 Correct 235 ms 142568 KB Output is correct
12 Correct 117 ms 123280 KB Output is correct
13 Correct 130 ms 104136 KB Output is correct
14 Correct 48 ms 94476 KB Output is correct
15 Correct 48 ms 94736 KB Output is correct
16 Correct 610 ms 165740 KB Output is correct
17 Correct 1347 ms 216096 KB Output is correct
18 Correct 1288 ms 216204 KB Output is correct
19 Correct 1444 ms 227888 KB Output is correct
20 Correct 1612 ms 224296 KB Output is correct
21 Correct 1480 ms 206072 KB Output is correct
22 Correct 76 ms 115872 KB Output is correct
23 Correct 216 ms 132528 KB Output is correct
24 Correct 105 ms 99272 KB Output is correct
25 Correct 308 ms 111848 KB Output is correct
26 Correct 589 ms 124376 KB Output is correct
27 Correct 702 ms 170220 KB Output is correct
28 Correct 923 ms 183844 KB Output is correct
29 Correct 1160 ms 197440 KB Output is correct
30 Correct 1425 ms 210260 KB Output is correct
31 Correct 1665 ms 224508 KB Output is correct
32 Correct 1541 ms 221904 KB Output is correct
33 Correct 1320 ms 215580 KB Output is correct
34 Correct 55 ms 95408 KB Output is correct
35 Correct 64 ms 96340 KB Output is correct
36 Correct 672 ms 167752 KB Output is correct
37 Correct 1108 ms 193904 KB Output is correct
38 Correct 1571 ms 220716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 115896 KB Output is correct
2 Correct 77 ms 115864 KB Output is correct
3 Correct 45 ms 94172 KB Output is correct
4 Correct 76 ms 115864 KB Output is correct
5 Correct 78 ms 115888 KB Output is correct
6 Correct 45 ms 94288 KB Output is correct
7 Correct 45 ms 94220 KB Output is correct
8 Correct 45 ms 94284 KB Output is correct
9 Correct 581 ms 165720 KB Output is correct
10 Correct 113 ms 120732 KB Output is correct
11 Correct 235 ms 142568 KB Output is correct
12 Correct 117 ms 123280 KB Output is correct
13 Correct 130 ms 104136 KB Output is correct
14 Correct 48 ms 94476 KB Output is correct
15 Correct 48 ms 94736 KB Output is correct
16 Correct 610 ms 165740 KB Output is correct
17 Runtime error 115 ms 190932 KB Execution killed with signal 6
18 Halted 0 ms 0 KB -