Submission #568329

# Submission time Handle Problem Language Result Execution time Memory
568329 2022-05-25T08:36:35 Z Stickfish Swapping Cities (APIO20_swap) C++17
100 / 100
264 ms 37832 KB
#include "swap.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
using namespace std;

const int MAXN = 3e5 + 123;
const int INF = 1e9 + 177013;
vector<int> edg[MAXN];
int depth[MAXN];
int timer = 0;
int rt[MAXN];
int crt[MAXN];
int vcnt = 0;
int n = 0;
int deg[MAXN];
bitset<MAXN> swp;

void dfs(int v) {
    //tin[v] = timer++;
    if (depth[rt[v]] - depth[crt[rt[v]]] == depth[crt[rt[v]]] - depth[crt[crt[rt[v]]]]) {
        crt[v] = crt[crt[rt[v]]];
    } else {
        crt[v] = rt[v];
    }
    for (auto u : edg[v]) {
        if (u == rt[v])
            continue;
        depth[u] = depth[v] + 1;
        dfs(u);
    }
    //tout[v] = timer;
}

int gst(int i) {
    if (rt[i] == i)
        return i;
    return rt[i] = gst(rt[i]);
}

void unite(int i, int j) {
    swp[vcnt] = (++deg[i] >= 3) | (++deg[j] >= 3);
    i = gst(i);
    j = gst(j);
    edg[vcnt].push_back(i);
    rt[i] = vcnt;
    if (i != j) {
        swp[vcnt] = swp[vcnt] | swp[j] | swp[i];
        edg[vcnt].push_back(j);
        rt[j] = vcnt;
    } else {
        swp[vcnt] = 1;
    }
    ++vcnt;
}

int get_up(int v, int d) {
    int ans = 0;
    while (depth[v] > d) {
        if (depth[crt[v]] >= d) {
            ans = max(ans, crt[v]);
            v = crt[v];
        } else {
            ans = max(ans, rt[v]);
            v = rt[v];
        }
    }
    return v;
}

int lca(int v, int u) {
    pair<int, int> ans(0, 0);
    if (depth[u] > depth[v]) {
        u = get_up(u, depth[v]);
    }
    if (depth[u] < depth[v]) {
        v = get_up(v, depth[u]);
    }
    while (v != u) {
        if (crt[v] == crt[u]) {
            v = rt[v];
            u = rt[u];
        } else {
            v = crt[v];
            u = crt[u];
        }
    }
    return v;
}

vector<int> cmprw;

void init(int N, int m, vector<int> U, vector<int> V, vector<int> W) {
    n = N;
    vector<pair<int, pair<int, int>>> edg_weight(m);
    for (int i = 0; i < m; ++i) {
        edg_weight[i] = {W[i], {U[i], V[i]}};
        cmprw.push_back(W[i]);
    }
    sort(edg_weight.begin(), edg_weight.end());
    sort(cmprw.begin(), cmprw.end());
    for (int i = 0; i < n + m + 8; ++i)
        rt[i] = i;
    vcnt = n;
    for (auto [w, e] : edg_weight) {
        unite(e.first, e.second);
    }
    for (int i = 0; i < vcnt; ++i) {
        for (auto u : edg[i])
            rt[u] = i;
    }
    for (int i = 0; i < vcnt; ++i) {
        if (rt[i] == i) {
            crt[i] = i;
            dfs(vcnt - 1);
        }
    }
    //for (int i = 0; i < vcnt; ++i) {
        //cout << rt[i] << ' ' << crt[i] << ' ' << swp[i] << endl;
    //}
}

int getMinimumFuelCapacity(int X, int Y) {
    int t = lca(X, Y);
    while (!swp[t]) {
        if (rt[t] == t)
            return -1;
        if (swp[crt[t]])
            t = rt[t];
        else
            t = crt[t];
    }
    return cmprw[t - n];
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7352 KB Output is correct
3 Correct 4 ms 7360 KB Output is correct
4 Correct 6 ms 7328 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 6 ms 7464 KB Output is correct
8 Correct 5 ms 7508 KB Output is correct
9 Correct 60 ms 16732 KB Output is correct
10 Correct 74 ms 18704 KB Output is correct
11 Correct 75 ms 18640 KB Output is correct
12 Correct 87 ms 19304 KB Output is correct
13 Correct 85 ms 21620 KB Output is correct
14 Correct 75 ms 16960 KB Output is correct
15 Correct 148 ms 22852 KB Output is correct
16 Correct 162 ms 22392 KB Output is correct
17 Correct 177 ms 23152 KB Output is correct
18 Correct 198 ms 25440 KB Output is correct
19 Correct 94 ms 14128 KB Output is correct
20 Correct 151 ms 22976 KB Output is correct
21 Correct 176 ms 22824 KB Output is correct
22 Correct 173 ms 23564 KB Output is correct
23 Correct 204 ms 25860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7352 KB Output is correct
3 Correct 215 ms 26724 KB Output is correct
4 Correct 172 ms 27304 KB Output is correct
5 Correct 175 ms 27276 KB Output is correct
6 Correct 180 ms 27164 KB Output is correct
7 Correct 177 ms 27548 KB Output is correct
8 Correct 173 ms 26792 KB Output is correct
9 Correct 180 ms 27084 KB Output is correct
10 Correct 173 ms 26696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7352 KB Output is correct
3 Correct 4 ms 7360 KB Output is correct
4 Correct 6 ms 7328 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 6 ms 7464 KB Output is correct
8 Correct 5 ms 7508 KB Output is correct
9 Correct 5 ms 7380 KB Output is correct
10 Correct 5 ms 7496 KB Output is correct
11 Correct 7 ms 7380 KB Output is correct
12 Correct 5 ms 7368 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 4 ms 7368 KB Output is correct
15 Correct 4 ms 7492 KB Output is correct
16 Correct 5 ms 7372 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 5 ms 7380 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 4 ms 7380 KB Output is correct
21 Correct 5 ms 7480 KB Output is correct
22 Correct 5 ms 7620 KB Output is correct
23 Correct 5 ms 7380 KB Output is correct
24 Correct 5 ms 7604 KB Output is correct
25 Correct 6 ms 7536 KB Output is correct
26 Correct 5 ms 7508 KB Output is correct
27 Correct 6 ms 7496 KB Output is correct
28 Correct 6 ms 7636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7352 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 6 ms 7328 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 6 ms 7464 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 60 ms 16732 KB Output is correct
11 Correct 74 ms 18704 KB Output is correct
12 Correct 75 ms 18640 KB Output is correct
13 Correct 87 ms 19304 KB Output is correct
14 Correct 85 ms 21620 KB Output is correct
15 Correct 5 ms 7496 KB Output is correct
16 Correct 7 ms 7380 KB Output is correct
17 Correct 5 ms 7368 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 4 ms 7368 KB Output is correct
20 Correct 4 ms 7492 KB Output is correct
21 Correct 5 ms 7372 KB Output is correct
22 Correct 5 ms 7508 KB Output is correct
23 Correct 5 ms 7380 KB Output is correct
24 Correct 5 ms 7508 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 5 ms 7480 KB Output is correct
27 Correct 5 ms 7620 KB Output is correct
28 Correct 5 ms 7380 KB Output is correct
29 Correct 5 ms 7604 KB Output is correct
30 Correct 6 ms 7536 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 6 ms 7496 KB Output is correct
33 Correct 6 ms 7636 KB Output is correct
34 Correct 15 ms 8916 KB Output is correct
35 Correct 84 ms 19136 KB Output is correct
36 Correct 83 ms 18864 KB Output is correct
37 Correct 94 ms 18996 KB Output is correct
38 Correct 91 ms 18844 KB Output is correct
39 Correct 77 ms 18732 KB Output is correct
40 Correct 83 ms 17860 KB Output is correct
41 Correct 89 ms 19200 KB Output is correct
42 Correct 110 ms 19008 KB Output is correct
43 Correct 74 ms 22060 KB Output is correct
44 Correct 81 ms 19256 KB Output is correct
45 Correct 124 ms 28160 KB Output is correct
46 Correct 101 ms 19008 KB Output is correct
47 Correct 83 ms 19000 KB Output is correct
48 Correct 103 ms 21872 KB Output is correct
49 Correct 95 ms 30432 KB Output is correct
50 Correct 80 ms 25140 KB Output is correct
51 Correct 108 ms 25604 KB Output is correct
52 Correct 138 ms 28416 KB Output is correct
53 Correct 151 ms 30140 KB Output is correct
54 Correct 175 ms 33992 KB Output is correct
55 Correct 77 ms 21732 KB Output is correct
56 Correct 140 ms 31408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7352 KB Output is correct
3 Correct 4 ms 7360 KB Output is correct
4 Correct 6 ms 7328 KB Output is correct
5 Correct 5 ms 7372 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 6 ms 7464 KB Output is correct
8 Correct 5 ms 7508 KB Output is correct
9 Correct 60 ms 16732 KB Output is correct
10 Correct 74 ms 18704 KB Output is correct
11 Correct 75 ms 18640 KB Output is correct
12 Correct 87 ms 19304 KB Output is correct
13 Correct 85 ms 21620 KB Output is correct
14 Correct 75 ms 16960 KB Output is correct
15 Correct 148 ms 22852 KB Output is correct
16 Correct 162 ms 22392 KB Output is correct
17 Correct 177 ms 23152 KB Output is correct
18 Correct 198 ms 25440 KB Output is correct
19 Correct 215 ms 26724 KB Output is correct
20 Correct 172 ms 27304 KB Output is correct
21 Correct 175 ms 27276 KB Output is correct
22 Correct 180 ms 27164 KB Output is correct
23 Correct 177 ms 27548 KB Output is correct
24 Correct 173 ms 26792 KB Output is correct
25 Correct 180 ms 27084 KB Output is correct
26 Correct 173 ms 26696 KB Output is correct
27 Correct 5 ms 7496 KB Output is correct
28 Correct 7 ms 7380 KB Output is correct
29 Correct 5 ms 7368 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 4 ms 7368 KB Output is correct
32 Correct 4 ms 7492 KB Output is correct
33 Correct 5 ms 7372 KB Output is correct
34 Correct 5 ms 7508 KB Output is correct
35 Correct 5 ms 7380 KB Output is correct
36 Correct 15 ms 8916 KB Output is correct
37 Correct 84 ms 19136 KB Output is correct
38 Correct 83 ms 18864 KB Output is correct
39 Correct 94 ms 18996 KB Output is correct
40 Correct 91 ms 18844 KB Output is correct
41 Correct 77 ms 18732 KB Output is correct
42 Correct 83 ms 17860 KB Output is correct
43 Correct 89 ms 19200 KB Output is correct
44 Correct 110 ms 19008 KB Output is correct
45 Correct 74 ms 22060 KB Output is correct
46 Correct 81 ms 19256 KB Output is correct
47 Correct 20 ms 9252 KB Output is correct
48 Correct 190 ms 22872 KB Output is correct
49 Correct 159 ms 22836 KB Output is correct
50 Correct 153 ms 22724 KB Output is correct
51 Correct 206 ms 22732 KB Output is correct
52 Correct 151 ms 22088 KB Output is correct
53 Correct 145 ms 20260 KB Output is correct
54 Correct 184 ms 23560 KB Output is correct
55 Correct 148 ms 22868 KB Output is correct
56 Correct 224 ms 25276 KB Output is correct
57 Correct 178 ms 23688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 4 ms 7352 KB Output is correct
4 Correct 4 ms 7360 KB Output is correct
5 Correct 6 ms 7328 KB Output is correct
6 Correct 5 ms 7372 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 6 ms 7464 KB Output is correct
9 Correct 5 ms 7508 KB Output is correct
10 Correct 60 ms 16732 KB Output is correct
11 Correct 74 ms 18704 KB Output is correct
12 Correct 75 ms 18640 KB Output is correct
13 Correct 87 ms 19304 KB Output is correct
14 Correct 85 ms 21620 KB Output is correct
15 Correct 75 ms 16960 KB Output is correct
16 Correct 148 ms 22852 KB Output is correct
17 Correct 162 ms 22392 KB Output is correct
18 Correct 177 ms 23152 KB Output is correct
19 Correct 198 ms 25440 KB Output is correct
20 Correct 215 ms 26724 KB Output is correct
21 Correct 172 ms 27304 KB Output is correct
22 Correct 175 ms 27276 KB Output is correct
23 Correct 180 ms 27164 KB Output is correct
24 Correct 177 ms 27548 KB Output is correct
25 Correct 173 ms 26792 KB Output is correct
26 Correct 180 ms 27084 KB Output is correct
27 Correct 173 ms 26696 KB Output is correct
28 Correct 5 ms 7496 KB Output is correct
29 Correct 7 ms 7380 KB Output is correct
30 Correct 5 ms 7368 KB Output is correct
31 Correct 4 ms 7380 KB Output is correct
32 Correct 4 ms 7368 KB Output is correct
33 Correct 4 ms 7492 KB Output is correct
34 Correct 5 ms 7372 KB Output is correct
35 Correct 5 ms 7508 KB Output is correct
36 Correct 5 ms 7380 KB Output is correct
37 Correct 15 ms 8916 KB Output is correct
38 Correct 84 ms 19136 KB Output is correct
39 Correct 83 ms 18864 KB Output is correct
40 Correct 94 ms 18996 KB Output is correct
41 Correct 91 ms 18844 KB Output is correct
42 Correct 77 ms 18732 KB Output is correct
43 Correct 83 ms 17860 KB Output is correct
44 Correct 89 ms 19200 KB Output is correct
45 Correct 110 ms 19008 KB Output is correct
46 Correct 74 ms 22060 KB Output is correct
47 Correct 81 ms 19256 KB Output is correct
48 Correct 20 ms 9252 KB Output is correct
49 Correct 190 ms 22872 KB Output is correct
50 Correct 159 ms 22836 KB Output is correct
51 Correct 153 ms 22724 KB Output is correct
52 Correct 206 ms 22732 KB Output is correct
53 Correct 151 ms 22088 KB Output is correct
54 Correct 145 ms 20260 KB Output is correct
55 Correct 184 ms 23560 KB Output is correct
56 Correct 148 ms 22868 KB Output is correct
57 Correct 224 ms 25276 KB Output is correct
58 Correct 178 ms 23688 KB Output is correct
59 Correct 94 ms 14128 KB Output is correct
60 Correct 151 ms 22976 KB Output is correct
61 Correct 176 ms 22824 KB Output is correct
62 Correct 173 ms 23564 KB Output is correct
63 Correct 204 ms 25860 KB Output is correct
64 Correct 5 ms 7508 KB Output is correct
65 Correct 4 ms 7380 KB Output is correct
66 Correct 5 ms 7480 KB Output is correct
67 Correct 5 ms 7620 KB Output is correct
68 Correct 5 ms 7380 KB Output is correct
69 Correct 5 ms 7604 KB Output is correct
70 Correct 6 ms 7536 KB Output is correct
71 Correct 5 ms 7508 KB Output is correct
72 Correct 6 ms 7496 KB Output is correct
73 Correct 6 ms 7636 KB Output is correct
74 Correct 124 ms 28160 KB Output is correct
75 Correct 101 ms 19008 KB Output is correct
76 Correct 83 ms 19000 KB Output is correct
77 Correct 103 ms 21872 KB Output is correct
78 Correct 95 ms 30432 KB Output is correct
79 Correct 80 ms 25140 KB Output is correct
80 Correct 108 ms 25604 KB Output is correct
81 Correct 138 ms 28416 KB Output is correct
82 Correct 151 ms 30140 KB Output is correct
83 Correct 175 ms 33992 KB Output is correct
84 Correct 77 ms 21732 KB Output is correct
85 Correct 140 ms 31408 KB Output is correct
86 Correct 64 ms 15176 KB Output is correct
87 Correct 185 ms 22788 KB Output is correct
88 Correct 184 ms 22872 KB Output is correct
89 Correct 216 ms 25268 KB Output is correct
90 Correct 177 ms 35132 KB Output is correct
91 Correct 180 ms 32764 KB Output is correct
92 Correct 226 ms 28472 KB Output is correct
93 Correct 215 ms 31964 KB Output is correct
94 Correct 264 ms 34008 KB Output is correct
95 Correct 237 ms 37832 KB Output is correct
96 Correct 193 ms 25408 KB Output is correct
97 Correct 232 ms 29668 KB Output is correct