답안 #1072033

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1072033 2024-08-23T13:33:50 Z shmax 분수 공원 (IOI21_parks) C++17
55 / 100
928 ms 83464 KB
#include "parks.h"
#include <bits/stdc++.h>


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

using namespace std;

using i32 = int;
//#define int long long
#define len(x) (int)(x.size())
template<typename T>
using vec = vector<T>;

struct DSU {
public:
    DSU() : _n(0) {}

    explicit DSU(int n) : _n(n), parent_or_size(n, -1) {}

    int unite(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool one(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
                std::remove_if(result.begin(), result.end(),
                               [&](const std::vector<int> &v) { return v.empty(); }),
                result.end());
        return result;
    }

private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;
};


i32 construct_roads(std::vector<i32> _x, std::vector<i32> _y) {
    int n = len(_x);
    map<pair<int, int>, int> mp;
    for (int i = 0; i < n; i++) {
        mp[{_x[i], _y[i]}] = i;
    }
    mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
    vec<pair<int, int>> edges;
    for (int i = 0; i < n; i++) {
        vec<pair<int, int>> moves = {{-2, 0},
                                     {2,  0},
                                     {0,  -2},
                                     {0,  2}};
        for (auto [dx, dy]: moves) {
            int nx = _x[i] + dx;
            int ny = _y[i] + dy;
            if (!mp.count({nx, ny})) continue;
            edges.push_back({i, mp[{nx, ny}]});
        }
    }
    for (int times = 0; times < 1; times++) {
        DSU dsu(n);

        shuffle(edges.begin(), edges.end(), rnd);

        vec<pair<int, int>> used_edges;
        for (auto [a, b]: edges) {
            if (dsu.one(a, b)) continue;
            dsu.unite(a, b);
            used_edges.emplace_back(a, b);
        }
        if (dsu.size(0) != n) return 0;
        map<pair<int, int>, int> benches;
        vec<pair<int, int>> benches_pos;
        int idx = 0;
        for (auto [a, b]: used_edges) {
            auto [x1, y1] = pair<int, int>{_x[a], _y[a]};
            auto [x2, y2] = pair<int, int>{_x[b], _y[b]};
            if (x1 == x2) {
                if (!benches.count({x1 - 1, (y1 + y2) / 2}))
                    benches[{x1 - 1, (y1 + y2) / 2}] = idx++, benches_pos.emplace_back(x1 - 1, (y1 + y2) / 2);
                if (!benches.count({x1 + 1, (y1 + y2) / 2}))
                    benches[{x1 + 1, (y1 + y2) / 2}] = idx++, benches_pos.emplace_back(x1 + 1, (y1 + y2) / 2);
            } else {
                if (!benches.count({(x1 + x2) / 2, y1 - 1}))
                    benches[{(x1 + x2) / 2, y1 - 1}] = idx++, benches_pos.emplace_back((x1 + x2) / 2, y1 - 1);
                if (!benches.count({(x1 + x2) / 2, y1 + 1}))
                    benches[{(x1 + x2) / 2, y1 + 1}] = idx++, benches_pos.emplace_back((x1 + x2) / 2, y1 + 1);
            }
        }
        vec<vec<int>> g(n - 1 + len(benches));
        int idt = 0;
        for (auto [a, b]: used_edges) {
            auto [x1, y1] = pair<int, int>{_x[a], _y[a]};
            auto [x2, y2] = pair<int, int>{_x[b], _y[b]};
            if (x1 == x2) {
                g[idt].push_back(n - 1 + benches[{x1 - 1, (y1 + y2) / 2}]);
                g[idt].push_back(n - 1 + benches[{x1 + 1, (y1 + y2) / 2}]);
            } else {
                g[idt].push_back(n - 1 + benches[{(x1 + x2) / 2, y1 - 1}]);
                g[idt].push_back(n - 1 + benches[{(x1 + x2) / 2, y1 + 1}]);
            }
            idt++;
        }

//        matching
//        shuffle
        for (int i = 0; i < n - 1; i++) {
            shuffle(g[i].begin(), g[i].end(), rnd);
        }
        vec<int> match(n - 1 + len(benches), -1);
        vec<bool> used(n - 1 + len(benches), false);
        function<bool(int)> dfs = [&](int v) -> bool {
            if (used[v]) return false;
            used[v] = true;
            for (auto u: g[v]) {
                if (match[u] == -1 || dfs(match[u])) {
                    match[u] = v;
                    match[v] = u;
                    return true;
                }
            }
            return false;
        };

        bool flag = true;
        do {
            flag = false;
            fill(used.begin(), used.end(), false);
            for (int i = 0; i < n - 1; i++) {
                if (match[i] == -1) {
                    if (dfs(i)) {
                        flag = true;
                    }
                }
            }
        } while (flag);

        int cnt = 0;
        for (int i = 0; i < n - 1; i++) {
            if (match[i] != -1) cnt++;
        }

        if (cnt == n - 1) {
            vec<i32> x_benches, y_benches;
            vec<i32> u, v;
            for (int i = 0; i < n - 1; i++) {
                x_benches.push_back(benches_pos[match[i] - n + 1].first);
                y_benches.push_back(benches_pos[match[i] - n + 1].second);
                u.push_back(used_edges[i].first);
                v.push_back(used_edges[i].second);
            }
            build(u, v, x_benches, y_benches);
            return 1;
        }


    }

    return 0;
}

Compilation message

parks.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization("unroll-loops")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 319 ms 41452 KB Output is correct
10 Correct 26 ms 4436 KB Output is correct
11 Correct 129 ms 22480 KB Output is correct
12 Correct 42 ms 6524 KB Output is correct
13 Correct 38 ms 5436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 309 ms 41520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 319 ms 41452 KB Output is correct
10 Correct 26 ms 4436 KB Output is correct
11 Correct 129 ms 22480 KB Output is correct
12 Correct 42 ms 6524 KB Output is correct
13 Correct 38 ms 5436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 309 ms 41520 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 928 ms 69124 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 3 ms 860 KB Output is correct
27 Correct 5 ms 860 KB Output is correct
28 Correct 278 ms 27816 KB Output is correct
29 Correct 450 ms 41828 KB Output is correct
30 Correct 612 ms 55400 KB Output is correct
31 Correct 810 ms 69168 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 308 ms 35248 KB Output is correct
46 Correct 526 ms 53224 KB Output is correct
47 Correct 509 ms 52296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 319 ms 41452 KB Output is correct
10 Correct 26 ms 4436 KB Output is correct
11 Correct 129 ms 22480 KB Output is correct
12 Correct 42 ms 6524 KB Output is correct
13 Correct 38 ms 5436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 309 ms 41520 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 928 ms 69124 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 3 ms 860 KB Output is correct
27 Correct 5 ms 860 KB Output is correct
28 Correct 278 ms 27816 KB Output is correct
29 Correct 450 ms 41828 KB Output is correct
30 Correct 612 ms 55400 KB Output is correct
31 Correct 810 ms 69168 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 308 ms 35248 KB Output is correct
46 Correct 526 ms 53224 KB Output is correct
47 Correct 509 ms 52296 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 772 ms 58652 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 319 ms 41452 KB Output is correct
10 Correct 26 ms 4436 KB Output is correct
11 Correct 129 ms 22480 KB Output is correct
12 Correct 42 ms 6524 KB Output is correct
13 Correct 38 ms 5436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 309 ms 41520 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 789 ms 73872 KB Output is correct
21 Correct 756 ms 68292 KB Output is correct
22 Correct 725 ms 66632 KB Output is correct
23 Correct 587 ms 71356 KB Output is correct
24 Correct 223 ms 16720 KB Output is correct
25 Correct 359 ms 23488 KB Output is correct
26 Correct 329 ms 23860 KB Output is correct
27 Correct 733 ms 83052 KB Output is correct
28 Correct 779 ms 82508 KB Output is correct
29 Correct 808 ms 82384 KB Output is correct
30 Correct 762 ms 82532 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 33 ms 5148 KB Output is correct
33 Correct 94 ms 8508 KB Output is correct
34 Correct 677 ms 72896 KB Output is correct
35 Correct 12 ms 1500 KB Output is correct
36 Correct 71 ms 6216 KB Output is correct
37 Correct 165 ms 11896 KB Output is correct
38 Correct 239 ms 27148 KB Output is correct
39 Correct 352 ms 36760 KB Output is correct
40 Correct 480 ms 49928 KB Output is correct
41 Correct 591 ms 57080 KB Output is correct
42 Correct 796 ms 67268 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 2 ms 604 KB Output is correct
55 Correct 3 ms 604 KB Output is correct
56 Correct 299 ms 35348 KB Output is correct
57 Correct 548 ms 52004 KB Output is correct
58 Correct 519 ms 53364 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 1 ms 600 KB Output is correct
62 Correct 690 ms 82692 KB Output is correct
63 Correct 672 ms 83412 KB Output is correct
64 Correct 670 ms 82232 KB Output is correct
65 Correct 4 ms 856 KB Output is correct
66 Correct 8 ms 1372 KB Output is correct
67 Correct 298 ms 34756 KB Output is correct
68 Correct 495 ms 54456 KB Output is correct
69 Correct 776 ms 69516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 319 ms 41452 KB Output is correct
10 Correct 26 ms 4436 KB Output is correct
11 Correct 129 ms 22480 KB Output is correct
12 Correct 42 ms 6524 KB Output is correct
13 Correct 38 ms 5436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 309 ms 41520 KB Output is correct
17 Correct 705 ms 83464 KB Output is correct
18 Correct 717 ms 83364 KB Output is correct
19 Correct 713 ms 82116 KB Output is correct
20 Correct 737 ms 67776 KB Output is correct
21 Correct 650 ms 67048 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 87 ms 10912 KB Output is correct
24 Correct 25 ms 2756 KB Output is correct
25 Correct 107 ms 9024 KB Output is correct
26 Correct 205 ms 16068 KB Output is correct
27 Correct 316 ms 34472 KB Output is correct
28 Correct 437 ms 42936 KB Output is correct
29 Correct 537 ms 52920 KB Output is correct
30 Correct 652 ms 60764 KB Output is correct
31 Correct 732 ms 68804 KB Output is correct
32 Correct 727 ms 72588 KB Output is correct
33 Correct 658 ms 83412 KB Output is correct
34 Correct 5 ms 860 KB Output is correct
35 Correct 10 ms 1496 KB Output is correct
36 Correct 307 ms 35152 KB Output is correct
37 Correct 589 ms 54492 KB Output is correct
38 Correct 745 ms 70340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 319 ms 41452 KB Output is correct
10 Correct 26 ms 4436 KB Output is correct
11 Correct 129 ms 22480 KB Output is correct
12 Correct 42 ms 6524 KB Output is correct
13 Correct 38 ms 5436 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 600 KB Output is correct
16 Correct 309 ms 41520 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 928 ms 69124 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 3 ms 860 KB Output is correct
27 Correct 5 ms 860 KB Output is correct
28 Correct 278 ms 27816 KB Output is correct
29 Correct 450 ms 41828 KB Output is correct
30 Correct 612 ms 55400 KB Output is correct
31 Correct 810 ms 69168 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 344 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 308 ms 35248 KB Output is correct
46 Correct 526 ms 53224 KB Output is correct
47 Correct 509 ms 52296 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 0 ms 348 KB Output is correct
55 Incorrect 772 ms 58652 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -