Submission #606135

# Submission time Handle Problem Language Result Execution time Memory
606135 2022-07-26T05:13:45 Z KoD Fountain Parks (IOI21_parks) C++17
45 / 100
2188 ms 70264 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] == 1) {
            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] == 1) {
                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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 244 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 489 ms 30552 KB Output is correct
10 Correct 19 ms 3284 KB Output is correct
11 Correct 134 ms 16496 KB Output is correct
12 Correct 35 ms 4820 KB Output is correct
13 Correct 100 ms 12728 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 525 ms 30632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 244 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 489 ms 30552 KB Output is correct
10 Correct 19 ms 3284 KB Output is correct
11 Correct 134 ms 16496 KB Output is correct
12 Correct 35 ms 4820 KB Output is correct
13 Correct 100 ms 12728 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 525 ms 30632 KB Output is correct
17 Incorrect 1 ms 212 KB Tree @(3, 3) appears more than once: for edges on positions 0 and 2
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 244 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 489 ms 30552 KB Output is correct
10 Correct 19 ms 3284 KB Output is correct
11 Correct 134 ms 16496 KB Output is correct
12 Correct 35 ms 4820 KB Output is correct
13 Correct 100 ms 12728 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 525 ms 30632 KB Output is correct
17 Incorrect 1 ms 212 KB Tree @(3, 3) appears more than once: for edges on positions 0 and 2
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 244 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 489 ms 30552 KB Output is correct
10 Correct 19 ms 3284 KB Output is correct
11 Correct 134 ms 16496 KB Output is correct
12 Correct 35 ms 4820 KB Output is correct
13 Correct 100 ms 12728 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 525 ms 30632 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1124 ms 60972 KB Output is correct
21 Correct 1307 ms 60936 KB Output is correct
22 Correct 1111 ms 60992 KB Output is correct
23 Correct 1031 ms 52284 KB Output is correct
24 Correct 327 ms 18336 KB Output is correct
25 Correct 1346 ms 56064 KB Output is correct
26 Correct 1415 ms 56140 KB Output is correct
27 Correct 1482 ms 60892 KB Output is correct
28 Correct 1414 ms 60824 KB Output is correct
29 Correct 1509 ms 60992 KB Output is correct
30 Correct 1436 ms 60944 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 46 ms 4444 KB Output is correct
33 Correct 135 ms 9284 KB Output is correct
34 Correct 1187 ms 61016 KB Output is correct
35 Correct 25 ms 3140 KB Output is correct
36 Correct 208 ms 14228 KB Output is correct
37 Correct 497 ms 28220 KB Output is correct
38 Correct 499 ms 24784 KB Output is correct
39 Correct 651 ms 33472 KB Output is correct
40 Correct 967 ms 43708 KB Output is correct
41 Correct 1273 ms 52272 KB Output is correct
42 Correct 1480 ms 60944 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 280 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 0 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 575 ms 30536 KB Output is correct
57 Correct 941 ms 45196 KB Output is correct
58 Correct 909 ms 45320 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 1376 ms 60940 KB Output is correct
63 Correct 1267 ms 60900 KB Output is correct
64 Correct 1222 ms 60580 KB Output is correct
65 Correct 8 ms 1364 KB Output is correct
66 Correct 18 ms 2456 KB Output is correct
67 Correct 649 ms 30540 KB Output is correct
68 Correct 1078 ms 46516 KB Output is correct
69 Correct 1727 ms 60900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 244 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 489 ms 30552 KB Output is correct
10 Correct 19 ms 3284 KB Output is correct
11 Correct 134 ms 16496 KB Output is correct
12 Correct 35 ms 4820 KB Output is correct
13 Correct 100 ms 12728 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 525 ms 30632 KB Output is correct
17 Correct 1402 ms 60832 KB Output is correct
18 Correct 1340 ms 60888 KB Output is correct
19 Correct 1391 ms 60880 KB Output is correct
20 Correct 2188 ms 70264 KB Output is correct
21 Correct 1523 ms 56860 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 130 ms 10156 KB Output is correct
24 Correct 78 ms 6600 KB Output is correct
25 Correct 419 ms 22384 KB Output is correct
26 Correct 990 ms 37196 KB Output is correct
27 Correct 670 ms 33760 KB Output is correct
28 Correct 923 ms 41788 KB Output is correct
29 Correct 1199 ms 51276 KB Output is correct
30 Correct 1499 ms 59364 KB Output is correct
31 Correct 1758 ms 67272 KB Output is correct
32 Correct 1662 ms 67352 KB Output is correct
33 Correct 1334 ms 60836 KB Output is correct
34 Correct 13 ms 1800 KB Output is correct
35 Correct 24 ms 3156 KB Output is correct
36 Correct 714 ms 32272 KB Output is correct
37 Correct 1230 ms 49036 KB Output is correct
38 Correct 1666 ms 64360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 244 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 489 ms 30552 KB Output is correct
10 Correct 19 ms 3284 KB Output is correct
11 Correct 134 ms 16496 KB Output is correct
12 Correct 35 ms 4820 KB Output is correct
13 Correct 100 ms 12728 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 4 ms 724 KB Output is correct
16 Correct 525 ms 30632 KB Output is correct
17 Incorrect 1 ms 212 KB Tree @(3, 3) appears more than once: for edges on positions 0 and 2
18 Halted 0 ms 0 KB -