Submission #435827

# Submission time Handle Problem Language Result Execution time Memory
435827 2021-06-23T18:55:06 Z talant117408 Fountain Parks (IOI21_parks) C++17
55 / 100
1209 ms 92072 KB
#include "parks.h"
#include <bits/stdc++.h>

using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;

const int MAXN = 6e5+7;
vector <int> graph[MAXN];
int N, RN, BN, degree[MAXN], parent[MAXN];
int used[MAXN];
int link[MAXN], saizu[MAXN];
int x[MAXN], y[MAXN];
vector <int> WrongRoads;
int numOfWrongRoads;
pii roads[MAXN];
vector <pii> benches;

int find(int n) {
    if (n == link[n]) return n;
    return link[n] = find(link[n]);
}

void unite(int a, int b) {
    a = find(a);
    b = find(b);
    if (a == b) return;
    if (saizu[a] < saizu[b]) swap(a, b);
    saizu[a] += saizu[b];
    link[b] = a;
}

bool same(int a, int b) {
    return find(a) == find(b);
}

void dfs(int v) {
    used[v] = 1;
    for (auto to : graph[v]) {
        if (!used[to]) {
            dfs(to);
        }
    }
}

void dfs2(int v, int p) {
    used[v] = 1;
    if (v < RN && sz(graph[v]) == 1) {
        WrongRoads.pb(v);
        numOfWrongRoads++;
    }
    for (auto to : graph[v]) {
        if (!used[to]) {
            dfs2(to, v);
        }
    }
    used[v] = 2;
}

void dfs3(int v, int p) {
    parent[v] = p;
    used[v] = 1;
    for (auto to : graph[v]) {
        if (used[to]) continue;
        degree[v]++;
        dfs3(to, v);
    }
}

int construct_roads(vector<int> X, vector<int> Y) {
    N = sz(X);
    
    if (N == 1) {
        build({}, {}, {}, {});
        return 1;
    }
    
    for (int i = 0; i < N; i++) {
        link[i] = i;
        saizu[i] = 1;
        x[i] = X[i];
        y[i] = Y[i];
    }
    X.clear(); Y.clear();
    
    //***************** get roads and benches *****************
    vector <pair <pii, int>> tmp;
    vector <pii> edges;
    set <pii> tmpBenches;
    for (int i = 0; i < N; i++) {
        tmp.pb(mp(mp(x[i], y[i]), i));
    }
    sort(all(tmp));
    for (int i = 0; i < N; i++) {
        auto it = lb(all(tmp), mp(mp(x[i]-2, y[i]), 0));
        if (it != tmp.end() && (*it).first == mp(x[i]-2, y[i])) {
            edges.pb(mp(i, (*it).second));
        }
        it = lb(all(tmp), mp(mp(x[i]+2, y[i]), 0));
        if (it != tmp.end() && (*it).first == mp(x[i]+2, y[i])) {
            edges.pb(mp(i, (*it).second));
        }
        it = lb(all(tmp), mp(mp(x[i], y[i]-2), 0));
        if (it != tmp.end() && (*it).first == mp(x[i], y[i]-2)) {
            edges.pb(mp(i, (*it).second));
        }
        it = lb(all(tmp), mp(mp(x[i], y[i]+2), 0));
        if (it != tmp.end() && (*it).first == mp(x[i], y[i]+2)) {
            edges.pb(mp(i, (*it).second));
        }
    }
    for (auto &to : edges) {
        if (mp(x[to.first], y[to.first]) > mp(x[to.second], y[to.second])) {
            swap(to.first, to.second);
        }
        if (!same(to.first, to.second)) {
            unite(to.first, to.second);
            graph[to.first].pb(to.second);
            graph[to.second].pb(to.first);
            roads[RN++] = mp(to.first, to.second);
            if (x[to.first] == x[to.second]) {
                tmpBenches.insert(mp(x[to.first]-1, y[to.first]+1));
                tmpBenches.insert(mp(x[to.first]+1, y[to.first]+1));
            }
            else {
                tmpBenches.insert(mp(x[to.first]+1, y[to.first]+1));
                tmpBenches.insert(mp(x[to.first]+1, y[to.first]-1));
            }
        }
    }
    for (auto to : tmpBenches) {
        benches.pb(to);
    }
    BN = sz(benches);
    //***************** get roads and benches *****************
    
    //***************** check if connected ***************** 
    dfs(0);
    bool flag = 1;
    for (int i = 0; i < N; i++) {
        if (!used[i]) {
            flag = 0;
            break;
        }
    }
    if (!flag) {
        return 0;
    }
    for (int i = 0; i < RN+BN; i++) {
        link[i] = i;
        saizu[i] = 1;
        used[i] = 0;
        graph[i].clear();
    }
    edges.clear();
    //***************** check if connected ***************** 
    
    //***************** create a new graph *****************
    for (int i = 0; i < RN; i++) {
        auto a = roads[i].first, b = roads[i].second;
        if (x[a] == x[b]) {
            auto it = lb(all(benches), mp(x[a]-1, y[a]+1))-benches.begin();
            edges.pb(mp(i, it+RN));
            it = lb(all(benches), mp(x[a]+1, y[a]+1))-benches.begin();
            edges.pb(mp(i, it+RN));
        }
        else {
            auto it = lb(all(benches), mp(x[a]+1, y[a]-1))-benches.begin();
            edges.pb(mp(i, it+RN));
            it = lb(all(benches), mp(x[a]+1, y[a]+1))-benches.begin();
            edges.pb(mp(i, it+RN));
        }
    }
    for (auto to : edges) {
        if (!same(to.first, to.second)) {
            unite(to.first, to.second);
            graph[to.first].pb(to.second);
            graph[to.second].pb(to.first);
        }
    }
    for (int i = 0; i < RN; i++) {
        if (!used[i]) {
            numOfWrongRoads = 0;
            dfs2(i, i);
            if (numOfWrongRoads) {
                WrongRoads.pop_back();
            }
        }
    }
    for (int i = 0; i < RN+BN; i++) {
        used[i] = 0;
    }
    for (auto to : WrongRoads) {
        used[to] = 1;
    }
    for (int i = 0; i < RN; i++) {
        if (!used[i] && sz(graph[i]) == 1) {
            dfs3(i, i);
        }
    }
    for (int i = 0; i < RN; i++) {
        if (!used[i]) {
            dfs3(i, i);
        }
    }
    for (int i = 0; i < RN+BN; i++) {
        used[i] = 0;
    }
    //***************** create a new graph *****************
    
    //***************** get the answer *****************
    vector <int> _u, _v, _x, _y;
    queue <int> K;
    for (int i = RN; i < RN+BN; i++) {
        if (sz(graph[i]) == 1) {
            K.push(i);
        }
    }
    while (!K.empty()) {
        auto v = K.front(); K.pop();
        if (!used[v] && !used[parent[v]]) {
            used[v] = 1;
            used[parent[v]] = 1;
            _u.pb(roads[parent[v]].first);
            _v.pb(roads[parent[v]].second);
            _x.pb(benches[v-RN].first);
            _y.pb(benches[v-RN].second);
            v = parent[parent[v]];
            degree[v]--;
            if (!used[v] && degree[v] == 0) {
                K.push(v);
            }
        }
    }
    build(_u, _v, _x, _y);
    //***************** get the answer *****************
    
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14336 KB Output is correct
2 Correct 10 ms 14428 KB Output is correct
3 Correct 11 ms 14412 KB Output is correct
4 Correct 10 ms 14396 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 11 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14428 KB Output is correct
9 Correct 473 ms 53188 KB Output is correct
10 Correct 28 ms 18316 KB Output is correct
11 Correct 123 ms 35304 KB Output is correct
12 Correct 38 ms 20256 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 11 ms 14540 KB Output is correct
15 Correct 12 ms 14764 KB Output is correct
16 Correct 458 ms 52632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14336 KB Output is correct
2 Correct 10 ms 14428 KB Output is correct
3 Correct 11 ms 14412 KB Output is correct
4 Correct 10 ms 14396 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 11 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14428 KB Output is correct
9 Correct 473 ms 53188 KB Output is correct
10 Correct 28 ms 18316 KB Output is correct
11 Correct 123 ms 35304 KB Output is correct
12 Correct 38 ms 20256 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 11 ms 14540 KB Output is correct
15 Correct 12 ms 14764 KB Output is correct
16 Correct 458 ms 52632 KB Output is correct
17 Correct 9 ms 14384 KB Output is correct
18 Correct 10 ms 14408 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 11 ms 14412 KB Output is correct
21 Correct 12 ms 14420 KB Output is correct
22 Correct 10 ms 14412 KB Output is correct
23 Correct 1044 ms 74108 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 12 ms 14796 KB Output is correct
26 Correct 14 ms 14924 KB Output is correct
27 Correct 15 ms 15180 KB Output is correct
28 Correct 320 ms 38584 KB Output is correct
29 Correct 543 ms 49716 KB Output is correct
30 Correct 884 ms 62736 KB Output is correct
31 Correct 1036 ms 73724 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 10 ms 14444 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 10 ms 14404 KB Output is correct
37 Correct 10 ms 14412 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 9 ms 14412 KB Output is correct
40 Correct 10 ms 14412 KB Output is correct
41 Correct 12 ms 14412 KB Output is correct
42 Correct 11 ms 14412 KB Output is correct
43 Correct 12 ms 14796 KB Output is correct
44 Correct 14 ms 15016 KB Output is correct
45 Correct 553 ms 46084 KB Output is correct
46 Correct 781 ms 61300 KB Output is correct
47 Correct 794 ms 61188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14336 KB Output is correct
2 Correct 10 ms 14428 KB Output is correct
3 Correct 11 ms 14412 KB Output is correct
4 Correct 10 ms 14396 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 11 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14428 KB Output is correct
9 Correct 473 ms 53188 KB Output is correct
10 Correct 28 ms 18316 KB Output is correct
11 Correct 123 ms 35304 KB Output is correct
12 Correct 38 ms 20256 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 11 ms 14540 KB Output is correct
15 Correct 12 ms 14764 KB Output is correct
16 Correct 458 ms 52632 KB Output is correct
17 Correct 9 ms 14384 KB Output is correct
18 Correct 10 ms 14408 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 11 ms 14412 KB Output is correct
21 Correct 12 ms 14420 KB Output is correct
22 Correct 10 ms 14412 KB Output is correct
23 Correct 1044 ms 74108 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 12 ms 14796 KB Output is correct
26 Correct 14 ms 14924 KB Output is correct
27 Correct 15 ms 15180 KB Output is correct
28 Correct 320 ms 38584 KB Output is correct
29 Correct 543 ms 49716 KB Output is correct
30 Correct 884 ms 62736 KB Output is correct
31 Correct 1036 ms 73724 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 10 ms 14444 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 10 ms 14404 KB Output is correct
37 Correct 10 ms 14412 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 9 ms 14412 KB Output is correct
40 Correct 10 ms 14412 KB Output is correct
41 Correct 12 ms 14412 KB Output is correct
42 Correct 11 ms 14412 KB Output is correct
43 Correct 12 ms 14796 KB Output is correct
44 Correct 14 ms 15016 KB Output is correct
45 Correct 553 ms 46084 KB Output is correct
46 Correct 781 ms 61300 KB Output is correct
47 Correct 794 ms 61188 KB Output is correct
48 Correct 9 ms 14352 KB Output is correct
49 Correct 9 ms 14412 KB Output is correct
50 Correct 10 ms 14412 KB Output is correct
51 Correct 12 ms 14428 KB Output is correct
52 Correct 11 ms 14412 KB Output is correct
53 Correct 13 ms 14340 KB Output is correct
54 Correct 11 ms 14412 KB Output is correct
55 Incorrect 1056 ms 71568 KB Given structure is not connected: There is no path between vertices 0 and 1
56 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14336 KB Output is correct
2 Correct 10 ms 14428 KB Output is correct
3 Correct 11 ms 14412 KB Output is correct
4 Correct 10 ms 14396 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 11 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14428 KB Output is correct
9 Correct 473 ms 53188 KB Output is correct
10 Correct 28 ms 18316 KB Output is correct
11 Correct 123 ms 35304 KB Output is correct
12 Correct 38 ms 20256 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 11 ms 14540 KB Output is correct
15 Correct 12 ms 14764 KB Output is correct
16 Correct 458 ms 52632 KB Output is correct
17 Correct 11 ms 14412 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 1080 ms 92072 KB Output is correct
21 Correct 1124 ms 74720 KB Output is correct
22 Correct 1055 ms 75860 KB Output is correct
23 Correct 862 ms 80084 KB Output is correct
24 Correct 203 ms 23868 KB Output is correct
25 Correct 699 ms 58816 KB Output is correct
26 Correct 656 ms 58892 KB Output is correct
27 Correct 1097 ms 90296 KB Output is correct
28 Correct 1079 ms 90232 KB Output is correct
29 Correct 1128 ms 88796 KB Output is correct
30 Correct 1121 ms 88784 KB Output is correct
31 Correct 11 ms 14412 KB Output is correct
32 Correct 49 ms 18708 KB Output is correct
33 Correct 98 ms 19160 KB Output is correct
34 Correct 1028 ms 88000 KB Output is correct
35 Correct 30 ms 16208 KB Output is correct
36 Correct 112 ms 23236 KB Output is correct
37 Correct 259 ms 32060 KB Output is correct
38 Correct 359 ms 37688 KB Output is correct
39 Correct 590 ms 45796 KB Output is correct
40 Correct 788 ms 55540 KB Output is correct
41 Correct 963 ms 63704 KB Output is correct
42 Correct 1127 ms 71928 KB Output is correct
43 Correct 10 ms 14372 KB Output is correct
44 Correct 10 ms 14412 KB Output is correct
45 Correct 10 ms 14452 KB Output is correct
46 Correct 10 ms 14400 KB Output is correct
47 Correct 10 ms 14352 KB Output is correct
48 Correct 10 ms 14412 KB Output is correct
49 Correct 10 ms 14336 KB Output is correct
50 Correct 10 ms 14412 KB Output is correct
51 Correct 9 ms 14412 KB Output is correct
52 Correct 9 ms 14412 KB Output is correct
53 Correct 10 ms 14412 KB Output is correct
54 Correct 13 ms 14740 KB Output is correct
55 Correct 14 ms 15000 KB Output is correct
56 Correct 505 ms 46156 KB Output is correct
57 Correct 795 ms 61324 KB Output is correct
58 Correct 847 ms 61224 KB Output is correct
59 Correct 10 ms 14412 KB Output is correct
60 Correct 10 ms 14428 KB Output is correct
61 Correct 10 ms 14412 KB Output is correct
62 Correct 1110 ms 91264 KB Output is correct
63 Correct 1100 ms 91476 KB Output is correct
64 Correct 1105 ms 91416 KB Output is correct
65 Correct 16 ms 15192 KB Output is correct
66 Correct 22 ms 15936 KB Output is correct
67 Correct 487 ms 45088 KB Output is correct
68 Correct 805 ms 61724 KB Output is correct
69 Correct 1147 ms 76016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14336 KB Output is correct
2 Correct 10 ms 14428 KB Output is correct
3 Correct 11 ms 14412 KB Output is correct
4 Correct 10 ms 14396 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 11 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14428 KB Output is correct
9 Correct 473 ms 53188 KB Output is correct
10 Correct 28 ms 18316 KB Output is correct
11 Correct 123 ms 35304 KB Output is correct
12 Correct 38 ms 20256 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 11 ms 14540 KB Output is correct
15 Correct 12 ms 14764 KB Output is correct
16 Correct 458 ms 52632 KB Output is correct
17 Correct 1209 ms 91968 KB Output is correct
18 Correct 1076 ms 90632 KB Output is correct
19 Correct 1136 ms 88240 KB Output is correct
20 Correct 1071 ms 72604 KB Output is correct
21 Correct 1068 ms 74656 KB Output is correct
22 Correct 10 ms 14412 KB Output is correct
23 Correct 113 ms 23608 KB Output is correct
24 Correct 48 ms 18128 KB Output is correct
25 Correct 169 ms 27560 KB Output is correct
26 Correct 316 ms 37172 KB Output is correct
27 Correct 477 ms 44084 KB Output is correct
28 Correct 625 ms 51072 KB Output is correct
29 Correct 879 ms 59548 KB Output is correct
30 Correct 991 ms 66612 KB Output is correct
31 Correct 1137 ms 73740 KB Output is correct
32 Correct 1097 ms 79596 KB Output is correct
33 Correct 1106 ms 91872 KB Output is correct
34 Correct 17 ms 15308 KB Output is correct
35 Correct 24 ms 16140 KB Output is correct
36 Correct 509 ms 45684 KB Output is correct
37 Correct 753 ms 62132 KB Output is correct
38 Correct 1076 ms 77124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 14336 KB Output is correct
2 Correct 10 ms 14428 KB Output is correct
3 Correct 11 ms 14412 KB Output is correct
4 Correct 10 ms 14396 KB Output is correct
5 Correct 9 ms 14412 KB Output is correct
6 Correct 11 ms 14412 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14428 KB Output is correct
9 Correct 473 ms 53188 KB Output is correct
10 Correct 28 ms 18316 KB Output is correct
11 Correct 123 ms 35304 KB Output is correct
12 Correct 38 ms 20256 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 11 ms 14540 KB Output is correct
15 Correct 12 ms 14764 KB Output is correct
16 Correct 458 ms 52632 KB Output is correct
17 Correct 9 ms 14384 KB Output is correct
18 Correct 10 ms 14408 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 11 ms 14412 KB Output is correct
21 Correct 12 ms 14420 KB Output is correct
22 Correct 10 ms 14412 KB Output is correct
23 Correct 1044 ms 74108 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 12 ms 14796 KB Output is correct
26 Correct 14 ms 14924 KB Output is correct
27 Correct 15 ms 15180 KB Output is correct
28 Correct 320 ms 38584 KB Output is correct
29 Correct 543 ms 49716 KB Output is correct
30 Correct 884 ms 62736 KB Output is correct
31 Correct 1036 ms 73724 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 10 ms 14444 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 10 ms 14404 KB Output is correct
37 Correct 10 ms 14412 KB Output is correct
38 Correct 10 ms 14412 KB Output is correct
39 Correct 9 ms 14412 KB Output is correct
40 Correct 10 ms 14412 KB Output is correct
41 Correct 12 ms 14412 KB Output is correct
42 Correct 11 ms 14412 KB Output is correct
43 Correct 12 ms 14796 KB Output is correct
44 Correct 14 ms 15016 KB Output is correct
45 Correct 553 ms 46084 KB Output is correct
46 Correct 781 ms 61300 KB Output is correct
47 Correct 794 ms 61188 KB Output is correct
48 Correct 9 ms 14352 KB Output is correct
49 Correct 9 ms 14412 KB Output is correct
50 Correct 10 ms 14412 KB Output is correct
51 Correct 12 ms 14428 KB Output is correct
52 Correct 11 ms 14412 KB Output is correct
53 Correct 13 ms 14340 KB Output is correct
54 Correct 11 ms 14412 KB Output is correct
55 Incorrect 1056 ms 71568 KB Given structure is not connected: There is no path between vertices 0 and 1
56 Halted 0 ms 0 KB -