Submission #443442

# Submission time Handle Problem Language Result Execution time Memory
443442 2021-07-10T13:45:08 Z mjhmjh1104 Fountain Parks (IOI21_parks) C++17
30 / 100
720 ms 42392 KB
#include "parks.h"
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

int uf[200006];

int _find(int x) {
    if (uf[x] == -1) return x;
    return uf[x] = _find(uf[x]);
}

void _merge(int x, int y) {
    x = _find(x), y = _find(y);
    if (x != y) uf[x] = y;
}

map<pair<int, int>, int> m;
set<pair<int, int>> s;

int construct_roads(vector<int> x, vector<int> y) {
    if ((int)x.size() == 1) return build({}, {}, {}, {}), 1;
    int n = (int)x.size();
    for (int i = 0; i < n; i++) uf[i] = -1;
    vector<int> u, v, a, b;
    vector<pair<int, int>> edges, n_edges;
    vector<int> points;
    for (int i = 0; i < n; i++) m[{ x[i], y[i] }] = i;
    for (int i = 0; i < n; i++) if (m.count({ x[i], y[i] - 2 })) {
        edges.push_back({ m[{ x[i], y[i] - 2 }], i });
        _merge(edges.back().first, edges.back().second);
    }
    for (int i = 0; i < n; i++) points.push_back(i);
    sort(points.begin(), points.end(), [&](const int &a, const int &b) {
        return y[a] < y[b];
    });
    for (int t = 0; t < n; t++) {
        int i = points[t];
        if (m.count({ x[i] - 2, y[i] })) {
            int t = m[{ x[i] - 2, y[i] }];
            if (_find(t) != _find(i)) {
                edges.push_back({ t, i });
                _merge(t, i);
            }
        }
    }
    for (int i = 1; i < n; i++) if (_find(0) != _find(i)) return 0;
    int min_x = *min_element(x.begin(), x.end()), max_x = *max_element(x.begin(), x.end());
    for (auto &i: edges) {
        if (x[i.first] == min_x && x[i.second] == min_x) {
            u.push_back(i.first);
            v.push_back(i.second);
            a.push_back(min_x - 1);
            b.push_back((y[i.first] + y[i.second]) / 2);
            s.insert({ a.back(), b.back() });
        } else if (x[i.first] == max_x && x[i.second] == max_x) {
            u.push_back(i.first);
            v.push_back(i.second);
            a.push_back(max_x + 1);
            b.push_back((y[i.first] + y[i.second]) / 2);
            s.insert({ a.back(), b.back() });
        } else n_edges.push_back(i);
    }
    sort(n_edges.begin(), n_edges.end(), [&](const pair<int, int> &a, const pair<int, int> &b) {
        if (min(y[a.first], y[a.second]) != min(y[b.first], y[b.second])) return min(y[a.first], y[a.second]) < min(y[b.first], y[b.second]);
        return min(x[a.first], x[a.second]) < min(x[b.first], x[b.second]);
    });
    for (auto &i: n_edges) {
        if (x[i.first] == x[i.second]) {
            if (!s.count({ x[i.first] - 1, (y[i.first] + y[i.second]) / 2 })) {
                s.insert({ x[i.first] - 1, (y[i.first] + y[i.second]) / 2 });
                u.push_back(i.first);
                v.push_back(i.second);
                a.push_back(x[i.first] - 1);
                b.push_back((y[i.first] + y[i.second]) / 2);
            } else {
                if (s.count({ x[i.first] + 1, (y[i.first] + y[i.second]) / 2 })) return 0;
                s.insert({ x[i.first] + 1, (y[i.first] + y[i.second]) / 2 });
                u.push_back(i.first);
                v.push_back(i.second);
                a.push_back(x[i.first] + 1);
                b.push_back((y[i.first] + y[i.second]) / 2);
            }
        } else {
            if (!s.count({ (x[i.first] + x[i.second]) / 2, y[i.first] - 1 })) {
                s.insert({ (x[i.first] + x[i.second]) / 2, y[i.first] - 1 });
                u.push_back(i.first);
                v.push_back(i.second);
                a.push_back((x[i.first] + x[i.second]) / 2);
                b.push_back(y[i.first] - 1);
            } else {
                if (s.count({ (x[i.first] + x[i.second]) / 2, y[i.first] + 1 })) return 0;
                s.insert({ (x[i.first] + x[i.second]) / 2, y[i.first] + 1 });
                u.push_back(i.first);
                v.push_back(i.second);
                a.push_back((x[i.first] + x[i.second]) / 2);
                b.push_back(y[i.first] + 1);
            }
        }
    }
    build(u, v, a, b);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 224 ms 19548 KB Output is correct
10 Correct 12 ms 2508 KB Output is correct
11 Correct 69 ms 11372 KB Output is correct
12 Correct 18 ms 3544 KB Output is correct
13 Correct 35 ms 5060 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 424 KB Output is correct
16 Correct 215 ms 19676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 224 ms 19548 KB Output is correct
10 Correct 12 ms 2508 KB Output is correct
11 Correct 69 ms 11372 KB Output is correct
12 Correct 18 ms 3544 KB Output is correct
13 Correct 35 ms 5060 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 424 KB Output is correct
16 Correct 215 ms 19676 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 639 ms 39196 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 3 ms 588 KB Output is correct
27 Correct 4 ms 568 KB Output is correct
28 Correct 186 ms 15856 KB Output is correct
29 Correct 303 ms 23676 KB Output is correct
30 Correct 437 ms 31648 KB Output is correct
31 Correct 668 ms 39360 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 2 ms 460 KB Output is correct
44 Correct 3 ms 588 KB Output is correct
45 Correct 230 ms 20000 KB Output is correct
46 Correct 387 ms 29044 KB Output is correct
47 Correct 366 ms 28896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 224 ms 19548 KB Output is correct
10 Correct 12 ms 2508 KB Output is correct
11 Correct 69 ms 11372 KB Output is correct
12 Correct 18 ms 3544 KB Output is correct
13 Correct 35 ms 5060 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 424 KB Output is correct
16 Correct 215 ms 19676 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 639 ms 39196 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 3 ms 588 KB Output is correct
27 Correct 4 ms 568 KB Output is correct
28 Correct 186 ms 15856 KB Output is correct
29 Correct 303 ms 23676 KB Output is correct
30 Correct 437 ms 31648 KB Output is correct
31 Correct 668 ms 39360 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 2 ms 460 KB Output is correct
44 Correct 3 ms 588 KB Output is correct
45 Correct 230 ms 20000 KB Output is correct
46 Correct 387 ms 29044 KB Output is correct
47 Correct 366 ms 28896 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 0 ms 204 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 0 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 638 ms 39784 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 3 ms 588 KB Output is correct
58 Correct 9 ms 1484 KB Output is correct
59 Correct 10 ms 1308 KB Output is correct
60 Correct 239 ms 19832 KB Output is correct
61 Correct 349 ms 27972 KB Output is correct
62 Correct 466 ms 33064 KB Output is correct
63 Correct 658 ms 39728 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 605 ms 39356 KB Output is correct
68 Correct 583 ms 39180 KB Output is correct
69 Correct 592 ms 39012 KB Output is correct
70 Correct 5 ms 588 KB Output is correct
71 Correct 8 ms 1040 KB Output is correct
72 Correct 230 ms 19676 KB Output is correct
73 Correct 433 ms 30184 KB Output is correct
74 Correct 585 ms 39384 KB Output is correct
75 Correct 611 ms 39428 KB Output is correct
76 Correct 588 ms 39168 KB Output is correct
77 Correct 5 ms 716 KB Output is correct
78 Correct 9 ms 1228 KB Output is correct
79 Correct 225 ms 19888 KB Output is correct
80 Correct 392 ms 29848 KB Output is correct
81 Correct 568 ms 40064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 224 ms 19548 KB Output is correct
10 Correct 12 ms 2508 KB Output is correct
11 Correct 69 ms 11372 KB Output is correct
12 Correct 18 ms 3544 KB Output is correct
13 Correct 35 ms 5060 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 424 KB Output is correct
16 Correct 215 ms 19676 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 657 ms 39664 KB Output is correct
21 Correct 573 ms 42392 KB Output is correct
22 Correct 546 ms 41924 KB Output is correct
23 Correct 535 ms 36156 KB Output is correct
24 Correct 397 ms 19524 KB Output is correct
25 Correct 495 ms 21260 KB Output is correct
26 Correct 518 ms 21692 KB Output is correct
27 Correct 634 ms 41276 KB Output is correct
28 Correct 623 ms 41244 KB Output is correct
29 Correct 664 ms 41404 KB Output is correct
30 Correct 625 ms 41228 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Incorrect 20 ms 1924 KB Solution announced impossible, but it is possible.
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 224 ms 19548 KB Output is correct
10 Correct 12 ms 2508 KB Output is correct
11 Correct 69 ms 11372 KB Output is correct
12 Correct 18 ms 3544 KB Output is correct
13 Correct 35 ms 5060 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 424 KB Output is correct
16 Correct 215 ms 19676 KB Output is correct
17 Correct 633 ms 39652 KB Output is correct
18 Correct 567 ms 39260 KB Output is correct
19 Correct 582 ms 40020 KB Output is correct
20 Correct 720 ms 40424 KB Output is correct
21 Correct 564 ms 36428 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Incorrect 48 ms 4040 KB Solution announced impossible, but it is possible.
24 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 224 ms 19548 KB Output is correct
10 Correct 12 ms 2508 KB Output is correct
11 Correct 69 ms 11372 KB Output is correct
12 Correct 18 ms 3544 KB Output is correct
13 Correct 35 ms 5060 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 424 KB Output is correct
16 Correct 215 ms 19676 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 639 ms 39196 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 2 ms 460 KB Output is correct
26 Correct 3 ms 588 KB Output is correct
27 Correct 4 ms 568 KB Output is correct
28 Correct 186 ms 15856 KB Output is correct
29 Correct 303 ms 23676 KB Output is correct
30 Correct 437 ms 31648 KB Output is correct
31 Correct 668 ms 39360 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 0 ms 204 KB Output is correct
41 Correct 0 ms 204 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 2 ms 460 KB Output is correct
44 Correct 3 ms 588 KB Output is correct
45 Correct 230 ms 20000 KB Output is correct
46 Correct 387 ms 29044 KB Output is correct
47 Correct 366 ms 28896 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 0 ms 204 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 0 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 638 ms 39784 KB Output is correct
56 Correct 0 ms 204 KB Output is correct
57 Correct 3 ms 588 KB Output is correct
58 Correct 9 ms 1484 KB Output is correct
59 Correct 10 ms 1308 KB Output is correct
60 Correct 239 ms 19832 KB Output is correct
61 Correct 349 ms 27972 KB Output is correct
62 Correct 466 ms 33064 KB Output is correct
63 Correct 658 ms 39728 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 0 ms 204 KB Output is correct
67 Correct 605 ms 39356 KB Output is correct
68 Correct 583 ms 39180 KB Output is correct
69 Correct 592 ms 39012 KB Output is correct
70 Correct 5 ms 588 KB Output is correct
71 Correct 8 ms 1040 KB Output is correct
72 Correct 230 ms 19676 KB Output is correct
73 Correct 433 ms 30184 KB Output is correct
74 Correct 585 ms 39384 KB Output is correct
75 Correct 611 ms 39428 KB Output is correct
76 Correct 588 ms 39168 KB Output is correct
77 Correct 5 ms 716 KB Output is correct
78 Correct 9 ms 1228 KB Output is correct
79 Correct 225 ms 19888 KB Output is correct
80 Correct 392 ms 29848 KB Output is correct
81 Correct 568 ms 40064 KB Output is correct
82 Correct 0 ms 204 KB Output is correct
83 Correct 0 ms 204 KB Output is correct
84 Correct 0 ms 204 KB Output is correct
85 Correct 657 ms 39664 KB Output is correct
86 Correct 573 ms 42392 KB Output is correct
87 Correct 546 ms 41924 KB Output is correct
88 Correct 535 ms 36156 KB Output is correct
89 Correct 397 ms 19524 KB Output is correct
90 Correct 495 ms 21260 KB Output is correct
91 Correct 518 ms 21692 KB Output is correct
92 Correct 634 ms 41276 KB Output is correct
93 Correct 623 ms 41244 KB Output is correct
94 Correct 664 ms 41404 KB Output is correct
95 Correct 625 ms 41228 KB Output is correct
96 Correct 0 ms 204 KB Output is correct
97 Incorrect 20 ms 1924 KB Solution announced impossible, but it is possible.
98 Halted 0 ms 0 KB -