Submission #406475

# Submission time Handle Problem Language Result Execution time Memory
406475 2021-05-17T16:00:44 Z tengiz05 Swapping Cities (APIO20_swap) C++17
100 / 100
480 ms 70192 KB
#include "swap.h"
#include <bits/stdc++.h>
constexpr int N = 3e5 + 5;
int n, m;
int up[N][20], f[N], ans[N], tin[N], tout[N];
std::vector<int> e[N], to[N];
struct desu{
    std::vector<int> p;
    desu(int n){
        p.assign(n, 0);
        iota(p.begin(), p.end(), 0);
    }
    int leader(int u){
        return (u == p[u] ? u : p[u] = leader(p[u]));
    }
    void merge(int u, int v){
        u = leader(u);
        v = leader(v);
        if (u == v) return;
        p[v] = u;
    }
    bool same(int u, int v){
        return leader(u) == leader(v);
    }
}d(N);
int timer = 0;
void dfs(int u, int p){
    tin[u] = timer++;
    up[u][0] = p;
    for (int i = 1; i < 20; i++) up[u][i] = up[up[u][i - 1]][i - 1];
    for (auto v : to[u]) {
        dfs(v, u);
    }
    tout[u] = timer;
}
bool is(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
int lca(int u, int v){
    if (is(u, v)) return u;
    if (is(v, u)) return v;
    for (int i = 19; i >= 0; i--) {
        if (!is(up[u][i], v)) u = up[u][i];
    }
    return up[u][0];
}
bool answer;
void init(int n, int m, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    ::n = n;
    ::m = m;
    std::vector<std::tuple<int, int, int>> edges;
    for (int i = 0; i < m; i++) {
        edges.emplace_back(W[i], U[i], V[i]);
    }
    sort(edges.begin(), edges.end());
    for (auto [w, u, v] : edges) {
        int nd = n++;
        ans[nd] = w;
        e[u].push_back(v);
        e[v].push_back(u);
        if (d.same(u, v)) {
            f[nd] = true;
            to[nd].push_back(d.leader(u));
            d.merge(nd, u);
        } else {
            to[nd].push_back(d.leader(u));
            to[nd].push_back(d.leader(v));
            f[nd] = f[d.leader(u)] | f[d.leader(v)] | (e[u].size() > 2 || e[v].size() > 2);
            d.merge(nd, u);
            d.merge(nd, v);
        }
    }
    assert(n == ::n + m);
    dfs(n - 1, n - 1);
    if (f[n - 1]) answer = true;
}
int getMinimumFuelCapacity(int X, int Y) {
    // if (!answer) return -1;
    int l = lca(X, Y);
    if (f[l]) return ans[l];
    for (int i = 19; i >= 0; i--)
        if (!f[up[l][i]]) l = up[l][i];
    l = up[l][0];
    if(f[l]) return ans[l];
    return -1;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15564 KB Output is correct
2 Correct 9 ms 15580 KB Output is correct
3 Correct 9 ms 15564 KB Output is correct
4 Correct 10 ms 15708 KB Output is correct
5 Correct 10 ms 15780 KB Output is correct
6 Correct 10 ms 15844 KB Output is correct
7 Correct 11 ms 15820 KB Output is correct
8 Correct 11 ms 15820 KB Output is correct
9 Correct 137 ms 37804 KB Output is correct
10 Correct 172 ms 42652 KB Output is correct
11 Correct 170 ms 42160 KB Output is correct
12 Correct 176 ms 43660 KB Output is correct
13 Correct 159 ms 46092 KB Output is correct
14 Correct 143 ms 37840 KB Output is correct
15 Correct 295 ms 44608 KB Output is correct
16 Correct 312 ms 43740 KB Output is correct
17 Correct 311 ms 45584 KB Output is correct
18 Correct 380 ms 47824 KB Output is correct
19 Correct 126 ms 23596 KB Output is correct
20 Correct 301 ms 45292 KB Output is correct
21 Correct 307 ms 44240 KB Output is correct
22 Correct 320 ms 45980 KB Output is correct
23 Correct 402 ms 48260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15564 KB Output is correct
2 Correct 9 ms 15580 KB Output is correct
3 Correct 329 ms 48876 KB Output is correct
4 Correct 348 ms 50376 KB Output is correct
5 Correct 337 ms 49672 KB Output is correct
6 Correct 330 ms 50220 KB Output is correct
7 Correct 343 ms 49984 KB Output is correct
8 Correct 332 ms 48528 KB Output is correct
9 Correct 330 ms 49528 KB Output is correct
10 Correct 319 ms 48196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15564 KB Output is correct
2 Correct 9 ms 15580 KB Output is correct
3 Correct 9 ms 15564 KB Output is correct
4 Correct 10 ms 15708 KB Output is correct
5 Correct 10 ms 15780 KB Output is correct
6 Correct 10 ms 15844 KB Output is correct
7 Correct 11 ms 15820 KB Output is correct
8 Correct 11 ms 15820 KB Output is correct
9 Correct 12 ms 15564 KB Output is correct
10 Correct 11 ms 15820 KB Output is correct
11 Correct 11 ms 15848 KB Output is correct
12 Correct 11 ms 15820 KB Output is correct
13 Correct 13 ms 15820 KB Output is correct
14 Correct 12 ms 15844 KB Output is correct
15 Correct 10 ms 15776 KB Output is correct
16 Correct 11 ms 15844 KB Output is correct
17 Correct 12 ms 15820 KB Output is correct
18 Correct 11 ms 15820 KB Output is correct
19 Correct 13 ms 15952 KB Output is correct
20 Correct 12 ms 15844 KB Output is correct
21 Correct 12 ms 15820 KB Output is correct
22 Correct 11 ms 15976 KB Output is correct
23 Correct 10 ms 15820 KB Output is correct
24 Correct 11 ms 15996 KB Output is correct
25 Correct 12 ms 16076 KB Output is correct
26 Correct 12 ms 16096 KB Output is correct
27 Correct 11 ms 15820 KB Output is correct
28 Correct 12 ms 16076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 15564 KB Output is correct
2 Correct 9 ms 15564 KB Output is correct
3 Correct 9 ms 15580 KB Output is correct
4 Correct 9 ms 15564 KB Output is correct
5 Correct 10 ms 15708 KB Output is correct
6 Correct 10 ms 15780 KB Output is correct
7 Correct 10 ms 15844 KB Output is correct
8 Correct 11 ms 15820 KB Output is correct
9 Correct 11 ms 15820 KB Output is correct
10 Correct 137 ms 37804 KB Output is correct
11 Correct 172 ms 42652 KB Output is correct
12 Correct 170 ms 42160 KB Output is correct
13 Correct 176 ms 43660 KB Output is correct
14 Correct 159 ms 46092 KB Output is correct
15 Correct 11 ms 15820 KB Output is correct
16 Correct 11 ms 15848 KB Output is correct
17 Correct 11 ms 15820 KB Output is correct
18 Correct 13 ms 15820 KB Output is correct
19 Correct 12 ms 15844 KB Output is correct
20 Correct 10 ms 15776 KB Output is correct
21 Correct 11 ms 15844 KB Output is correct
22 Correct 12 ms 15820 KB Output is correct
23 Correct 11 ms 15820 KB Output is correct
24 Correct 13 ms 15952 KB Output is correct
25 Correct 12 ms 15844 KB Output is correct
26 Correct 12 ms 15820 KB Output is correct
27 Correct 11 ms 15976 KB Output is correct
28 Correct 10 ms 15820 KB Output is correct
29 Correct 11 ms 15996 KB Output is correct
30 Correct 12 ms 16076 KB Output is correct
31 Correct 12 ms 16096 KB Output is correct
32 Correct 11 ms 15820 KB Output is correct
33 Correct 12 ms 16076 KB Output is correct
34 Correct 28 ms 19584 KB Output is correct
35 Correct 173 ms 45236 KB Output is correct
36 Correct 186 ms 45252 KB Output is correct
37 Correct 175 ms 45132 KB Output is correct
38 Correct 176 ms 44724 KB Output is correct
39 Correct 172 ms 44728 KB Output is correct
40 Correct 158 ms 42184 KB Output is correct
41 Correct 181 ms 45592 KB Output is correct
42 Correct 170 ms 45104 KB Output is correct
43 Correct 163 ms 48312 KB Output is correct
44 Correct 179 ms 45516 KB Output is correct
45 Correct 236 ms 56180 KB Output is correct
46 Correct 174 ms 45136 KB Output is correct
47 Correct 174 ms 45288 KB Output is correct
48 Correct 188 ms 48828 KB Output is correct
49 Correct 142 ms 53424 KB Output is correct
50 Correct 122 ms 44992 KB Output is correct
51 Correct 207 ms 50744 KB Output is correct
52 Correct 261 ms 59632 KB Output is correct
53 Correct 298 ms 62816 KB Output is correct
54 Correct 318 ms 68444 KB Output is correct
55 Correct 156 ms 48080 KB Output is correct
56 Correct 292 ms 63564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15564 KB Output is correct
2 Correct 9 ms 15580 KB Output is correct
3 Correct 9 ms 15564 KB Output is correct
4 Correct 10 ms 15708 KB Output is correct
5 Correct 10 ms 15780 KB Output is correct
6 Correct 10 ms 15844 KB Output is correct
7 Correct 11 ms 15820 KB Output is correct
8 Correct 11 ms 15820 KB Output is correct
9 Correct 137 ms 37804 KB Output is correct
10 Correct 172 ms 42652 KB Output is correct
11 Correct 170 ms 42160 KB Output is correct
12 Correct 176 ms 43660 KB Output is correct
13 Correct 159 ms 46092 KB Output is correct
14 Correct 143 ms 37840 KB Output is correct
15 Correct 295 ms 44608 KB Output is correct
16 Correct 312 ms 43740 KB Output is correct
17 Correct 311 ms 45584 KB Output is correct
18 Correct 380 ms 47824 KB Output is correct
19 Correct 329 ms 48876 KB Output is correct
20 Correct 348 ms 50376 KB Output is correct
21 Correct 337 ms 49672 KB Output is correct
22 Correct 330 ms 50220 KB Output is correct
23 Correct 343 ms 49984 KB Output is correct
24 Correct 332 ms 48528 KB Output is correct
25 Correct 330 ms 49528 KB Output is correct
26 Correct 319 ms 48196 KB Output is correct
27 Correct 11 ms 15820 KB Output is correct
28 Correct 11 ms 15848 KB Output is correct
29 Correct 11 ms 15820 KB Output is correct
30 Correct 13 ms 15820 KB Output is correct
31 Correct 12 ms 15844 KB Output is correct
32 Correct 10 ms 15776 KB Output is correct
33 Correct 11 ms 15844 KB Output is correct
34 Correct 12 ms 15820 KB Output is correct
35 Correct 11 ms 15820 KB Output is correct
36 Correct 28 ms 19584 KB Output is correct
37 Correct 173 ms 45236 KB Output is correct
38 Correct 186 ms 45252 KB Output is correct
39 Correct 175 ms 45132 KB Output is correct
40 Correct 176 ms 44724 KB Output is correct
41 Correct 172 ms 44728 KB Output is correct
42 Correct 158 ms 42184 KB Output is correct
43 Correct 181 ms 45592 KB Output is correct
44 Correct 170 ms 45104 KB Output is correct
45 Correct 163 ms 48312 KB Output is correct
46 Correct 179 ms 45516 KB Output is correct
47 Correct 34 ms 19620 KB Output is correct
48 Correct 287 ms 48972 KB Output is correct
49 Correct 284 ms 49080 KB Output is correct
50 Correct 288 ms 48976 KB Output is correct
51 Correct 291 ms 48820 KB Output is correct
52 Correct 287 ms 47120 KB Output is correct
53 Correct 237 ms 40380 KB Output is correct
54 Correct 299 ms 49324 KB Output is correct
55 Correct 303 ms 49104 KB Output is correct
56 Correct 325 ms 51408 KB Output is correct
57 Correct 301 ms 49408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 15564 KB Output is correct
2 Correct 9 ms 15564 KB Output is correct
3 Correct 9 ms 15580 KB Output is correct
4 Correct 9 ms 15564 KB Output is correct
5 Correct 10 ms 15708 KB Output is correct
6 Correct 10 ms 15780 KB Output is correct
7 Correct 10 ms 15844 KB Output is correct
8 Correct 11 ms 15820 KB Output is correct
9 Correct 11 ms 15820 KB Output is correct
10 Correct 137 ms 37804 KB Output is correct
11 Correct 172 ms 42652 KB Output is correct
12 Correct 170 ms 42160 KB Output is correct
13 Correct 176 ms 43660 KB Output is correct
14 Correct 159 ms 46092 KB Output is correct
15 Correct 143 ms 37840 KB Output is correct
16 Correct 295 ms 44608 KB Output is correct
17 Correct 312 ms 43740 KB Output is correct
18 Correct 311 ms 45584 KB Output is correct
19 Correct 380 ms 47824 KB Output is correct
20 Correct 329 ms 48876 KB Output is correct
21 Correct 348 ms 50376 KB Output is correct
22 Correct 337 ms 49672 KB Output is correct
23 Correct 330 ms 50220 KB Output is correct
24 Correct 343 ms 49984 KB Output is correct
25 Correct 332 ms 48528 KB Output is correct
26 Correct 330 ms 49528 KB Output is correct
27 Correct 319 ms 48196 KB Output is correct
28 Correct 11 ms 15820 KB Output is correct
29 Correct 11 ms 15848 KB Output is correct
30 Correct 11 ms 15820 KB Output is correct
31 Correct 13 ms 15820 KB Output is correct
32 Correct 12 ms 15844 KB Output is correct
33 Correct 10 ms 15776 KB Output is correct
34 Correct 11 ms 15844 KB Output is correct
35 Correct 12 ms 15820 KB Output is correct
36 Correct 11 ms 15820 KB Output is correct
37 Correct 28 ms 19584 KB Output is correct
38 Correct 173 ms 45236 KB Output is correct
39 Correct 186 ms 45252 KB Output is correct
40 Correct 175 ms 45132 KB Output is correct
41 Correct 176 ms 44724 KB Output is correct
42 Correct 172 ms 44728 KB Output is correct
43 Correct 158 ms 42184 KB Output is correct
44 Correct 181 ms 45592 KB Output is correct
45 Correct 170 ms 45104 KB Output is correct
46 Correct 163 ms 48312 KB Output is correct
47 Correct 179 ms 45516 KB Output is correct
48 Correct 34 ms 19620 KB Output is correct
49 Correct 287 ms 48972 KB Output is correct
50 Correct 284 ms 49080 KB Output is correct
51 Correct 288 ms 48976 KB Output is correct
52 Correct 291 ms 48820 KB Output is correct
53 Correct 287 ms 47120 KB Output is correct
54 Correct 237 ms 40380 KB Output is correct
55 Correct 299 ms 49324 KB Output is correct
56 Correct 303 ms 49104 KB Output is correct
57 Correct 325 ms 51408 KB Output is correct
58 Correct 301 ms 49408 KB Output is correct
59 Correct 126 ms 23596 KB Output is correct
60 Correct 301 ms 45292 KB Output is correct
61 Correct 307 ms 44240 KB Output is correct
62 Correct 320 ms 45980 KB Output is correct
63 Correct 402 ms 48260 KB Output is correct
64 Correct 13 ms 15952 KB Output is correct
65 Correct 12 ms 15844 KB Output is correct
66 Correct 12 ms 15820 KB Output is correct
67 Correct 11 ms 15976 KB Output is correct
68 Correct 10 ms 15820 KB Output is correct
69 Correct 11 ms 15996 KB Output is correct
70 Correct 12 ms 16076 KB Output is correct
71 Correct 12 ms 16096 KB Output is correct
72 Correct 11 ms 15820 KB Output is correct
73 Correct 12 ms 16076 KB Output is correct
74 Correct 236 ms 56180 KB Output is correct
75 Correct 174 ms 45136 KB Output is correct
76 Correct 174 ms 45288 KB Output is correct
77 Correct 188 ms 48828 KB Output is correct
78 Correct 142 ms 53424 KB Output is correct
79 Correct 122 ms 44992 KB Output is correct
80 Correct 207 ms 50744 KB Output is correct
81 Correct 261 ms 59632 KB Output is correct
82 Correct 298 ms 62816 KB Output is correct
83 Correct 318 ms 68444 KB Output is correct
84 Correct 156 ms 48080 KB Output is correct
85 Correct 292 ms 63564 KB Output is correct
86 Correct 115 ms 30704 KB Output is correct
87 Correct 305 ms 48852 KB Output is correct
88 Correct 289 ms 48824 KB Output is correct
89 Correct 360 ms 50808 KB Output is correct
90 Correct 266 ms 57816 KB Output is correct
91 Correct 289 ms 54512 KB Output is correct
92 Correct 371 ms 52152 KB Output is correct
93 Correct 400 ms 61288 KB Output is correct
94 Correct 480 ms 64436 KB Output is correct
95 Correct 453 ms 70192 KB Output is correct
96 Correct 319 ms 51516 KB Output is correct
97 Correct 436 ms 57720 KB Output is correct