Submission #1061756

# Submission time Handle Problem Language Result Execution time Memory
1061756 2024-08-16T12:42:30 Z mc061 Fountain Parks (IOI21_parks) C++17
50 / 100
1838 ms 74268 KB
#include <bits/stdc++.h>
using namespace std;
 
map<pair<int, int>, int> vid;
int di[2] = {2, 0};
int dj[2] = {0, 2};
 
struct edge {
    int i1, j1, i2, j2;
};
 
vector<edge> edges;
const int N = 2e5+6;
struct DSU {
    int p[N]={};
    int sz[N];
    DSU() {
        iota(p, p+N, 0);
        fill(sz, sz+N, 1);
    }
    int get(int a) {
        return p[a] = (a == p[a] ? a : get(p[a]));
    }
    void merge(int a, int b) {
        int x = get(a), y = get(b);
        if (x == y) return;
        if (sz[x] > sz[y]) swap(x, y);
        p[x] = y;
        sz[y] += sz[x];
    }
};
 
vector<edge> horizontal, vertical;
 
void build(vector<int> u, vector<int> v, vector<int> a, vector<int> b);
bool try_build() {
    //0 - up(hor)   (from road: -1;+1)
    //1 - down(hor) (from road: +1;+1)
    //2 - left(ver) (from road: +1;-1)
    //3 - right(ver)(from road: +1;+1)
 
    vector<int> tlx = {-1, 1, 1, 1};
    vector<int> tly = {1, 1, -1, 1};
 
    vector<int> dx = {0, 0, 2, 2};
    vector<int> dy = {2, 2, 0, 0};
 
    map<pair<int, int>, int> bench_dir;
    for (const auto& e : horizontal) {
        bench_dir[{e.i1-1, e.j1+1}] = 0;
    }
    
    auto sieve_down = [&] (int bi, int bj) -> void {
        bench_dir.erase({bi, bj});
        bi += 2;
        while (bench_dir.count({bi, bj})) {
            bench_dir[{bi, bj}] = 1;
            bi += 2;
        }
        bench_dir[{bi, bj}] = 1;
    };
 
    for (const auto& e : vertical) {
        int wi_left = e.i1+1;
        int wj_left = e.j1-1;
 
        int wi_right = e.i1+1;
        int wj_right = e.j1+1;
 
        if (bench_dir.count({wi_left, wj_left})) {
            if (!bench_dir.count({wi_right, wj_right})) {
                bench_dir[{wi_right, wj_right}] = 3;
                continue;
            }
            int prev_dir = bench_dir[{wi_left, wj_left}];
            
            if (prev_dir == 0) {
                sieve_down(wi_left, wj_left);
                bench_dir[{wi_left, wj_left}] = 2;
                continue;
            }
            if (prev_dir == 1 || prev_dir == 3) {
                int prev_dir2 = bench_dir[{wi_right, wj_right}];
                if (prev_dir2 == 1) {
                    return false;
                }
            }
            sieve_down(wi_right, wj_right);
            bench_dir[{wi_right, wj_right}] = 3;
        }
        else {
            bench_dir[{wi_left, wj_left}] = 2;
        }
    }
    map<array<int, 4>, pair<int, int>> road_to_bench;
    for (const auto& [x, y] : bench_dir) {         
        int tx = x.first-tlx[y];
        int ty = x.second-tly[y];
        int bx = tx+dx[y];
        int by = ty+dy[y];
        road_to_bench[{tx, ty, bx, by}] = {x.first, x.second};
    }
 
    vector<int> u, v, a, b;
 
    for (const auto& e : horizontal) {
        u.push_back(vid[{e.i1, e.j1}]);
        v.push_back(vid[{e.i2, e.j2}]);
        a.push_back(road_to_bench[{e.i1, e.j1, e.i2, e.j2}].first);
        b.push_back(road_to_bench[{e.i1, e.j1, e.i2, e.j2}].second);
    }
    for (const auto& e : vertical) {
        u.push_back(vid[{e.i1, e.j1}]);
        v.push_back(vid[{e.i2, e.j2}]);
        a.push_back(road_to_bench[{e.i1, e.j1, e.i2, e.j2}].first);
        b.push_back(road_to_bench[{e.i1, e.j1, e.i2, e.j2}].second);
    }
    build(u, v, a, b);
    return true;
}
 
int construct_roads(vector<int> x, vector<int> y) {
    const int n = x.size();
    for (int i = 0; i < n; ++i) {
        vid[{x[i], y[i]}] = i;
    }
    vector<edge> all_vert;
    vector<edge> all_hor;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 2; ++j) {
            int ni = x[i] + di[j];
            int nj = y[i] + dj[j];
            auto it = vid.find({ni, nj});
            if (it == vid.end()) continue;
            edges.push_back({x[i], y[i], ni, nj});
            bool hor = ni == x[i];
            if (!hor) all_vert.push_back({x[i], y[i], ni, nj});
            else all_hor.push_back({x[i], y[i], ni, nj});
        }
    }
 
    for (int z = 0; z < 12; ++z) {
        random_shuffle(all_hor.begin(), all_hor.end());
        random_shuffle(all_vert.begin(), all_vert.end());
        vertical.clear();
        horizontal.clear();
        DSU d;
        if (z % 2 == 0) {
            for (const edge& e : all_vert) {
                int id1 = vid[{e.i1, e.j1}];
                int id2 = vid[{e.i2, e.j2}];
                if (d.get(id1) == d.get(id2)) continue;
                d.merge(id1, id2);
                vertical.push_back(e);
            }
            for (const edge& e : all_hor) {
                bool hor = e.i1 == e.i2;
                int id1 = vid[{e.i1, e.j1}];
                int id2 = vid[{e.i2, e.j2}];
                if (d.get(id1) == d.get(id2)) continue;
                d.merge(id1, id2);
                horizontal.push_back(e);
            }
        }
        else {
            for (const edge& e : all_hor) {
                bool hor = e.i1 == e.i2;
                int id1 = vid[{e.i1, e.j1}];
                int id2 = vid[{e.i2, e.j2}];
                if (d.get(id1) == d.get(id2)) continue;
                d.merge(id1, id2);
                horizontal.push_back(e);
            }
            for (const edge& e : all_vert) {
                int id1 = vid[{e.i1, e.j1}];
                int id2 = vid[{e.i2, e.j2}];
                if (d.get(id1) == d.get(id2)) continue;
                d.merge(id1, id2);
                vertical.push_back(e);
            }
        }
 
        if (d.sz[d.get(0)] != n) return 0;
 
        sort(vertical.begin(), vertical.end(), [&](const edge& a, const edge& b) {
            if (a.i1 != b.i1) return a.i1 < b.i1;
            return a.j1 < b.j1; 
        });
        int j = try_build();
        if (j == 1) return true;
    }
    return false;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:157:22: warning: unused variable 'hor' [-Wunused-variable]
  157 |                 bool hor = e.i1 == e.i2;
      |                      ^~~
parks.cpp:167:22: warning: unused variable 'hor' [-Wunused-variable]
  167 |                 bool hor = e.i1 == e.i2;
      |                      ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 264 ms 32056 KB Output is correct
10 Correct 18 ms 4912 KB Output is correct
11 Correct 128 ms 17988 KB Output is correct
12 Correct 28 ms 6408 KB Output is correct
13 Correct 36 ms 8392 KB Output is correct
14 Correct 2 ms 2136 KB Output is correct
15 Correct 2 ms 2140 KB Output is correct
16 Correct 266 ms 32036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 264 ms 32056 KB Output is correct
10 Correct 18 ms 4912 KB Output is correct
11 Correct 128 ms 17988 KB Output is correct
12 Correct 28 ms 6408 KB Output is correct
13 Correct 36 ms 8392 KB Output is correct
14 Correct 2 ms 2136 KB Output is correct
15 Correct 2 ms 2140 KB Output is correct
16 Correct 266 ms 32036 KB Output is correct
17 Correct 1 ms 1884 KB Output is correct
18 Correct 1 ms 1948 KB Output is correct
19 Correct 1 ms 1884 KB Output is correct
20 Correct 1 ms 1880 KB Output is correct
21 Correct 1 ms 1848 KB Output is correct
22 Correct 1 ms 1880 KB Output is correct
23 Correct 712 ms 66872 KB Output is correct
24 Correct 1 ms 1880 KB Output is correct
25 Correct 3 ms 2396 KB Output is correct
26 Correct 4 ms 2396 KB Output is correct
27 Correct 4 ms 2652 KB Output is correct
28 Correct 216 ms 27484 KB Output is correct
29 Correct 361 ms 39472 KB Output is correct
30 Correct 562 ms 54496 KB Output is correct
31 Correct 693 ms 65564 KB Output is correct
32 Correct 1 ms 1884 KB Output is correct
33 Correct 1 ms 1852 KB Output is correct
34 Correct 1 ms 1884 KB Output is correct
35 Correct 1 ms 1884 KB Output is correct
36 Correct 1 ms 1884 KB Output is correct
37 Correct 1 ms 1880 KB Output is correct
38 Correct 1 ms 1884 KB Output is correct
39 Correct 1 ms 1884 KB Output is correct
40 Correct 1 ms 1884 KB Output is correct
41 Correct 1 ms 1884 KB Output is correct
42 Correct 1 ms 1884 KB Output is correct
43 Correct 3 ms 2136 KB Output is correct
44 Correct 3 ms 2396 KB Output is correct
45 Correct 261 ms 32224 KB Output is correct
46 Correct 414 ms 48428 KB Output is correct
47 Correct 420 ms 47456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 264 ms 32056 KB Output is correct
10 Correct 18 ms 4912 KB Output is correct
11 Correct 128 ms 17988 KB Output is correct
12 Correct 28 ms 6408 KB Output is correct
13 Correct 36 ms 8392 KB Output is correct
14 Correct 2 ms 2136 KB Output is correct
15 Correct 2 ms 2140 KB Output is correct
16 Correct 266 ms 32036 KB Output is correct
17 Correct 1 ms 1884 KB Output is correct
18 Correct 1 ms 1948 KB Output is correct
19 Correct 1 ms 1884 KB Output is correct
20 Correct 1 ms 1880 KB Output is correct
21 Correct 1 ms 1848 KB Output is correct
22 Correct 1 ms 1880 KB Output is correct
23 Correct 712 ms 66872 KB Output is correct
24 Correct 1 ms 1880 KB Output is correct
25 Correct 3 ms 2396 KB Output is correct
26 Correct 4 ms 2396 KB Output is correct
27 Correct 4 ms 2652 KB Output is correct
28 Correct 216 ms 27484 KB Output is correct
29 Correct 361 ms 39472 KB Output is correct
30 Correct 562 ms 54496 KB Output is correct
31 Correct 693 ms 65564 KB Output is correct
32 Correct 1 ms 1884 KB Output is correct
33 Correct 1 ms 1852 KB Output is correct
34 Correct 1 ms 1884 KB Output is correct
35 Correct 1 ms 1884 KB Output is correct
36 Correct 1 ms 1884 KB Output is correct
37 Correct 1 ms 1880 KB Output is correct
38 Correct 1 ms 1884 KB Output is correct
39 Correct 1 ms 1884 KB Output is correct
40 Correct 1 ms 1884 KB Output is correct
41 Correct 1 ms 1884 KB Output is correct
42 Correct 1 ms 1884 KB Output is correct
43 Correct 3 ms 2136 KB Output is correct
44 Correct 3 ms 2396 KB Output is correct
45 Correct 261 ms 32224 KB Output is correct
46 Correct 414 ms 48428 KB Output is correct
47 Correct 420 ms 47456 KB Output is correct
48 Correct 1 ms 1880 KB Output is correct
49 Correct 1 ms 1884 KB Output is correct
50 Correct 2 ms 1884 KB Output is correct
51 Correct 1 ms 1880 KB Output is correct
52 Correct 1 ms 1940 KB Output is correct
53 Correct 1 ms 1884 KB Output is correct
54 Correct 2 ms 1880 KB Output is correct
55 Correct 976 ms 74268 KB Output is correct
56 Correct 1 ms 1880 KB Output is correct
57 Correct 4 ms 2652 KB Output is correct
58 Correct 15 ms 3916 KB Output is correct
59 Correct 11 ms 3676 KB Output is correct
60 Correct 407 ms 36248 KB Output is correct
61 Correct 624 ms 50368 KB Output is correct
62 Correct 807 ms 59264 KB Output is correct
63 Correct 1000 ms 73836 KB Output is correct
64 Correct 1 ms 1884 KB Output is correct
65 Correct 1 ms 1884 KB Output is correct
66 Correct 1 ms 1884 KB Output is correct
67 Correct 639 ms 62616 KB Output is correct
68 Correct 639 ms 62172 KB Output is correct
69 Correct 636 ms 64244 KB Output is correct
70 Correct 4 ms 2648 KB Output is correct
71 Correct 7 ms 3160 KB Output is correct
72 Correct 305 ms 32256 KB Output is correct
73 Correct 454 ms 48268 KB Output is correct
74 Correct 631 ms 62904 KB Output is correct
75 Correct 671 ms 64236 KB Output is correct
76 Correct 667 ms 62624 KB Output is correct
77 Correct 5 ms 2648 KB Output is correct
78 Correct 9 ms 3288 KB Output is correct
79 Correct 258 ms 32432 KB Output is correct
80 Correct 476 ms 50056 KB Output is correct
81 Correct 631 ms 63772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 264 ms 32056 KB Output is correct
10 Correct 18 ms 4912 KB Output is correct
11 Correct 128 ms 17988 KB Output is correct
12 Correct 28 ms 6408 KB Output is correct
13 Correct 36 ms 8392 KB Output is correct
14 Correct 2 ms 2136 KB Output is correct
15 Correct 2 ms 2140 KB Output is correct
16 Correct 266 ms 32036 KB Output is correct
17 Correct 2 ms 1884 KB Output is correct
18 Correct 1 ms 1884 KB Output is correct
19 Correct 1 ms 1880 KB Output is correct
20 Correct 577 ms 67032 KB Output is correct
21 Correct 609 ms 64716 KB Output is correct
22 Correct 598 ms 65924 KB Output is correct
23 Correct 435 ms 55584 KB Output is correct
24 Correct 165 ms 17572 KB Output is correct
25 Correct 341 ms 30404 KB Output is correct
26 Correct 251 ms 31392 KB Output is correct
27 Correct 613 ms 63028 KB Output is correct
28 Correct 639 ms 62724 KB Output is correct
29 Correct 568 ms 63508 KB Output is correct
30 Correct 579 ms 62772 KB Output is correct
31 Correct 1 ms 1884 KB Output is correct
32 Correct 29 ms 6244 KB Output is correct
33 Correct 76 ms 9668 KB Output is correct
34 Correct 582 ms 64604 KB Output is correct
35 Correct 10 ms 3236 KB Output is correct
36 Correct 60 ms 8624 KB Output is correct
37 Correct 132 ms 15492 KB Output is correct
38 Incorrect 676 ms 15288 KB Solution announced impossible, but it is possible.
39 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 264 ms 32056 KB Output is correct
10 Correct 18 ms 4912 KB Output is correct
11 Correct 128 ms 17988 KB Output is correct
12 Correct 28 ms 6408 KB Output is correct
13 Correct 36 ms 8392 KB Output is correct
14 Correct 2 ms 2136 KB Output is correct
15 Correct 2 ms 2140 KB Output is correct
16 Correct 266 ms 32036 KB Output is correct
17 Correct 573 ms 63704 KB Output is correct
18 Correct 576 ms 65736 KB Output is correct
19 Correct 622 ms 63852 KB Output is correct
20 Correct 636 ms 63664 KB Output is correct
21 Correct 528 ms 56404 KB Output is correct
22 Correct 2 ms 1884 KB Output is correct
23 Correct 73 ms 11832 KB Output is correct
24 Correct 22 ms 4688 KB Output is correct
25 Correct 95 ms 11576 KB Output is correct
26 Correct 186 ms 19728 KB Output is correct
27 Correct 716 ms 33724 KB Output is correct
28 Correct 962 ms 42856 KB Output is correct
29 Correct 1703 ms 51868 KB Output is correct
30 Correct 1838 ms 57836 KB Output is correct
31 Correct 1008 ms 65972 KB Output is correct
32 Correct 640 ms 63464 KB Output is correct
33 Correct 627 ms 62124 KB Output is correct
34 Correct 5 ms 2652 KB Output is correct
35 Correct 8 ms 3220 KB Output is correct
36 Correct 264 ms 32420 KB Output is correct
37 Correct 452 ms 48640 KB Output is correct
38 Correct 620 ms 63208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 264 ms 32056 KB Output is correct
10 Correct 18 ms 4912 KB Output is correct
11 Correct 128 ms 17988 KB Output is correct
12 Correct 28 ms 6408 KB Output is correct
13 Correct 36 ms 8392 KB Output is correct
14 Correct 2 ms 2136 KB Output is correct
15 Correct 2 ms 2140 KB Output is correct
16 Correct 266 ms 32036 KB Output is correct
17 Correct 1 ms 1884 KB Output is correct
18 Correct 1 ms 1948 KB Output is correct
19 Correct 1 ms 1884 KB Output is correct
20 Correct 1 ms 1880 KB Output is correct
21 Correct 1 ms 1848 KB Output is correct
22 Correct 1 ms 1880 KB Output is correct
23 Correct 712 ms 66872 KB Output is correct
24 Correct 1 ms 1880 KB Output is correct
25 Correct 3 ms 2396 KB Output is correct
26 Correct 4 ms 2396 KB Output is correct
27 Correct 4 ms 2652 KB Output is correct
28 Correct 216 ms 27484 KB Output is correct
29 Correct 361 ms 39472 KB Output is correct
30 Correct 562 ms 54496 KB Output is correct
31 Correct 693 ms 65564 KB Output is correct
32 Correct 1 ms 1884 KB Output is correct
33 Correct 1 ms 1852 KB Output is correct
34 Correct 1 ms 1884 KB Output is correct
35 Correct 1 ms 1884 KB Output is correct
36 Correct 1 ms 1884 KB Output is correct
37 Correct 1 ms 1880 KB Output is correct
38 Correct 1 ms 1884 KB Output is correct
39 Correct 1 ms 1884 KB Output is correct
40 Correct 1 ms 1884 KB Output is correct
41 Correct 1 ms 1884 KB Output is correct
42 Correct 1 ms 1884 KB Output is correct
43 Correct 3 ms 2136 KB Output is correct
44 Correct 3 ms 2396 KB Output is correct
45 Correct 261 ms 32224 KB Output is correct
46 Correct 414 ms 48428 KB Output is correct
47 Correct 420 ms 47456 KB Output is correct
48 Correct 1 ms 1880 KB Output is correct
49 Correct 1 ms 1884 KB Output is correct
50 Correct 2 ms 1884 KB Output is correct
51 Correct 1 ms 1880 KB Output is correct
52 Correct 1 ms 1940 KB Output is correct
53 Correct 1 ms 1884 KB Output is correct
54 Correct 2 ms 1880 KB Output is correct
55 Correct 976 ms 74268 KB Output is correct
56 Correct 1 ms 1880 KB Output is correct
57 Correct 4 ms 2652 KB Output is correct
58 Correct 15 ms 3916 KB Output is correct
59 Correct 11 ms 3676 KB Output is correct
60 Correct 407 ms 36248 KB Output is correct
61 Correct 624 ms 50368 KB Output is correct
62 Correct 807 ms 59264 KB Output is correct
63 Correct 1000 ms 73836 KB Output is correct
64 Correct 1 ms 1884 KB Output is correct
65 Correct 1 ms 1884 KB Output is correct
66 Correct 1 ms 1884 KB Output is correct
67 Correct 639 ms 62616 KB Output is correct
68 Correct 639 ms 62172 KB Output is correct
69 Correct 636 ms 64244 KB Output is correct
70 Correct 4 ms 2648 KB Output is correct
71 Correct 7 ms 3160 KB Output is correct
72 Correct 305 ms 32256 KB Output is correct
73 Correct 454 ms 48268 KB Output is correct
74 Correct 631 ms 62904 KB Output is correct
75 Correct 671 ms 64236 KB Output is correct
76 Correct 667 ms 62624 KB Output is correct
77 Correct 5 ms 2648 KB Output is correct
78 Correct 9 ms 3288 KB Output is correct
79 Correct 258 ms 32432 KB Output is correct
80 Correct 476 ms 50056 KB Output is correct
81 Correct 631 ms 63772 KB Output is correct
82 Correct 2 ms 1884 KB Output is correct
83 Correct 1 ms 1884 KB Output is correct
84 Correct 1 ms 1880 KB Output is correct
85 Correct 577 ms 67032 KB Output is correct
86 Correct 609 ms 64716 KB Output is correct
87 Correct 598 ms 65924 KB Output is correct
88 Correct 435 ms 55584 KB Output is correct
89 Correct 165 ms 17572 KB Output is correct
90 Correct 341 ms 30404 KB Output is correct
91 Correct 251 ms 31392 KB Output is correct
92 Correct 613 ms 63028 KB Output is correct
93 Correct 639 ms 62724 KB Output is correct
94 Correct 568 ms 63508 KB Output is correct
95 Correct 579 ms 62772 KB Output is correct
96 Correct 1 ms 1884 KB Output is correct
97 Correct 29 ms 6244 KB Output is correct
98 Correct 76 ms 9668 KB Output is correct
99 Correct 582 ms 64604 KB Output is correct
100 Correct 10 ms 3236 KB Output is correct
101 Correct 60 ms 8624 KB Output is correct
102 Correct 132 ms 15492 KB Output is correct
103 Incorrect 676 ms 15288 KB Solution announced impossible, but it is possible.
104 Halted 0 ms 0 KB -