Submission #367768

# Submission time Handle Problem Language Result Execution time Memory
367768 2021-02-18T09:41:55 Z Sara Swapping Cities (APIO20_swap) C++14
100 / 100
481 ms 50728 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define ll long long
#define F first
#define S second
#define pb push_back

const int O = 3e5 + 5;
const int LOG = 25;
const int MOD = 1e9 + 7;
const ll  inf = 1e9 + 5;

int n, m, q, par[O];
int deg[O];
int mn1[O], mn2[O];
int h[O], pr[LOG][O];
vector <int> g[O];

struct edge{
    int u = 0, v = 0, w = 0, id = 0;
} e[O];

bool cmp(edge e1, edge e2){
    return make_pair(e1.w, e1.id) < make_pair(e2.w, e2.id);
}

void clear(){
    for (int v = 1; v < O; v ++){
        par[v] = v;
        mn1[v] = mn2[v] = inf;
    }
    return;
}

int find_par(int v){
    if (par[v] == v) return v;
    return par[v] = find_par(par[v]);
}

void merge(int u, int v, int w){
    deg[u] ++; deg[v] ++;
    int pu = find_par(u);
    int pv = find_par(v);
    if (pu == pv){
        mn2[pv] = min(mn2[pv], w);
    }
    else {
        n ++;
        mn1[n] = w;
        mn2[n] = min(mn2[pu], mn2[pv]);
        par[pu] = par[pv] = n;
        g[n].pb(pu); g[n].pb(pv);
        pr[0][pu] = pr[0][pv] = n;
        if (3 <= deg[u] || 3 <= deg[v]){
            mn2[n] = min(mn2[n], w);
        }
    }
    return;
}

void dfs(int v){
    for (int u : g[v]){
        h[u] = h[v] + 1;
        dfs(u);
    }
    return;
}

void build(){
    for (int i = 1; i < LOG; i ++){
        for (int v = 1; v <= n; v ++){
            pr[i][v] = pr[i - 1][pr[i - 1][v]];
        }
    }
    return;
}

int get_par(int v, int dif){
    for (int i = 0; i < LOG; i ++){
        if ((dif >> i) & 1){
            v = pr[i][v];
        }
    }
    return v;
}

int LCA(int u, int v){
    if (h[v] < h[u]) swap(u, v);
    v = get_par(v, h[v] - h[u]);
    if (u == v) return v;
    for (int i = LOG - 1; i >= 0; i --){
        if (pr[i][u] != pr[i][v]){
            u = pr[i][u];
            v = pr[i][v];
        }
    }
    v = pr[0][v];
    return v;
}

int getMinimumFuelCapacity(int X, int Y){
    X ++; Y ++;
    int lca = LCA(X, Y);
    if (mn2[lca] != inf){
        return max(mn1[lca], mn2[lca]);
    }
    for (int i = LOG - 1; i >= 0; i --){
        int up = pr[i][lca];
        if (up && mn2[up] == inf){
            lca = up;
        }
    }
    lca = pr[0][lca];
    if (lca == 0) return -1;
    return max(mn1[lca], mn2[lca]);
}

void init(int N, int M, vector <int> U, vector <int> V, vector <int> W){
    n = N; m = M;
    for (int i = 0; i < m; i ++){
        e[i].u = U[i] + 1;
        e[i].v = V[i] + 1;
        e[i].w = W[i];
        e[i].id = i;
    }
    sort(e, e + M, cmp);
    clear();
    for (int i = 0; i < m; i ++){
        merge(e[i].u, e[i].v, e[i].w);
    }    
    dfs(n); build();
    return;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11116 KB Output is correct
2 Correct 8 ms 11116 KB Output is correct
3 Correct 8 ms 11116 KB Output is correct
4 Correct 8 ms 11116 KB Output is correct
5 Correct 9 ms 11372 KB Output is correct
6 Correct 9 ms 11372 KB Output is correct
7 Correct 9 ms 11500 KB Output is correct
8 Correct 10 ms 11372 KB Output is correct
9 Correct 85 ms 34540 KB Output is correct
10 Correct 107 ms 39788 KB Output is correct
11 Correct 109 ms 39276 KB Output is correct
12 Correct 113 ms 40940 KB Output is correct
13 Correct 98 ms 43244 KB Output is correct
14 Correct 114 ms 34816 KB Output is correct
15 Correct 362 ms 44148 KB Output is correct
16 Correct 348 ms 43288 KB Output is correct
17 Correct 362 ms 45008 KB Output is correct
18 Correct 481 ms 47636 KB Output is correct
19 Correct 129 ms 20844 KB Output is correct
20 Correct 341 ms 45288 KB Output is correct
21 Correct 367 ms 44452 KB Output is correct
22 Correct 360 ms 46416 KB Output is correct
23 Correct 481 ms 48848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11116 KB Output is correct
2 Correct 8 ms 11116 KB Output is correct
3 Correct 392 ms 48652 KB Output is correct
4 Correct 423 ms 50128 KB Output is correct
5 Correct 425 ms 49408 KB Output is correct
6 Correct 401 ms 50000 KB Output is correct
7 Correct 411 ms 49920 KB Output is correct
8 Correct 413 ms 48536 KB Output is correct
9 Correct 400 ms 49512 KB Output is correct
10 Correct 401 ms 48164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11116 KB Output is correct
2 Correct 8 ms 11116 KB Output is correct
3 Correct 8 ms 11116 KB Output is correct
4 Correct 8 ms 11116 KB Output is correct
5 Correct 9 ms 11372 KB Output is correct
6 Correct 9 ms 11372 KB Output is correct
7 Correct 9 ms 11500 KB Output is correct
8 Correct 10 ms 11372 KB Output is correct
9 Correct 7 ms 11116 KB Output is correct
10 Correct 10 ms 11500 KB Output is correct
11 Correct 9 ms 11372 KB Output is correct
12 Correct 9 ms 11372 KB Output is correct
13 Correct 8 ms 11244 KB Output is correct
14 Correct 8 ms 11372 KB Output is correct
15 Correct 9 ms 11500 KB Output is correct
16 Correct 11 ms 11372 KB Output is correct
17 Correct 9 ms 11372 KB Output is correct
18 Correct 9 ms 11372 KB Output is correct
19 Correct 8 ms 11244 KB Output is correct
20 Correct 8 ms 11372 KB Output is correct
21 Correct 9 ms 11372 KB Output is correct
22 Correct 9 ms 11244 KB Output is correct
23 Correct 9 ms 11244 KB Output is correct
24 Correct 9 ms 11372 KB Output is correct
25 Correct 9 ms 11372 KB Output is correct
26 Correct 9 ms 11372 KB Output is correct
27 Correct 9 ms 11372 KB Output is correct
28 Correct 10 ms 11372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11116 KB Output is correct
2 Correct 8 ms 11116 KB Output is correct
3 Correct 8 ms 11116 KB Output is correct
4 Correct 8 ms 11116 KB Output is correct
5 Correct 8 ms 11116 KB Output is correct
6 Correct 9 ms 11372 KB Output is correct
7 Correct 9 ms 11372 KB Output is correct
8 Correct 9 ms 11500 KB Output is correct
9 Correct 10 ms 11372 KB Output is correct
10 Correct 85 ms 34540 KB Output is correct
11 Correct 107 ms 39788 KB Output is correct
12 Correct 109 ms 39276 KB Output is correct
13 Correct 113 ms 40940 KB Output is correct
14 Correct 98 ms 43244 KB Output is correct
15 Correct 10 ms 11500 KB Output is correct
16 Correct 9 ms 11372 KB Output is correct
17 Correct 9 ms 11372 KB Output is correct
18 Correct 8 ms 11244 KB Output is correct
19 Correct 8 ms 11372 KB Output is correct
20 Correct 9 ms 11500 KB Output is correct
21 Correct 11 ms 11372 KB Output is correct
22 Correct 9 ms 11372 KB Output is correct
23 Correct 9 ms 11372 KB Output is correct
24 Correct 8 ms 11244 KB Output is correct
25 Correct 8 ms 11372 KB Output is correct
26 Correct 9 ms 11372 KB Output is correct
27 Correct 9 ms 11244 KB Output is correct
28 Correct 9 ms 11244 KB Output is correct
29 Correct 9 ms 11372 KB Output is correct
30 Correct 9 ms 11372 KB Output is correct
31 Correct 9 ms 11372 KB Output is correct
32 Correct 9 ms 11372 KB Output is correct
33 Correct 10 ms 11372 KB Output is correct
34 Correct 19 ms 15084 KB Output is correct
35 Correct 108 ms 40684 KB Output is correct
36 Correct 105 ms 40684 KB Output is correct
37 Correct 111 ms 40556 KB Output is correct
38 Correct 106 ms 40320 KB Output is correct
39 Correct 102 ms 40044 KB Output is correct
40 Correct 97 ms 37740 KB Output is correct
41 Correct 108 ms 40940 KB Output is correct
42 Correct 106 ms 40684 KB Output is correct
43 Correct 91 ms 43756 KB Output is correct
44 Correct 114 ms 40940 KB Output is correct
45 Correct 129 ms 39788 KB Output is correct
46 Correct 105 ms 40684 KB Output is correct
47 Correct 106 ms 40560 KB Output is correct
48 Correct 116 ms 41836 KB Output is correct
49 Correct 88 ms 20332 KB Output is correct
50 Correct 72 ms 19948 KB Output is correct
51 Correct 108 ms 34924 KB Output is correct
52 Correct 149 ms 44704 KB Output is correct
53 Correct 156 ms 46316 KB Output is correct
54 Correct 165 ms 46688 KB Output is correct
55 Correct 96 ms 43628 KB Output is correct
56 Correct 158 ms 46700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11116 KB Output is correct
2 Correct 8 ms 11116 KB Output is correct
3 Correct 8 ms 11116 KB Output is correct
4 Correct 8 ms 11116 KB Output is correct
5 Correct 9 ms 11372 KB Output is correct
6 Correct 9 ms 11372 KB Output is correct
7 Correct 9 ms 11500 KB Output is correct
8 Correct 10 ms 11372 KB Output is correct
9 Correct 85 ms 34540 KB Output is correct
10 Correct 107 ms 39788 KB Output is correct
11 Correct 109 ms 39276 KB Output is correct
12 Correct 113 ms 40940 KB Output is correct
13 Correct 98 ms 43244 KB Output is correct
14 Correct 114 ms 34816 KB Output is correct
15 Correct 362 ms 44148 KB Output is correct
16 Correct 348 ms 43288 KB Output is correct
17 Correct 362 ms 45008 KB Output is correct
18 Correct 481 ms 47636 KB Output is correct
19 Correct 392 ms 48652 KB Output is correct
20 Correct 423 ms 50128 KB Output is correct
21 Correct 425 ms 49408 KB Output is correct
22 Correct 401 ms 50000 KB Output is correct
23 Correct 411 ms 49920 KB Output is correct
24 Correct 413 ms 48536 KB Output is correct
25 Correct 400 ms 49512 KB Output is correct
26 Correct 401 ms 48164 KB Output is correct
27 Correct 10 ms 11500 KB Output is correct
28 Correct 9 ms 11372 KB Output is correct
29 Correct 9 ms 11372 KB Output is correct
30 Correct 8 ms 11244 KB Output is correct
31 Correct 8 ms 11372 KB Output is correct
32 Correct 9 ms 11500 KB Output is correct
33 Correct 11 ms 11372 KB Output is correct
34 Correct 9 ms 11372 KB Output is correct
35 Correct 9 ms 11372 KB Output is correct
36 Correct 19 ms 15084 KB Output is correct
37 Correct 108 ms 40684 KB Output is correct
38 Correct 105 ms 40684 KB Output is correct
39 Correct 111 ms 40556 KB Output is correct
40 Correct 106 ms 40320 KB Output is correct
41 Correct 102 ms 40044 KB Output is correct
42 Correct 97 ms 37740 KB Output is correct
43 Correct 108 ms 40940 KB Output is correct
44 Correct 106 ms 40684 KB Output is correct
45 Correct 91 ms 43756 KB Output is correct
46 Correct 114 ms 40940 KB Output is correct
47 Correct 30 ms 15340 KB Output is correct
48 Correct 349 ms 45488 KB Output is correct
49 Correct 338 ms 45648 KB Output is correct
50 Correct 324 ms 45604 KB Output is correct
51 Correct 330 ms 45428 KB Output is correct
52 Correct 318 ms 43672 KB Output is correct
53 Correct 281 ms 36832 KB Output is correct
54 Correct 347 ms 46416 KB Output is correct
55 Correct 358 ms 45524 KB Output is correct
56 Correct 438 ms 48140 KB Output is correct
57 Correct 349 ms 46544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11116 KB Output is correct
2 Correct 8 ms 11116 KB Output is correct
3 Correct 8 ms 11116 KB Output is correct
4 Correct 8 ms 11116 KB Output is correct
5 Correct 8 ms 11116 KB Output is correct
6 Correct 9 ms 11372 KB Output is correct
7 Correct 9 ms 11372 KB Output is correct
8 Correct 9 ms 11500 KB Output is correct
9 Correct 10 ms 11372 KB Output is correct
10 Correct 85 ms 34540 KB Output is correct
11 Correct 107 ms 39788 KB Output is correct
12 Correct 109 ms 39276 KB Output is correct
13 Correct 113 ms 40940 KB Output is correct
14 Correct 98 ms 43244 KB Output is correct
15 Correct 114 ms 34816 KB Output is correct
16 Correct 362 ms 44148 KB Output is correct
17 Correct 348 ms 43288 KB Output is correct
18 Correct 362 ms 45008 KB Output is correct
19 Correct 481 ms 47636 KB Output is correct
20 Correct 392 ms 48652 KB Output is correct
21 Correct 423 ms 50128 KB Output is correct
22 Correct 425 ms 49408 KB Output is correct
23 Correct 401 ms 50000 KB Output is correct
24 Correct 411 ms 49920 KB Output is correct
25 Correct 413 ms 48536 KB Output is correct
26 Correct 400 ms 49512 KB Output is correct
27 Correct 401 ms 48164 KB Output is correct
28 Correct 10 ms 11500 KB Output is correct
29 Correct 9 ms 11372 KB Output is correct
30 Correct 9 ms 11372 KB Output is correct
31 Correct 8 ms 11244 KB Output is correct
32 Correct 8 ms 11372 KB Output is correct
33 Correct 9 ms 11500 KB Output is correct
34 Correct 11 ms 11372 KB Output is correct
35 Correct 9 ms 11372 KB Output is correct
36 Correct 9 ms 11372 KB Output is correct
37 Correct 19 ms 15084 KB Output is correct
38 Correct 108 ms 40684 KB Output is correct
39 Correct 105 ms 40684 KB Output is correct
40 Correct 111 ms 40556 KB Output is correct
41 Correct 106 ms 40320 KB Output is correct
42 Correct 102 ms 40044 KB Output is correct
43 Correct 97 ms 37740 KB Output is correct
44 Correct 108 ms 40940 KB Output is correct
45 Correct 106 ms 40684 KB Output is correct
46 Correct 91 ms 43756 KB Output is correct
47 Correct 114 ms 40940 KB Output is correct
48 Correct 30 ms 15340 KB Output is correct
49 Correct 349 ms 45488 KB Output is correct
50 Correct 338 ms 45648 KB Output is correct
51 Correct 324 ms 45604 KB Output is correct
52 Correct 330 ms 45428 KB Output is correct
53 Correct 318 ms 43672 KB Output is correct
54 Correct 281 ms 36832 KB Output is correct
55 Correct 347 ms 46416 KB Output is correct
56 Correct 358 ms 45524 KB Output is correct
57 Correct 438 ms 48140 KB Output is correct
58 Correct 349 ms 46544 KB Output is correct
59 Correct 129 ms 20844 KB Output is correct
60 Correct 341 ms 45288 KB Output is correct
61 Correct 367 ms 44452 KB Output is correct
62 Correct 360 ms 46416 KB Output is correct
63 Correct 481 ms 48848 KB Output is correct
64 Correct 8 ms 11244 KB Output is correct
65 Correct 8 ms 11372 KB Output is correct
66 Correct 9 ms 11372 KB Output is correct
67 Correct 9 ms 11244 KB Output is correct
68 Correct 9 ms 11244 KB Output is correct
69 Correct 9 ms 11372 KB Output is correct
70 Correct 9 ms 11372 KB Output is correct
71 Correct 9 ms 11372 KB Output is correct
72 Correct 9 ms 11372 KB Output is correct
73 Correct 10 ms 11372 KB Output is correct
74 Correct 129 ms 39788 KB Output is correct
75 Correct 105 ms 40684 KB Output is correct
76 Correct 106 ms 40560 KB Output is correct
77 Correct 116 ms 41836 KB Output is correct
78 Correct 88 ms 20332 KB Output is correct
79 Correct 72 ms 19948 KB Output is correct
80 Correct 108 ms 34924 KB Output is correct
81 Correct 149 ms 44704 KB Output is correct
82 Correct 156 ms 46316 KB Output is correct
83 Correct 165 ms 46688 KB Output is correct
84 Correct 96 ms 43628 KB Output is correct
85 Correct 158 ms 46700 KB Output is correct
86 Correct 95 ms 22940 KB Output is correct
87 Correct 334 ms 45616 KB Output is correct
88 Correct 338 ms 45504 KB Output is correct
89 Correct 383 ms 45016 KB Output is correct
90 Correct 191 ms 24900 KB Output is correct
91 Correct 205 ms 27152 KB Output is correct
92 Correct 352 ms 39808 KB Output is correct
93 Correct 374 ms 49332 KB Output is correct
94 Correct 430 ms 50728 KB Output is correct
95 Correct 405 ms 50540 KB Output is correct
96 Correct 431 ms 48212 KB Output is correct
97 Correct 409 ms 49452 KB Output is correct