Submission #685024

# Submission time Handle Problem Language Result Execution time Memory
685024 2023-01-23T05:49:14 Z sharaelong Fountain Parks (IOI21_parks) C++17
15 / 100
429 ms 71056 KB
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

struct DisjointSet {
    int n;
    vector<int> parent, rank;
    DisjointSet(int _n) : n(_n) {
        parent.resize(n);
        iota(parent.begin(), parent.end(), 0);
        rank.resize(n, 0);
    }

    int find(int u) {
        return parent[u] = (u == parent[u] ? u : find(parent[u]));
    }

    void merge(int u, int v) {
        u = find(u); v = find(v);
        if (u == v) return;
        if (rank[u] > rank[v]) swap(u, v);
        parent[u] = v;
        if (rank[u] == rank[v]) ++rank[v];
    }
};

int construct_roads(vector<int> x, vector<int> y) {
    int n = x.size();
    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    sort(p.begin(), p.end(), [&](int a, int b) {
        return pii{x[a],y[a]} < pii{x[b], y[b]};
    });
    
    DisjointSet dsu(n);
    map<pii, int> fountains;
    vector<vector<pii>> points(200001);
    int min_x = 200000;
    set<pii> benches;
    vector<int> u, v, a, b;
    for (int i=0; i<n; ++i) fountains[{ x[i], y[i] }] = i, min_x = min(min_x, x[i]);
    for (int i=0; i<n; ++i) points[x[i]].push_back({ y[i], i });
    for (int i=0; i<points.size(); i+=2) sort(points[i].begin(), points[i].end());
    for (int j=0; j+1<points[min_x].size(); ++j) {
        if (points[min_x][j].first + 2 == points[min_x][j+1].first) {
            dsu.merge(points[min_x][j].second, points[min_x][j+1].second);
            u.push_back(points[min_x][j].second);
            v.push_back(points[min_x][j+1].second);
            a.push_back(min_x-1);
            b.push_back(points[min_x][j].first + 1);
            benches.insert({ points[min_x][j].first + 1, min_x-1 });
        }
    }

    for (int x=min_x+2; x<points.size(); x+=2) {
        for (int j=0; j<points[x].size(); ++j) {
            auto[y1, i1] = points[x][j];
            if (fountains.find({ x-2, y1 }) != fountains.end()) {
                int idx = fountains[{x-2, y1}];
                if (dsu.find(idx) != dsu.find(i1)) {
                    dsu.merge(idx, i1);
                    u.push_back(idx);
                    v.push_back(i1);
                    a.push_back(x-1);
                    if (benches.find({x-1, y1-1}) != benches.end()) {
                        assert(benches.find({x-1, y1+1}) == benches.end());
                        b.push_back(y1+1);
                    } else {
                        b.push_back(y1-1);
                    }
                    benches.insert({ a.back(), b.back() });
                }
            }
            if (j+1 == points[x].size()) break;
            
            auto[y2, i2] = points[x][j+1];
            if (y1 + 2 == y2) {
                dsu.merge(i1, i2);
                u.push_back(i1);
                v.push_back(i2);
                b.push_back(y1+1);
                if (benches.find({x-1, y1+1}) != benches.end()) {
                    assert(benches.find({x+1, y1+1}) == benches.end());
                    a.push_back(x+1);
                } else {
                    a.push_back(x-1);
                }
                benches.insert({ a.back(), b.back() });
            }
        }
    }

    for (int i=1; i<n; ++i) {
        if (dsu.find(i) != dsu.find(0)) {
            return 0;
        }
    }
    build(u, v, a, b);
    return 1;
}

Compilation message

parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for (int i=0; i<points.size(); i+=2) sort(points[i].begin(), points[i].end());
      |                   ~^~~~~~~~~~~~~~
parks.cpp:46:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int j=0; j+1<points[min_x].size(); ++j) {
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~
parks.cpp:57:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int x=min_x+2; x<points.size(); x+=2) {
      |                         ~^~~~~~~~~~~~~~
parks.cpp:58:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |         for (int j=0; j<points[x].size(); ++j) {
      |                       ~^~~~~~~~~~~~~~~~~
parks.cpp:76:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |             if (j+1 == points[x].size()) break;
      |                 ~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 136 ms 25124 KB Output is correct
10 Correct 12 ms 6936 KB Output is correct
11 Correct 54 ms 15760 KB Output is correct
12 Correct 17 ms 8004 KB Output is correct
13 Correct 32 ms 12532 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5308 KB Output is correct
16 Correct 125 ms 25120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 136 ms 25124 KB Output is correct
10 Correct 12 ms 6936 KB Output is correct
11 Correct 54 ms 15760 KB Output is correct
12 Correct 17 ms 8004 KB Output is correct
13 Correct 32 ms 12532 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5308 KB Output is correct
16 Correct 125 ms 25120 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 328 ms 45216 KB Output is correct
24 Correct 4 ms 4948 KB Output is correct
25 Correct 4 ms 5176 KB Output is correct
26 Correct 8 ms 5436 KB Output is correct
27 Correct 6 ms 5588 KB Output is correct
28 Correct 127 ms 21172 KB Output is correct
29 Correct 176 ms 29108 KB Output is correct
30 Correct 248 ms 37380 KB Output is correct
31 Correct 327 ms 45204 KB Output is correct
32 Correct 3 ms 4952 KB Output is correct
33 Correct 3 ms 4960 KB Output is correct
34 Correct 3 ms 4960 KB Output is correct
35 Correct 3 ms 4960 KB Output is correct
36 Correct 3 ms 4960 KB Output is correct
37 Correct 4 ms 4924 KB Output is correct
38 Correct 3 ms 4960 KB Output is correct
39 Correct 4 ms 4948 KB Output is correct
40 Correct 3 ms 4948 KB Output is correct
41 Correct 4 ms 4948 KB Output is correct
42 Correct 4 ms 4948 KB Output is correct
43 Correct 5 ms 5332 KB Output is correct
44 Correct 6 ms 5460 KB Output is correct
45 Correct 144 ms 25116 KB Output is correct
46 Correct 246 ms 34764 KB Output is correct
47 Correct 224 ms 34840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 136 ms 25124 KB Output is correct
10 Correct 12 ms 6936 KB Output is correct
11 Correct 54 ms 15760 KB Output is correct
12 Correct 17 ms 8004 KB Output is correct
13 Correct 32 ms 12532 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5308 KB Output is correct
16 Correct 125 ms 25120 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 328 ms 45216 KB Output is correct
24 Correct 4 ms 4948 KB Output is correct
25 Correct 4 ms 5176 KB Output is correct
26 Correct 8 ms 5436 KB Output is correct
27 Correct 6 ms 5588 KB Output is correct
28 Correct 127 ms 21172 KB Output is correct
29 Correct 176 ms 29108 KB Output is correct
30 Correct 248 ms 37380 KB Output is correct
31 Correct 327 ms 45204 KB Output is correct
32 Correct 3 ms 4952 KB Output is correct
33 Correct 3 ms 4960 KB Output is correct
34 Correct 3 ms 4960 KB Output is correct
35 Correct 3 ms 4960 KB Output is correct
36 Correct 3 ms 4960 KB Output is correct
37 Correct 4 ms 4924 KB Output is correct
38 Correct 3 ms 4960 KB Output is correct
39 Correct 4 ms 4948 KB Output is correct
40 Correct 3 ms 4948 KB Output is correct
41 Correct 4 ms 4948 KB Output is correct
42 Correct 4 ms 4948 KB Output is correct
43 Correct 5 ms 5332 KB Output is correct
44 Correct 6 ms 5460 KB Output is correct
45 Correct 144 ms 25116 KB Output is correct
46 Correct 246 ms 34764 KB Output is correct
47 Correct 224 ms 34840 KB Output is correct
48 Correct 3 ms 4948 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 4884 KB Output is correct
55 Runtime error 226 ms 71056 KB Execution killed with signal 6
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 136 ms 25124 KB Output is correct
10 Correct 12 ms 6936 KB Output is correct
11 Correct 54 ms 15760 KB Output is correct
12 Correct 17 ms 8004 KB Output is correct
13 Correct 32 ms 12532 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5308 KB Output is correct
16 Correct 125 ms 25120 KB Output is correct
17 Correct 4 ms 4908 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 429 ms 47420 KB Output is correct
21 Correct 387 ms 46732 KB Output is correct
22 Correct 373 ms 46744 KB Output is correct
23 Correct 287 ms 40736 KB Output is correct
24 Correct 201 ms 26444 KB Output is correct
25 Correct 305 ms 38692 KB Output is correct
26 Correct 294 ms 38820 KB Output is correct
27 Runtime error 170 ms 51916 KB Execution killed with signal 6
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 136 ms 25124 KB Output is correct
10 Correct 12 ms 6936 KB Output is correct
11 Correct 54 ms 15760 KB Output is correct
12 Correct 17 ms 8004 KB Output is correct
13 Correct 32 ms 12532 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5308 KB Output is correct
16 Correct 125 ms 25120 KB Output is correct
17 Correct 349 ms 46112 KB Output is correct
18 Correct 355 ms 46260 KB Output is correct
19 Runtime error 290 ms 66804 KB Execution killed with signal 6
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 136 ms 25124 KB Output is correct
10 Correct 12 ms 6936 KB Output is correct
11 Correct 54 ms 15760 KB Output is correct
12 Correct 17 ms 8004 KB Output is correct
13 Correct 32 ms 12532 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5308 KB Output is correct
16 Correct 125 ms 25120 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 4 ms 5076 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 328 ms 45216 KB Output is correct
24 Correct 4 ms 4948 KB Output is correct
25 Correct 4 ms 5176 KB Output is correct
26 Correct 8 ms 5436 KB Output is correct
27 Correct 6 ms 5588 KB Output is correct
28 Correct 127 ms 21172 KB Output is correct
29 Correct 176 ms 29108 KB Output is correct
30 Correct 248 ms 37380 KB Output is correct
31 Correct 327 ms 45204 KB Output is correct
32 Correct 3 ms 4952 KB Output is correct
33 Correct 3 ms 4960 KB Output is correct
34 Correct 3 ms 4960 KB Output is correct
35 Correct 3 ms 4960 KB Output is correct
36 Correct 3 ms 4960 KB Output is correct
37 Correct 4 ms 4924 KB Output is correct
38 Correct 3 ms 4960 KB Output is correct
39 Correct 4 ms 4948 KB Output is correct
40 Correct 3 ms 4948 KB Output is correct
41 Correct 4 ms 4948 KB Output is correct
42 Correct 4 ms 4948 KB Output is correct
43 Correct 5 ms 5332 KB Output is correct
44 Correct 6 ms 5460 KB Output is correct
45 Correct 144 ms 25116 KB Output is correct
46 Correct 246 ms 34764 KB Output is correct
47 Correct 224 ms 34840 KB Output is correct
48 Correct 3 ms 4948 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 4884 KB Output is correct
55 Runtime error 226 ms 71056 KB Execution killed with signal 6
56 Halted 0 ms 0 KB -