Submission #436092

# Submission time Handle Problem Language Result Execution time Memory
436092 2021-06-24T08:20:43 Z talant117408 Fountain Parks (IOI21_parks) C++17
55 / 100
1180 ms 92116 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];
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) {
    parent[v] = p;
    used[v] = 1;
    for (auto to : graph[v]) {
        if (used[to]) continue;
        degree[v]++;
        dfs2(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) {
        graph[to.first].pb(to.second);
        graph[to.second].pb(to.first);
    }
    for (int i = 0; i < RN; i++) {
        if (!used[i]) {
            dfs2(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 = 0; i < RN+BN; i++) {
        if (degree[i] == 0) {
          if (i >= RN) {
            K.push(i);
          }
          else if (parent[i] >= RN) {
            K.push(parent[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 10 ms 14284 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14348 KB Output is correct
5 Correct 11 ms 14364 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14384 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53220 KB Output is correct
10 Correct 29 ms 18308 KB Output is correct
11 Correct 122 ms 35256 KB Output is correct
12 Correct 37 ms 20264 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 15 ms 14540 KB Output is correct
15 Correct 12 ms 14796 KB Output is correct
16 Correct 460 ms 52676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14348 KB Output is correct
5 Correct 11 ms 14364 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14384 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53220 KB Output is correct
10 Correct 29 ms 18308 KB Output is correct
11 Correct 122 ms 35256 KB Output is correct
12 Correct 37 ms 20264 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 15 ms 14540 KB Output is correct
15 Correct 12 ms 14796 KB Output is correct
16 Correct 460 ms 52676 KB Output is correct
17 Correct 11 ms 14412 KB Output is correct
18 Correct 10 ms 14412 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 11 ms 14412 KB Output is correct
21 Correct 11 ms 14412 KB Output is correct
22 Correct 11 ms 14412 KB Output is correct
23 Correct 962 ms 73984 KB Output is correct
24 Correct 11 ms 14416 KB Output is correct
25 Correct 16 ms 14796 KB Output is correct
26 Correct 15 ms 14920 KB Output is correct
27 Correct 15 ms 15180 KB Output is correct
28 Correct 311 ms 38696 KB Output is correct
29 Correct 535 ms 49704 KB Output is correct
30 Correct 796 ms 62808 KB Output is correct
31 Correct 1051 ms 73804 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 10 ms 14368 KB Output is correct
34 Correct 11 ms 14412 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 10 ms 14348 KB Output is correct
37 Correct 10 ms 14432 KB Output is correct
38 Correct 10 ms 14356 KB Output is correct
39 Correct 10 ms 14412 KB Output is correct
40 Correct 11 ms 14380 KB Output is correct
41 Correct 10 ms 14412 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 15 ms 14764 KB Output is correct
44 Correct 14 ms 14924 KB Output is correct
45 Correct 446 ms 46048 KB Output is correct
46 Correct 772 ms 61320 KB Output is correct
47 Correct 760 ms 61256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14348 KB Output is correct
5 Correct 11 ms 14364 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14384 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53220 KB Output is correct
10 Correct 29 ms 18308 KB Output is correct
11 Correct 122 ms 35256 KB Output is correct
12 Correct 37 ms 20264 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 15 ms 14540 KB Output is correct
15 Correct 12 ms 14796 KB Output is correct
16 Correct 460 ms 52676 KB Output is correct
17 Correct 11 ms 14412 KB Output is correct
18 Correct 10 ms 14412 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 11 ms 14412 KB Output is correct
21 Correct 11 ms 14412 KB Output is correct
22 Correct 11 ms 14412 KB Output is correct
23 Correct 962 ms 73984 KB Output is correct
24 Correct 11 ms 14416 KB Output is correct
25 Correct 16 ms 14796 KB Output is correct
26 Correct 15 ms 14920 KB Output is correct
27 Correct 15 ms 15180 KB Output is correct
28 Correct 311 ms 38696 KB Output is correct
29 Correct 535 ms 49704 KB Output is correct
30 Correct 796 ms 62808 KB Output is correct
31 Correct 1051 ms 73804 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 10 ms 14368 KB Output is correct
34 Correct 11 ms 14412 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 10 ms 14348 KB Output is correct
37 Correct 10 ms 14432 KB Output is correct
38 Correct 10 ms 14356 KB Output is correct
39 Correct 10 ms 14412 KB Output is correct
40 Correct 11 ms 14380 KB Output is correct
41 Correct 10 ms 14412 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 15 ms 14764 KB Output is correct
44 Correct 14 ms 14924 KB Output is correct
45 Correct 446 ms 46048 KB Output is correct
46 Correct 772 ms 61320 KB Output is correct
47 Correct 760 ms 61256 KB Output is correct
48 Correct 11 ms 14412 KB Output is correct
49 Correct 10 ms 14368 KB Output is correct
50 Correct 13 ms 14540 KB Output is correct
51 Correct 11 ms 14412 KB Output is correct
52 Correct 10 ms 14412 KB Output is correct
53 Correct 11 ms 14412 KB Output is correct
54 Correct 10 ms 14344 KB Output is correct
55 Incorrect 1018 ms 71772 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 10 ms 14284 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14348 KB Output is correct
5 Correct 11 ms 14364 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14384 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53220 KB Output is correct
10 Correct 29 ms 18308 KB Output is correct
11 Correct 122 ms 35256 KB Output is correct
12 Correct 37 ms 20264 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 15 ms 14540 KB Output is correct
15 Correct 12 ms 14796 KB Output is correct
16 Correct 460 ms 52676 KB Output is correct
17 Correct 11 ms 14412 KB Output is correct
18 Correct 10 ms 14392 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 1059 ms 92108 KB Output is correct
21 Correct 1028 ms 74752 KB Output is correct
22 Correct 1008 ms 75892 KB Output is correct
23 Correct 887 ms 80000 KB Output is correct
24 Correct 200 ms 23868 KB Output is correct
25 Correct 761 ms 58832 KB Output is correct
26 Correct 702 ms 58796 KB Output is correct
27 Correct 1080 ms 90356 KB Output is correct
28 Correct 1120 ms 90604 KB Output is correct
29 Correct 1180 ms 88752 KB Output is correct
30 Correct 1111 ms 88788 KB Output is correct
31 Correct 11 ms 14412 KB Output is correct
32 Correct 50 ms 18676 KB Output is correct
33 Correct 101 ms 19132 KB Output is correct
34 Correct 1112 ms 88004 KB Output is correct
35 Correct 26 ms 16140 KB Output is correct
36 Correct 152 ms 23216 KB Output is correct
37 Correct 278 ms 32060 KB Output is correct
38 Correct 341 ms 37800 KB Output is correct
39 Correct 585 ms 45888 KB Output is correct
40 Correct 832 ms 55596 KB Output is correct
41 Correct 860 ms 63816 KB Output is correct
42 Correct 1060 ms 71996 KB Output is correct
43 Correct 10 ms 14412 KB Output is correct
44 Correct 9 ms 14412 KB Output is correct
45 Correct 11 ms 14432 KB Output is correct
46 Correct 10 ms 14412 KB Output is correct
47 Correct 9 ms 14412 KB Output is correct
48 Correct 11 ms 14412 KB Output is correct
49 Correct 10 ms 14412 KB Output is correct
50 Correct 10 ms 14412 KB Output is correct
51 Correct 11 ms 14412 KB Output is correct
52 Correct 11 ms 14344 KB Output is correct
53 Correct 11 ms 14356 KB Output is correct
54 Correct 15 ms 14796 KB Output is correct
55 Correct 15 ms 14924 KB Output is correct
56 Correct 478 ms 46044 KB Output is correct
57 Correct 800 ms 61424 KB Output is correct
58 Correct 776 ms 61228 KB Output is correct
59 Correct 11 ms 14412 KB Output is correct
60 Correct 10 ms 14412 KB Output is correct
61 Correct 10 ms 14316 KB Output is correct
62 Correct 1059 ms 91360 KB Output is correct
63 Correct 1045 ms 91252 KB Output is correct
64 Correct 1121 ms 91308 KB Output is correct
65 Correct 17 ms 15180 KB Output is correct
66 Correct 21 ms 15908 KB Output is correct
67 Correct 439 ms 45176 KB Output is correct
68 Correct 849 ms 61768 KB Output is correct
69 Correct 1107 ms 76136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14348 KB Output is correct
5 Correct 11 ms 14364 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14384 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53220 KB Output is correct
10 Correct 29 ms 18308 KB Output is correct
11 Correct 122 ms 35256 KB Output is correct
12 Correct 37 ms 20264 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 15 ms 14540 KB Output is correct
15 Correct 12 ms 14796 KB Output is correct
16 Correct 460 ms 52676 KB Output is correct
17 Correct 1074 ms 92116 KB Output is correct
18 Correct 1098 ms 90580 KB Output is correct
19 Correct 1034 ms 88112 KB Output is correct
20 Correct 954 ms 72672 KB Output is correct
21 Correct 931 ms 74768 KB Output is correct
22 Correct 11 ms 14380 KB Output is correct
23 Correct 145 ms 23672 KB Output is correct
24 Correct 47 ms 18116 KB Output is correct
25 Correct 172 ms 27476 KB Output is correct
26 Correct 345 ms 37080 KB Output is correct
27 Correct 420 ms 44056 KB Output is correct
28 Correct 615 ms 51032 KB Output is correct
29 Correct 723 ms 59648 KB Output is correct
30 Correct 897 ms 66648 KB Output is correct
31 Correct 1065 ms 73604 KB Output is correct
32 Correct 1053 ms 79584 KB Output is correct
33 Correct 1138 ms 91784 KB Output is correct
34 Correct 18 ms 15308 KB Output is correct
35 Correct 24 ms 16108 KB Output is correct
36 Correct 503 ms 45656 KB Output is correct
37 Correct 781 ms 62120 KB Output is correct
38 Correct 1107 ms 77340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 11 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 10 ms 14348 KB Output is correct
5 Correct 11 ms 14364 KB Output is correct
6 Correct 10 ms 14412 KB Output is correct
7 Correct 10 ms 14384 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53220 KB Output is correct
10 Correct 29 ms 18308 KB Output is correct
11 Correct 122 ms 35256 KB Output is correct
12 Correct 37 ms 20264 KB Output is correct
13 Correct 63 ms 24380 KB Output is correct
14 Correct 15 ms 14540 KB Output is correct
15 Correct 12 ms 14796 KB Output is correct
16 Correct 460 ms 52676 KB Output is correct
17 Correct 11 ms 14412 KB Output is correct
18 Correct 10 ms 14412 KB Output is correct
19 Correct 10 ms 14412 KB Output is correct
20 Correct 11 ms 14412 KB Output is correct
21 Correct 11 ms 14412 KB Output is correct
22 Correct 11 ms 14412 KB Output is correct
23 Correct 962 ms 73984 KB Output is correct
24 Correct 11 ms 14416 KB Output is correct
25 Correct 16 ms 14796 KB Output is correct
26 Correct 15 ms 14920 KB Output is correct
27 Correct 15 ms 15180 KB Output is correct
28 Correct 311 ms 38696 KB Output is correct
29 Correct 535 ms 49704 KB Output is correct
30 Correct 796 ms 62808 KB Output is correct
31 Correct 1051 ms 73804 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 10 ms 14368 KB Output is correct
34 Correct 11 ms 14412 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 10 ms 14348 KB Output is correct
37 Correct 10 ms 14432 KB Output is correct
38 Correct 10 ms 14356 KB Output is correct
39 Correct 10 ms 14412 KB Output is correct
40 Correct 11 ms 14380 KB Output is correct
41 Correct 10 ms 14412 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 15 ms 14764 KB Output is correct
44 Correct 14 ms 14924 KB Output is correct
45 Correct 446 ms 46048 KB Output is correct
46 Correct 772 ms 61320 KB Output is correct
47 Correct 760 ms 61256 KB Output is correct
48 Correct 11 ms 14412 KB Output is correct
49 Correct 10 ms 14368 KB Output is correct
50 Correct 13 ms 14540 KB Output is correct
51 Correct 11 ms 14412 KB Output is correct
52 Correct 10 ms 14412 KB Output is correct
53 Correct 11 ms 14412 KB Output is correct
54 Correct 10 ms 14344 KB Output is correct
55 Incorrect 1018 ms 71772 KB Given structure is not connected: There is no path between vertices 0 and 1
56 Halted 0 ms 0 KB -