Submission #606127

# Submission time Handle Problem Language Result Execution time Memory
606127 2022-07-26T05:11:09 Z KoD Fountain Parks (IOI21_parks) C++17
45 / 100
1793 ms 70752 KB
#include "parks.h"
#include <bits/stdc++.h>

using std::array;
using std::pair;
using std::tuple;
using std::vector;

template <class F>
struct fixed : private F {
    explicit fixed(F&& f) : F(std::forward<F>(f)) {}
    template <class... Args>
    decltype(auto) operator()(Args&&... args) const {
        return F::operator()(*this, std::forward<Args>(args)...);
    }
};

struct UnionFind {
    vector<int> data;
    explicit UnionFind(const int n) : data(n, -1) {}
    int find(const int u) {
        return data[u] < 0 ? u : data[u] = find(data[u]);
    }
    bool merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        if (data[x] > data[y]) std::swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }
};

struct Points {
    vector<pair<int, int>> pts;
    std::map<pair<int, int>, int> map;

    Points() : pts(), map() {}

    int add_or_get(const pair<int, int>& p) {
        if (const auto itr = map.find(p); itr != map.end()) {
            return itr->second;
        } else {
            pts.push_back(p);
            return map[p] = (int)pts.size() - 1;
        }
    }

    int try_get(const pair<int, int>& p) const {
        if (const auto itr = map.find(p); itr != map.end()) {
            return itr->second;
        } else {
            return -1;
        }
    }
};

int construct_roads(vector<int> X, vector<int> Y) {
    const int F = (int)X.size();    
    Points fountain, road, bench;
    for (int i = 0; i < F; ++i) {
        fountain.add_or_get({X[i], Y[i]});
    }
    vector<pair<int, int>> edges;
    for (int i = 0; i < F; ++i) {
        if (const int j = fountain.try_get({X[i] + 2, Y[i]}); j != -1) {
            const int r = road.add_or_get({X[i] + 1, Y[i]});
            if ((X[i] + Y[i]) % 4 == 0) {
                const int b = bench.add_or_get({X[i] + 1, Y[i] + 1});
                edges.emplace_back(r, b);
            } else {
                const int b = bench.add_or_get({X[i] + 1, Y[i] - 1});
                edges.emplace_back(r, b);
            }
        }
        if (const int j = fountain.try_get({X[i], Y[i] + 2}); j != -1) {
            const int r = road.add_or_get({X[i], Y[i] + 1});
            if ((X[i] + Y[i]) % 4 == 0) {
                const int b = bench.add_or_get({X[i] - 1, Y[i] + 1});
                edges.emplace_back(r, b);
            } else {
                const int b = bench.add_or_get({X[i] + 1, Y[i] + 1});
                edges.emplace_back(r, b);
            }
        }
    }
    const int R = (int)road.pts.size();
    const int B = (int)bench.pts.size();
    vector<int> type(B), deg(B);
    for (const auto& [r, b] : edges) {
        deg[b] += 1;
    }
    for (int i = 0; i < B; ++i) {
        const auto& [x, y] = bench.pts[i];
        type[i] = (x / 2 + y / 2) / 2;
    }
    vector<char> use(R, true), visited(B);
    const auto dfs = fixed([&](auto&& dfs, const int r, const int b) -> void {
        if (r == -1 or b == -1 or deg[b] != 2 or visited[b]) {
            return;
        }
        use[r] = false;
        visited[b] = true;
        const auto& [x, y] = bench.pts[b];
        if (type[b] == 0) {
            dfs(road.try_get({x, y - 1}), bench.try_get({x, y - 2}));
            dfs(road.try_get({x, y + 1}), bench.try_get({x, y + 2}));
        } else {
            dfs(road.try_get({x - 1, y}), bench.try_get({x - 2, y}));
            dfs(road.try_get({x + 1, y}), bench.try_get({x + 2, y}));
        }
    });
    for (int src = 0; src < B; ++src) {
        if (deg[src] == 1) {
            const auto& [x, y] = bench.pts[src];
            if (type[src] == 0) {
                dfs(road.try_get({x, y - 1}), bench.try_get({x, y - 2}));
                dfs(road.try_get({x, y + 1}), bench.try_get({x, y + 2}));
            } else {
                dfs(road.try_get({x - 1, y}), bench.try_get({x - 2, y}));
                dfs(road.try_get({x + 1, y}), bench.try_get({x + 2, y}));
            }
        }
    }
    vector<int> match(R);
    for (const auto& [r, b] : edges) {
        if (use[r]) {
            match[r] = b;
        }
    }
    UnionFind dsu(F);
    vector<int> u, v, a, b;
    for (int i = 0; i < R; ++i) {
        const auto& [x, y] = road.pts[i];
        if (x % 2 == 1) {
            const int s = fountain.try_get({x - 1, y});
            const int t = fountain.try_get({x + 1, y});
            if (dsu.merge(s, t)) {
                u.push_back(s);
                v.push_back(t);
                a.push_back(bench.pts[match[i]].first);
                b.push_back(bench.pts[match[i]].second);
            }
        } else {
            const int s = fountain.try_get({x, y - 1});
            const int t = fountain.try_get({x, y + 1});
            if (dsu.merge(s, t)) {
                u.push_back(s);
                v.push_back(t);
                a.push_back(bench.pts[match[i]].first);
                b.push_back(bench.pts[match[i]].second);
            }
        }
    }
    if ((int)u.size() < F - 1) {
        return 0;
    }
    build(u, v, a, b); 
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 451 ms 30672 KB Output is correct
10 Correct 25 ms 3384 KB Output is correct
11 Correct 129 ms 16688 KB Output is correct
12 Correct 30 ms 4800 KB Output is correct
13 Correct 88 ms 12864 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 482 ms 30656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 451 ms 30672 KB Output is correct
10 Correct 25 ms 3384 KB Output is correct
11 Correct 129 ms 16688 KB Output is correct
12 Correct 30 ms 4800 KB Output is correct
13 Correct 88 ms 12864 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 482 ms 30656 KB Output is correct
17 Incorrect 1 ms 212 KB Tree @(3, 3) appears more than once: for edges on positions 0 and 1
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 451 ms 30672 KB Output is correct
10 Correct 25 ms 3384 KB Output is correct
11 Correct 129 ms 16688 KB Output is correct
12 Correct 30 ms 4800 KB Output is correct
13 Correct 88 ms 12864 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 482 ms 30656 KB Output is correct
17 Incorrect 1 ms 212 KB Tree @(3, 3) appears more than once: for edges on positions 0 and 1
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 451 ms 30672 KB Output is correct
10 Correct 25 ms 3384 KB Output is correct
11 Correct 129 ms 16688 KB Output is correct
12 Correct 30 ms 4800 KB Output is correct
13 Correct 88 ms 12864 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 482 ms 30656 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 963 ms 61276 KB Output is correct
21 Correct 1320 ms 61132 KB Output is correct
22 Correct 1124 ms 61192 KB Output is correct
23 Correct 1014 ms 52472 KB Output is correct
24 Correct 315 ms 18708 KB Output is correct
25 Correct 1288 ms 56460 KB Output is correct
26 Correct 1316 ms 56456 KB Output is correct
27 Correct 1364 ms 61108 KB Output is correct
28 Correct 1462 ms 61192 KB Output is correct
29 Correct 1388 ms 61164 KB Output is correct
30 Correct 1424 ms 61100 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 45 ms 4676 KB Output is correct
33 Correct 105 ms 9480 KB Output is correct
34 Correct 1082 ms 61140 KB Output is correct
35 Correct 25 ms 3260 KB Output is correct
36 Correct 188 ms 14520 KB Output is correct
37 Correct 629 ms 28444 KB Output is correct
38 Correct 447 ms 25140 KB Output is correct
39 Correct 666 ms 33740 KB Output is correct
40 Correct 906 ms 43836 KB Output is correct
41 Correct 1199 ms 52496 KB Output is correct
42 Correct 1376 ms 61212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 0 ms 304 KB Output is correct
46 Correct 1 ms 300 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 1 ms 212 KB Output is correct
50 Correct 1 ms 212 KB Output is correct
51 Correct 1 ms 300 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 0 ms 212 KB Output is correct
54 Correct 4 ms 852 KB Output is correct
55 Correct 6 ms 1108 KB Output is correct
56 Correct 503 ms 30872 KB Output is correct
57 Correct 861 ms 45276 KB Output is correct
58 Correct 872 ms 45420 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1166 ms 61136 KB Output is correct
63 Correct 1219 ms 61320 KB Output is correct
64 Correct 1199 ms 60912 KB Output is correct
65 Correct 8 ms 1388 KB Output is correct
66 Correct 17 ms 2516 KB Output is correct
67 Correct 594 ms 30792 KB Output is correct
68 Correct 1053 ms 46668 KB Output is correct
69 Correct 1521 ms 61088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 451 ms 30672 KB Output is correct
10 Correct 25 ms 3384 KB Output is correct
11 Correct 129 ms 16688 KB Output is correct
12 Correct 30 ms 4800 KB Output is correct
13 Correct 88 ms 12864 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 482 ms 30656 KB Output is correct
17 Correct 1254 ms 61148 KB Output is correct
18 Correct 1230 ms 61148 KB Output is correct
19 Correct 1193 ms 61108 KB Output is correct
20 Correct 1793 ms 70752 KB Output is correct
21 Correct 1321 ms 57192 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 127 ms 10192 KB Output is correct
24 Correct 74 ms 6688 KB Output is correct
25 Correct 369 ms 22624 KB Output is correct
26 Correct 778 ms 37644 KB Output is correct
27 Correct 679 ms 34040 KB Output is correct
28 Correct 918 ms 42104 KB Output is correct
29 Correct 1157 ms 51628 KB Output is correct
30 Correct 1468 ms 59624 KB Output is correct
31 Correct 1700 ms 67556 KB Output is correct
32 Correct 1485 ms 67624 KB Output is correct
33 Correct 1250 ms 61160 KB Output is correct
34 Correct 13 ms 1832 KB Output is correct
35 Correct 32 ms 3020 KB Output is correct
36 Correct 581 ms 32604 KB Output is correct
37 Correct 959 ms 49468 KB Output is correct
38 Correct 1492 ms 64620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 451 ms 30672 KB Output is correct
10 Correct 25 ms 3384 KB Output is correct
11 Correct 129 ms 16688 KB Output is correct
12 Correct 30 ms 4800 KB Output is correct
13 Correct 88 ms 12864 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 852 KB Output is correct
16 Correct 482 ms 30656 KB Output is correct
17 Incorrect 1 ms 212 KB Tree @(3, 3) appears more than once: for edges on positions 0 and 1
18 Halted 0 ms 0 KB -