Submission #982531

# Submission time Handle Problem Language Result Execution time Memory
982531 2024-05-14T10:57:58 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
253 ms 53360 KB
#include <bits/stdc++.h>
#include "swap.h"

using namespace std;
using ll = long long;

const int N = 300300;

const int INF = 1e9;

struct edge {
    int u;
    int v;
    int w;
};  

int bg[N];
int en[N];
int cap[N];
bool isbam[N];
int head[N];
int par[N][18];


vector<edge> edges;

int find(int x) {
    return head[x] = (head[x] == x ? x : find(head[x]));
};

vector<int> g[N];
int tin[N], tout[N], T;

void dfs(int s) {
    tin[s] = ++T;
    for (int to : g[s]) {
        dfs(to);
    }
    tout[s] = T;
}

bool up(int u, int v) {
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

void init(int N, int M,
          vector<int> U, vector<int> V, vector<int> W) {
    for (int i = 0; i < M; i++) {
        edges.push_back({U[i], V[i], W[i]});
    }
    
    sort(edges.begin(), edges.end(), [](edge x, edge y) {
        return x.w < y.w;
    });

    int lst = N - 1;
    for (int i = 0; i < N; i++) {
        bg[i] = en[i] = i;
        head[i] = i;
        isbam[i] = true;
    }
    for (auto e : edges) {
        lst++;
        int t1 = find(e.u);
        int t2 = find(e.v);

        head[lst] = par[lst][0] = lst;
        par[t1][0] = par[t2][0] = lst;
        head[t1] = head[t2] = lst;
        cap[lst] = e.w;

        if (t1 == t2) {
            bg[lst] = bg[t1];
            en[lst] = en[t1];
            isbam[lst] = false;
        } else {
            int ar[2][2] = {{bg[t1], en[t1]}, {bg[t2], en[t2]}}; 
            
            bool flag = true; 
            for (int bit_0 = 0; bit_0 < 2 && flag; bit_0++) {
                for (int bit_1 = 0; bit_1 < 2; bit_1++) {
                    if (e.u == ar[0][bit_0] && e.v == ar[1][bit_1]) {
                        bg[lst] = ar[0][!bit_0];
                        en[lst] = ar[1][!bit_1];
                        flag = false;
                        break;
                    }
                    if (e.v == ar[0][bit_0] && e.u == ar[1][bit_1]) {
                        bg[lst] = ar[0][!bit_0];
                        en[lst] = ar[1][!bit_1];
                        flag = false;
                        break;
                    }
                } 
            }
            if (flag) 
                isbam[lst] = false;
            else 
                isbam[lst] = (isbam[t1] & isbam[t2]);
        }
    }
    par[lst][0] = lst;
    for (int i = lst; i >= 0; i--) {
        for (int k = 1; k < 18; k++) {
            par[i][k] = par[ par[i][k - 1] ][k - 1];
        }
        if (par[i][0] != i)
            g[par[i][0]].push_back(i);
    }

    dfs(lst);
}

int getMinimumFuelCapacity(int X, int Y) {
    for (int i = 17; i >= 0; i--) {
        if (!up(par[X][i], Y) || isbam[par[X][i]])
            X = par[X][i];
    }
    if (up(par[X][0], Y) && !isbam[par[X][0]]) {
        X = par[X][0];
        return cap[X];
    }
    return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16828 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 60 ms 32480 KB Output is correct
10 Correct 76 ms 37824 KB Output is correct
11 Correct 70 ms 35488 KB Output is correct
12 Correct 82 ms 37916 KB Output is correct
13 Correct 81 ms 39368 KB Output is correct
14 Correct 68 ms 32456 KB Output is correct
15 Correct 155 ms 39520 KB Output is correct
16 Correct 161 ms 37452 KB Output is correct
17 Correct 148 ms 39984 KB Output is correct
18 Correct 153 ms 41148 KB Output is correct
19 Correct 73 ms 23428 KB Output is correct
20 Correct 185 ms 40772 KB Output is correct
21 Correct 146 ms 38736 KB Output is correct
22 Correct 159 ms 41264 KB Output is correct
23 Correct 171 ms 42564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 177 ms 41080 KB Output is correct
4 Correct 188 ms 43576 KB Output is correct
5 Correct 177 ms 43392 KB Output is correct
6 Correct 181 ms 43468 KB Output is correct
7 Correct 203 ms 43592 KB Output is correct
8 Correct 167 ms 41080 KB Output is correct
9 Correct 192 ms 43336 KB Output is correct
10 Correct 169 ms 40980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16828 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 3 ms 16932 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 16728 KB Output is correct
12 Correct 3 ms 16732 KB Output is correct
13 Correct 3 ms 16732 KB Output is correct
14 Correct 3 ms 16732 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16732 KB Output is correct
17 Correct 3 ms 16832 KB Output is correct
18 Correct 4 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 4 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16988 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 4 ms 16988 KB Output is correct
25 Correct 4 ms 16988 KB Output is correct
26 Correct 4 ms 17032 KB Output is correct
27 Correct 3 ms 16728 KB Output is correct
28 Correct 4 ms 16984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16932 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 4 ms 16828 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 60 ms 32480 KB Output is correct
11 Correct 76 ms 37824 KB Output is correct
12 Correct 70 ms 35488 KB Output is correct
13 Correct 82 ms 37916 KB Output is correct
14 Correct 81 ms 39368 KB Output is correct
15 Correct 3 ms 16732 KB Output is correct
16 Correct 3 ms 16728 KB Output is correct
17 Correct 3 ms 16732 KB Output is correct
18 Correct 3 ms 16732 KB Output is correct
19 Correct 3 ms 16732 KB Output is correct
20 Correct 3 ms 16732 KB Output is correct
21 Correct 3 ms 16732 KB Output is correct
22 Correct 3 ms 16832 KB Output is correct
23 Correct 4 ms 16732 KB Output is correct
24 Correct 3 ms 16732 KB Output is correct
25 Correct 4 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 3 ms 16988 KB Output is correct
28 Correct 3 ms 16732 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 4 ms 17032 KB Output is correct
32 Correct 3 ms 16728 KB Output is correct
33 Correct 4 ms 16984 KB Output is correct
34 Correct 12 ms 19740 KB Output is correct
35 Correct 78 ms 38108 KB Output is correct
36 Correct 76 ms 38088 KB Output is correct
37 Correct 77 ms 38148 KB Output is correct
38 Correct 87 ms 37836 KB Output is correct
39 Correct 79 ms 37776 KB Output is correct
40 Correct 75 ms 35268 KB Output is correct
41 Correct 86 ms 38096 KB Output is correct
42 Correct 75 ms 38080 KB Output is correct
43 Correct 77 ms 39708 KB Output is correct
44 Correct 102 ms 38084 KB Output is correct
45 Correct 126 ms 45724 KB Output is correct
46 Correct 77 ms 38084 KB Output is correct
47 Correct 96 ms 38088 KB Output is correct
48 Correct 101 ms 39876 KB Output is correct
49 Correct 88 ms 41740 KB Output is correct
50 Correct 63 ms 36036 KB Output is correct
51 Correct 101 ms 41976 KB Output is correct
52 Correct 123 ms 48016 KB Output is correct
53 Correct 198 ms 49248 KB Output is correct
54 Correct 133 ms 51904 KB Output is correct
55 Correct 91 ms 39584 KB Output is correct
56 Correct 161 ms 50280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16728 KB Output is correct
2 Correct 3 ms 16732 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 4 ms 16828 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 60 ms 32480 KB Output is correct
10 Correct 76 ms 37824 KB Output is correct
11 Correct 70 ms 35488 KB Output is correct
12 Correct 82 ms 37916 KB Output is correct
13 Correct 81 ms 39368 KB Output is correct
14 Correct 68 ms 32456 KB Output is correct
15 Correct 155 ms 39520 KB Output is correct
16 Correct 161 ms 37452 KB Output is correct
17 Correct 148 ms 39984 KB Output is correct
18 Correct 153 ms 41148 KB Output is correct
19 Correct 177 ms 41080 KB Output is correct
20 Correct 188 ms 43576 KB Output is correct
21 Correct 177 ms 43392 KB Output is correct
22 Correct 181 ms 43468 KB Output is correct
23 Correct 203 ms 43592 KB Output is correct
24 Correct 167 ms 41080 KB Output is correct
25 Correct 192 ms 43336 KB Output is correct
26 Correct 169 ms 40980 KB Output is correct
27 Correct 3 ms 16732 KB Output is correct
28 Correct 3 ms 16728 KB Output is correct
29 Correct 3 ms 16732 KB Output is correct
30 Correct 3 ms 16732 KB Output is correct
31 Correct 3 ms 16732 KB Output is correct
32 Correct 3 ms 16732 KB Output is correct
33 Correct 3 ms 16732 KB Output is correct
34 Correct 3 ms 16832 KB Output is correct
35 Correct 4 ms 16732 KB Output is correct
36 Correct 12 ms 19740 KB Output is correct
37 Correct 78 ms 38108 KB Output is correct
38 Correct 76 ms 38088 KB Output is correct
39 Correct 77 ms 38148 KB Output is correct
40 Correct 87 ms 37836 KB Output is correct
41 Correct 79 ms 37776 KB Output is correct
42 Correct 75 ms 35268 KB Output is correct
43 Correct 86 ms 38096 KB Output is correct
44 Correct 75 ms 38080 KB Output is correct
45 Correct 77 ms 39708 KB Output is correct
46 Correct 102 ms 38084 KB Output is correct
47 Correct 18 ms 19996 KB Output is correct
48 Correct 156 ms 40496 KB Output is correct
49 Correct 155 ms 40748 KB Output is correct
50 Correct 157 ms 40584 KB Output is correct
51 Correct 155 ms 40596 KB Output is correct
52 Correct 155 ms 38468 KB Output is correct
53 Correct 132 ms 35152 KB Output is correct
54 Correct 152 ms 41264 KB Output is correct
55 Correct 152 ms 40752 KB Output is correct
56 Correct 188 ms 41956 KB Output is correct
57 Correct 167 ms 41300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16932 KB Output is correct
2 Correct 3 ms 16728 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 3 ms 16732 KB Output is correct
8 Correct 4 ms 16828 KB Output is correct
9 Correct 3 ms 16732 KB Output is correct
10 Correct 60 ms 32480 KB Output is correct
11 Correct 76 ms 37824 KB Output is correct
12 Correct 70 ms 35488 KB Output is correct
13 Correct 82 ms 37916 KB Output is correct
14 Correct 81 ms 39368 KB Output is correct
15 Correct 68 ms 32456 KB Output is correct
16 Correct 155 ms 39520 KB Output is correct
17 Correct 161 ms 37452 KB Output is correct
18 Correct 148 ms 39984 KB Output is correct
19 Correct 153 ms 41148 KB Output is correct
20 Correct 177 ms 41080 KB Output is correct
21 Correct 188 ms 43576 KB Output is correct
22 Correct 177 ms 43392 KB Output is correct
23 Correct 181 ms 43468 KB Output is correct
24 Correct 203 ms 43592 KB Output is correct
25 Correct 167 ms 41080 KB Output is correct
26 Correct 192 ms 43336 KB Output is correct
27 Correct 169 ms 40980 KB Output is correct
28 Correct 3 ms 16732 KB Output is correct
29 Correct 3 ms 16728 KB Output is correct
30 Correct 3 ms 16732 KB Output is correct
31 Correct 3 ms 16732 KB Output is correct
32 Correct 3 ms 16732 KB Output is correct
33 Correct 3 ms 16732 KB Output is correct
34 Correct 3 ms 16732 KB Output is correct
35 Correct 3 ms 16832 KB Output is correct
36 Correct 4 ms 16732 KB Output is correct
37 Correct 12 ms 19740 KB Output is correct
38 Correct 78 ms 38108 KB Output is correct
39 Correct 76 ms 38088 KB Output is correct
40 Correct 77 ms 38148 KB Output is correct
41 Correct 87 ms 37836 KB Output is correct
42 Correct 79 ms 37776 KB Output is correct
43 Correct 75 ms 35268 KB Output is correct
44 Correct 86 ms 38096 KB Output is correct
45 Correct 75 ms 38080 KB Output is correct
46 Correct 77 ms 39708 KB Output is correct
47 Correct 102 ms 38084 KB Output is correct
48 Correct 18 ms 19996 KB Output is correct
49 Correct 156 ms 40496 KB Output is correct
50 Correct 155 ms 40748 KB Output is correct
51 Correct 157 ms 40584 KB Output is correct
52 Correct 155 ms 40596 KB Output is correct
53 Correct 155 ms 38468 KB Output is correct
54 Correct 132 ms 35152 KB Output is correct
55 Correct 152 ms 41264 KB Output is correct
56 Correct 152 ms 40752 KB Output is correct
57 Correct 188 ms 41956 KB Output is correct
58 Correct 167 ms 41300 KB Output is correct
59 Correct 73 ms 23428 KB Output is correct
60 Correct 185 ms 40772 KB Output is correct
61 Correct 146 ms 38736 KB Output is correct
62 Correct 159 ms 41264 KB Output is correct
63 Correct 171 ms 42564 KB Output is correct
64 Correct 3 ms 16732 KB Output is correct
65 Correct 4 ms 16732 KB Output is correct
66 Correct 3 ms 16732 KB Output is correct
67 Correct 3 ms 16988 KB Output is correct
68 Correct 3 ms 16732 KB Output is correct
69 Correct 4 ms 16988 KB Output is correct
70 Correct 4 ms 16988 KB Output is correct
71 Correct 4 ms 17032 KB Output is correct
72 Correct 3 ms 16728 KB Output is correct
73 Correct 4 ms 16984 KB Output is correct
74 Correct 126 ms 45724 KB Output is correct
75 Correct 77 ms 38084 KB Output is correct
76 Correct 96 ms 38088 KB Output is correct
77 Correct 101 ms 39876 KB Output is correct
78 Correct 88 ms 41740 KB Output is correct
79 Correct 63 ms 36036 KB Output is correct
80 Correct 101 ms 41976 KB Output is correct
81 Correct 123 ms 48016 KB Output is correct
82 Correct 198 ms 49248 KB Output is correct
83 Correct 133 ms 51904 KB Output is correct
84 Correct 91 ms 39584 KB Output is correct
85 Correct 161 ms 50280 KB Output is correct
86 Correct 64 ms 27684 KB Output is correct
87 Correct 175 ms 40492 KB Output is correct
88 Correct 154 ms 40552 KB Output is correct
89 Correct 201 ms 42216 KB Output is correct
90 Correct 160 ms 46612 KB Output is correct
91 Correct 166 ms 44344 KB Output is correct
92 Correct 200 ms 42244 KB Output is correct
93 Correct 199 ms 50784 KB Output is correct
94 Correct 253 ms 51512 KB Output is correct
95 Correct 208 ms 53360 KB Output is correct
96 Correct 186 ms 42068 KB Output is correct
97 Correct 251 ms 47068 KB Output is correct