답안 #435733

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435733 2021-06-23T16:07:35 Z talant117408 분수 공원 (IOI21_parks) C++17
55 / 100
1383 ms 92084 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];
int numOfRoads, numOfBenches;
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) numOfRoads++;
    else numOfBenches++;
    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]) {
            numOfRoads = numOfBenches = 0;
            dfs2(i, i);
            if (numOfRoads > numOfBenches) {
                return 0;
            }
        }
    }
    for (int i = 0; i < RN+BN; i++) {
        used[i] = 0;
    }
    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 (!degree[i]) {
            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]];
            if (v >= RN) {
                degree[v]--;
                if (!used[v] && !degree[v]) {
                    K.push(v);
                }
            }
        }
    }
    build(_u, _v, _x, _y);
    //***************** get the answer *****************
    
    return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14444 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53144 KB Output is correct
10 Correct 36 ms 18308 KB Output is correct
11 Correct 124 ms 35320 KB Output is correct
12 Correct 35 ms 20200 KB Output is correct
13 Correct 64 ms 24328 KB Output is correct
14 Correct 10 ms 14576 KB Output is correct
15 Correct 14 ms 14796 KB Output is correct
16 Correct 485 ms 52500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14444 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53144 KB Output is correct
10 Correct 36 ms 18308 KB Output is correct
11 Correct 124 ms 35320 KB Output is correct
12 Correct 35 ms 20200 KB Output is correct
13 Correct 64 ms 24328 KB Output is correct
14 Correct 10 ms 14576 KB Output is correct
15 Correct 14 ms 14796 KB Output is correct
16 Correct 485 ms 52500 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 10 ms 14412 KB Output is correct
21 Correct 10 ms 14412 KB Output is correct
22 Correct 10 ms 14412 KB Output is correct
23 Correct 1054 ms 73972 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 12 ms 14796 KB Output is correct
26 Correct 15 ms 14924 KB Output is correct
27 Correct 14 ms 15180 KB Output is correct
28 Correct 370 ms 38604 KB Output is correct
29 Correct 592 ms 49812 KB Output is correct
30 Correct 833 ms 62808 KB Output is correct
31 Correct 1075 ms 73828 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 9 ms 14412 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 9 ms 14412 KB Output is correct
37 Correct 9 ms 14412 KB Output is correct
38 Correct 9 ms 14412 KB Output is correct
39 Correct 10 ms 14376 KB Output is correct
40 Correct 9 ms 14412 KB Output is correct
41 Correct 12 ms 14424 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 13 ms 14796 KB Output is correct
44 Correct 17 ms 14924 KB Output is correct
45 Correct 459 ms 46096 KB Output is correct
46 Correct 783 ms 61308 KB Output is correct
47 Correct 878 ms 61268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14444 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53144 KB Output is correct
10 Correct 36 ms 18308 KB Output is correct
11 Correct 124 ms 35320 KB Output is correct
12 Correct 35 ms 20200 KB Output is correct
13 Correct 64 ms 24328 KB Output is correct
14 Correct 10 ms 14576 KB Output is correct
15 Correct 14 ms 14796 KB Output is correct
16 Correct 485 ms 52500 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 10 ms 14412 KB Output is correct
21 Correct 10 ms 14412 KB Output is correct
22 Correct 10 ms 14412 KB Output is correct
23 Correct 1054 ms 73972 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 12 ms 14796 KB Output is correct
26 Correct 15 ms 14924 KB Output is correct
27 Correct 14 ms 15180 KB Output is correct
28 Correct 370 ms 38604 KB Output is correct
29 Correct 592 ms 49812 KB Output is correct
30 Correct 833 ms 62808 KB Output is correct
31 Correct 1075 ms 73828 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 9 ms 14412 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 9 ms 14412 KB Output is correct
37 Correct 9 ms 14412 KB Output is correct
38 Correct 9 ms 14412 KB Output is correct
39 Correct 10 ms 14376 KB Output is correct
40 Correct 9 ms 14412 KB Output is correct
41 Correct 12 ms 14424 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 13 ms 14796 KB Output is correct
44 Correct 17 ms 14924 KB Output is correct
45 Correct 459 ms 46096 KB Output is correct
46 Correct 783 ms 61308 KB Output is correct
47 Correct 878 ms 61268 KB Output is correct
48 Correct 9 ms 14412 KB Output is correct
49 Correct 11 ms 14412 KB Output is correct
50 Correct 10 ms 14420 KB Output is correct
51 Correct 10 ms 14412 KB Output is correct
52 Correct 9 ms 14412 KB Output is correct
53 Correct 9 ms 14392 KB Output is correct
54 Correct 10 ms 14412 KB Output is correct
55 Incorrect 937 ms 58480 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14444 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53144 KB Output is correct
10 Correct 36 ms 18308 KB Output is correct
11 Correct 124 ms 35320 KB Output is correct
12 Correct 35 ms 20200 KB Output is correct
13 Correct 64 ms 24328 KB Output is correct
14 Correct 10 ms 14576 KB Output is correct
15 Correct 14 ms 14796 KB Output is correct
16 Correct 485 ms 52500 KB Output is correct
17 Correct 9 ms 14328 KB Output is correct
18 Correct 9 ms 14412 KB Output is correct
19 Correct 13 ms 14384 KB Output is correct
20 Correct 1054 ms 92084 KB Output is correct
21 Correct 1111 ms 74696 KB Output is correct
22 Correct 1198 ms 75804 KB Output is correct
23 Correct 953 ms 80012 KB Output is correct
24 Correct 236 ms 23880 KB Output is correct
25 Correct 764 ms 58876 KB Output is correct
26 Correct 723 ms 58900 KB Output is correct
27 Correct 1116 ms 90252 KB Output is correct
28 Correct 1311 ms 90240 KB Output is correct
29 Correct 1357 ms 88652 KB Output is correct
30 Correct 1247 ms 88768 KB Output is correct
31 Correct 11 ms 14412 KB Output is correct
32 Correct 62 ms 18648 KB Output is correct
33 Correct 103 ms 19160 KB Output is correct
34 Correct 1127 ms 87924 KB Output is correct
35 Correct 30 ms 16124 KB Output is correct
36 Correct 115 ms 23216 KB Output is correct
37 Correct 309 ms 32080 KB Output is correct
38 Correct 402 ms 37692 KB Output is correct
39 Correct 652 ms 45868 KB Output is correct
40 Correct 811 ms 55524 KB Output is correct
41 Correct 949 ms 63836 KB Output is correct
42 Correct 1293 ms 71856 KB Output is correct
43 Correct 9 ms 14412 KB Output is correct
44 Correct 18 ms 14412 KB Output is correct
45 Correct 17 ms 14416 KB Output is correct
46 Correct 10 ms 14412 KB Output is correct
47 Correct 12 ms 14412 KB Output is correct
48 Correct 18 ms 14364 KB Output is correct
49 Correct 18 ms 14324 KB Output is correct
50 Correct 18 ms 14412 KB Output is correct
51 Correct 13 ms 14412 KB Output is correct
52 Correct 11 ms 14412 KB Output is correct
53 Correct 11 ms 14412 KB Output is correct
54 Correct 13 ms 14796 KB Output is correct
55 Correct 15 ms 14924 KB Output is correct
56 Correct 518 ms 46148 KB Output is correct
57 Correct 803 ms 61320 KB Output is correct
58 Correct 817 ms 61304 KB Output is correct
59 Correct 11 ms 14412 KB Output is correct
60 Correct 9 ms 14328 KB Output is correct
61 Correct 9 ms 14412 KB Output is correct
62 Correct 1198 ms 91208 KB Output is correct
63 Correct 1249 ms 91412 KB Output is correct
64 Correct 1237 ms 91400 KB Output is correct
65 Correct 31 ms 15152 KB Output is correct
66 Correct 22 ms 15936 KB Output is correct
67 Correct 533 ms 45308 KB Output is correct
68 Correct 979 ms 61744 KB Output is correct
69 Correct 1383 ms 75944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14444 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53144 KB Output is correct
10 Correct 36 ms 18308 KB Output is correct
11 Correct 124 ms 35320 KB Output is correct
12 Correct 35 ms 20200 KB Output is correct
13 Correct 64 ms 24328 KB Output is correct
14 Correct 10 ms 14576 KB Output is correct
15 Correct 14 ms 14796 KB Output is correct
16 Correct 485 ms 52500 KB Output is correct
17 Correct 1160 ms 91872 KB Output is correct
18 Correct 1142 ms 90516 KB Output is correct
19 Correct 1121 ms 88216 KB Output is correct
20 Correct 1116 ms 72632 KB Output is correct
21 Correct 964 ms 74600 KB Output is correct
22 Correct 11 ms 14412 KB Output is correct
23 Correct 111 ms 23668 KB Output is correct
24 Correct 55 ms 18116 KB Output is correct
25 Correct 189 ms 27448 KB Output is correct
26 Correct 337 ms 37192 KB Output is correct
27 Correct 502 ms 44000 KB Output is correct
28 Correct 650 ms 51000 KB Output is correct
29 Correct 861 ms 59692 KB Output is correct
30 Correct 1096 ms 66616 KB Output is correct
31 Correct 1180 ms 73568 KB Output is correct
32 Correct 1193 ms 79612 KB Output is correct
33 Correct 1199 ms 91940 KB Output is correct
34 Correct 18 ms 15376 KB Output is correct
35 Correct 27 ms 16124 KB Output is correct
36 Correct 514 ms 45636 KB Output is correct
37 Correct 806 ms 62236 KB Output is correct
38 Correct 1157 ms 77156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 14284 KB Output is correct
2 Correct 9 ms 14412 KB Output is correct
3 Correct 9 ms 14412 KB Output is correct
4 Correct 9 ms 14412 KB Output is correct
5 Correct 9 ms 14444 KB Output is correct
6 Correct 10 ms 14424 KB Output is correct
7 Correct 10 ms 14412 KB Output is correct
8 Correct 10 ms 14412 KB Output is correct
9 Correct 435 ms 53144 KB Output is correct
10 Correct 36 ms 18308 KB Output is correct
11 Correct 124 ms 35320 KB Output is correct
12 Correct 35 ms 20200 KB Output is correct
13 Correct 64 ms 24328 KB Output is correct
14 Correct 10 ms 14576 KB Output is correct
15 Correct 14 ms 14796 KB Output is correct
16 Correct 485 ms 52500 KB Output is correct
17 Correct 10 ms 14412 KB Output is correct
18 Correct 9 ms 14420 KB Output is correct
19 Correct 9 ms 14412 KB Output is correct
20 Correct 10 ms 14412 KB Output is correct
21 Correct 10 ms 14412 KB Output is correct
22 Correct 10 ms 14412 KB Output is correct
23 Correct 1054 ms 73972 KB Output is correct
24 Correct 10 ms 14412 KB Output is correct
25 Correct 12 ms 14796 KB Output is correct
26 Correct 15 ms 14924 KB Output is correct
27 Correct 14 ms 15180 KB Output is correct
28 Correct 370 ms 38604 KB Output is correct
29 Correct 592 ms 49812 KB Output is correct
30 Correct 833 ms 62808 KB Output is correct
31 Correct 1075 ms 73828 KB Output is correct
32 Correct 10 ms 14412 KB Output is correct
33 Correct 9 ms 14412 KB Output is correct
34 Correct 9 ms 14412 KB Output is correct
35 Correct 10 ms 14412 KB Output is correct
36 Correct 9 ms 14412 KB Output is correct
37 Correct 9 ms 14412 KB Output is correct
38 Correct 9 ms 14412 KB Output is correct
39 Correct 10 ms 14376 KB Output is correct
40 Correct 9 ms 14412 KB Output is correct
41 Correct 12 ms 14424 KB Output is correct
42 Correct 10 ms 14412 KB Output is correct
43 Correct 13 ms 14796 KB Output is correct
44 Correct 17 ms 14924 KB Output is correct
45 Correct 459 ms 46096 KB Output is correct
46 Correct 783 ms 61308 KB Output is correct
47 Correct 878 ms 61268 KB Output is correct
48 Correct 9 ms 14412 KB Output is correct
49 Correct 11 ms 14412 KB Output is correct
50 Correct 10 ms 14420 KB Output is correct
51 Correct 10 ms 14412 KB Output is correct
52 Correct 9 ms 14412 KB Output is correct
53 Correct 9 ms 14392 KB Output is correct
54 Correct 10 ms 14412 KB Output is correct
55 Incorrect 937 ms 58480 KB Solution announced impossible, but it is possible.
56 Halted 0 ms 0 KB -