Submission #551017

# Submission time Handle Problem Language Result Execution time Memory
551017 2022-04-19T17:47:19 Z hoanghq2004 Swapping Cities (APIO20_swap) C++14
100 / 100
314 ms 60472 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define Submit
#ifdef Submit
#include "swap.h"

#endif // Submit

using namespace __gnu_pbds;
using namespace std;

const int N = 3e5 + 10;

int n;
int deg[N];
int root[N], ok[N];
vector <int> g[N];
int par[N][20];
int in[N], out[N], ti, ans[N];

int Find(int u) {
    return (u == root[u] ? u : root[u] = Find(root[u]));
}

int Union(int u, int v, int _) {
    ++n;
    root[n] = n;
    if ((u = Find(u)) == (v = Find(v))) {
        ok[n] = 1;
        root[u] = n;
        g[n].push_back(u);
    } else {
        ok[n] = ok[u] | ok[v] | _;
        root[u] = root[v] = n;
        g[n].push_back(u);
        g[n].push_back(v);
    }
    return n;
}

void dfs(int u) {
    in[u] = ++ti;
    for (auto v: g[u]) {
        par[v][0] = u;
        for (int i = 1; i < 20; ++i)
            par[v][i] = par[par[v][i - 1]][i - 1];
        dfs(v);
    }
    out[u] = ti;
}

int check(int u, int v) {
    return in[u] <= in[v] && out[v] <= out[u];
}

int LCA(int u, int v) {
    if (check(u, v)) return u;
    for (int i = 19; i >= 0; --i)
        if (par[u][i] && !check(par[u][i], v)) u = par[u][i];
    return par[u][0];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    n = N;
    vector <tuple <int, int, int> > e;
    for (int i = 1; i <= n; ++i) root[i] = i;
    for (int i = 0; i < M; ++i) {
        ++U[i], ++V[i];
        e.push_back({W[i], U[i], V[i]});
    }
    sort(e.begin(), e.end());
    for (int i = 0; i < e.size(); ++i) {
        auto [w, u, v] = e[i];
        ++deg[u], ++deg[v];
        int cur = Union(u, v, (deg[u] > 2 || deg[v] > 2));
        ans[cur] = w;
    }
    for (int i = n; i >= 1; --i)
        if (in[i] == 0) dfs(i);
}

int getMinimumFuelCapacity(int X, int Y) {
    ++X, ++Y;
    int w = LCA(X, Y);
    if (ok[w]) return ans[w];
    for (int i = 19; i >= 0; --i)
        if (par[w][i] && !ok[par[w][i]]) w = par[w][i];
    w = par[w][0];
    if (ok[w]) return ans[w];
    return -1;
}


#ifndef Submit

int main() {
  int N, M;
  assert(2 == scanf("%d %d", &N, &M));

  std::vector<int> U(M), V(M), W(M);
  for (int i = 0; i < M; ++i) {
    assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
  }

  int Q;
  assert(1 == scanf("%d", &Q));

  std::vector<int> X(Q), Y(Q);
  for (int i = 0; i < Q; ++i) {
    assert(2 == scanf("%d %d", &X[i], &Y[i]));
  }

  init(N, M, U, V, W);

  std::vector<int> minimum_fuel_capacities(Q);
  for (int i = 0; i < Q; ++i) {
    minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
  }

  for (int i = 0; i < Q; ++i) {
    printf("%d\n", minimum_fuel_capacities[i]);
  }

  return 0;
}

#endif // Submit

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (int i = 0; i < e.size(); ++i) {
      |                     ~~^~~~~~~~~~
swap.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |         auto [w, u, v] = e[i];
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 5 ms 7572 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 5 ms 7636 KB Output is correct
9 Correct 80 ms 27660 KB Output is correct
10 Correct 104 ms 32168 KB Output is correct
11 Correct 96 ms 31776 KB Output is correct
12 Correct 100 ms 33220 KB Output is correct
13 Correct 89 ms 34880 KB Output is correct
14 Correct 89 ms 27728 KB Output is correct
15 Correct 176 ms 33868 KB Output is correct
16 Correct 173 ms 33160 KB Output is correct
17 Correct 175 ms 34768 KB Output is correct
18 Correct 280 ms 36416 KB Output is correct
19 Correct 71 ms 14716 KB Output is correct
20 Correct 172 ms 34404 KB Output is correct
21 Correct 171 ms 33660 KB Output is correct
22 Correct 184 ms 35308 KB Output is correct
23 Correct 270 ms 36836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 291 ms 36712 KB Output is correct
4 Correct 233 ms 38024 KB Output is correct
5 Correct 244 ms 37332 KB Output is correct
6 Correct 227 ms 37924 KB Output is correct
7 Correct 267 ms 37684 KB Output is correct
8 Correct 231 ms 36452 KB Output is correct
9 Correct 234 ms 37364 KB Output is correct
10 Correct 226 ms 36212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 5 ms 7572 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 5 ms 7636 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 6 ms 7636 KB Output is correct
11 Correct 5 ms 7636 KB Output is correct
12 Correct 4 ms 7636 KB Output is correct
13 Correct 5 ms 7508 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 5 ms 7636 KB Output is correct
16 Correct 5 ms 7660 KB Output is correct
17 Correct 4 ms 7636 KB Output is correct
18 Correct 5 ms 7636 KB Output is correct
19 Correct 5 ms 7636 KB Output is correct
20 Correct 4 ms 7636 KB Output is correct
21 Correct 4 ms 7636 KB Output is correct
22 Correct 5 ms 7636 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 5 ms 7696 KB Output is correct
25 Correct 5 ms 7824 KB Output is correct
26 Correct 5 ms 7764 KB Output is correct
27 Correct 5 ms 7636 KB Output is correct
28 Correct 7 ms 7772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 5 ms 7636 KB Output is correct
7 Correct 5 ms 7572 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 5 ms 7636 KB Output is correct
10 Correct 80 ms 27660 KB Output is correct
11 Correct 104 ms 32168 KB Output is correct
12 Correct 96 ms 31776 KB Output is correct
13 Correct 100 ms 33220 KB Output is correct
14 Correct 89 ms 34880 KB Output is correct
15 Correct 6 ms 7636 KB Output is correct
16 Correct 5 ms 7636 KB Output is correct
17 Correct 4 ms 7636 KB Output is correct
18 Correct 5 ms 7508 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7636 KB Output is correct
21 Correct 5 ms 7660 KB Output is correct
22 Correct 4 ms 7636 KB Output is correct
23 Correct 5 ms 7636 KB Output is correct
24 Correct 5 ms 7636 KB Output is correct
25 Correct 4 ms 7636 KB Output is correct
26 Correct 4 ms 7636 KB Output is correct
27 Correct 5 ms 7636 KB Output is correct
28 Correct 4 ms 7508 KB Output is correct
29 Correct 5 ms 7696 KB Output is correct
30 Correct 5 ms 7824 KB Output is correct
31 Correct 5 ms 7764 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 7 ms 7772 KB Output is correct
34 Correct 15 ms 10900 KB Output is correct
35 Correct 101 ms 33324 KB Output is correct
36 Correct 99 ms 33344 KB Output is correct
37 Correct 101 ms 33240 KB Output is correct
38 Correct 99 ms 32892 KB Output is correct
39 Correct 97 ms 32852 KB Output is correct
40 Correct 93 ms 30656 KB Output is correct
41 Correct 101 ms 33288 KB Output is correct
42 Correct 96 ms 33240 KB Output is correct
43 Correct 89 ms 35352 KB Output is correct
44 Correct 105 ms 33416 KB Output is correct
45 Correct 132 ms 41740 KB Output is correct
46 Correct 101 ms 34624 KB Output is correct
47 Correct 102 ms 34740 KB Output is correct
48 Correct 115 ms 37572 KB Output is correct
49 Correct 101 ms 39828 KB Output is correct
50 Correct 84 ms 32888 KB Output is correct
51 Correct 114 ms 38492 KB Output is correct
52 Correct 148 ms 46600 KB Output is correct
53 Correct 173 ms 49548 KB Output is correct
54 Correct 174 ms 54180 KB Output is correct
55 Correct 84 ms 36504 KB Output is correct
56 Correct 160 ms 49660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 5 ms 7572 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 5 ms 7636 KB Output is correct
9 Correct 80 ms 27660 KB Output is correct
10 Correct 104 ms 32168 KB Output is correct
11 Correct 96 ms 31776 KB Output is correct
12 Correct 100 ms 33220 KB Output is correct
13 Correct 89 ms 34880 KB Output is correct
14 Correct 89 ms 27728 KB Output is correct
15 Correct 176 ms 33868 KB Output is correct
16 Correct 173 ms 33160 KB Output is correct
17 Correct 175 ms 34768 KB Output is correct
18 Correct 280 ms 36416 KB Output is correct
19 Correct 291 ms 36712 KB Output is correct
20 Correct 233 ms 38024 KB Output is correct
21 Correct 244 ms 37332 KB Output is correct
22 Correct 227 ms 37924 KB Output is correct
23 Correct 267 ms 37684 KB Output is correct
24 Correct 231 ms 36452 KB Output is correct
25 Correct 234 ms 37364 KB Output is correct
26 Correct 226 ms 36212 KB Output is correct
27 Correct 6 ms 7636 KB Output is correct
28 Correct 5 ms 7636 KB Output is correct
29 Correct 4 ms 7636 KB Output is correct
30 Correct 5 ms 7508 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 5 ms 7660 KB Output is correct
34 Correct 4 ms 7636 KB Output is correct
35 Correct 5 ms 7636 KB Output is correct
36 Correct 15 ms 10900 KB Output is correct
37 Correct 101 ms 33324 KB Output is correct
38 Correct 99 ms 33344 KB Output is correct
39 Correct 101 ms 33240 KB Output is correct
40 Correct 99 ms 32892 KB Output is correct
41 Correct 97 ms 32852 KB Output is correct
42 Correct 93 ms 30656 KB Output is correct
43 Correct 101 ms 33288 KB Output is correct
44 Correct 96 ms 33240 KB Output is correct
45 Correct 89 ms 35352 KB Output is correct
46 Correct 105 ms 33416 KB Output is correct
47 Correct 20 ms 10776 KB Output is correct
48 Correct 170 ms 34800 KB Output is correct
49 Correct 164 ms 34732 KB Output is correct
50 Correct 169 ms 34772 KB Output is correct
51 Correct 174 ms 34512 KB Output is correct
52 Correct 165 ms 33088 KB Output is correct
53 Correct 143 ms 27224 KB Output is correct
54 Correct 176 ms 35152 KB Output is correct
55 Correct 174 ms 34952 KB Output is correct
56 Correct 230 ms 36448 KB Output is correct
57 Correct 183 ms 35228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 5 ms 7636 KB Output is correct
7 Correct 5 ms 7572 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 5 ms 7636 KB Output is correct
10 Correct 80 ms 27660 KB Output is correct
11 Correct 104 ms 32168 KB Output is correct
12 Correct 96 ms 31776 KB Output is correct
13 Correct 100 ms 33220 KB Output is correct
14 Correct 89 ms 34880 KB Output is correct
15 Correct 89 ms 27728 KB Output is correct
16 Correct 176 ms 33868 KB Output is correct
17 Correct 173 ms 33160 KB Output is correct
18 Correct 175 ms 34768 KB Output is correct
19 Correct 280 ms 36416 KB Output is correct
20 Correct 291 ms 36712 KB Output is correct
21 Correct 233 ms 38024 KB Output is correct
22 Correct 244 ms 37332 KB Output is correct
23 Correct 227 ms 37924 KB Output is correct
24 Correct 267 ms 37684 KB Output is correct
25 Correct 231 ms 36452 KB Output is correct
26 Correct 234 ms 37364 KB Output is correct
27 Correct 226 ms 36212 KB Output is correct
28 Correct 6 ms 7636 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 4 ms 7636 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7508 KB Output is correct
33 Correct 5 ms 7636 KB Output is correct
34 Correct 5 ms 7660 KB Output is correct
35 Correct 4 ms 7636 KB Output is correct
36 Correct 5 ms 7636 KB Output is correct
37 Correct 15 ms 10900 KB Output is correct
38 Correct 101 ms 33324 KB Output is correct
39 Correct 99 ms 33344 KB Output is correct
40 Correct 101 ms 33240 KB Output is correct
41 Correct 99 ms 32892 KB Output is correct
42 Correct 97 ms 32852 KB Output is correct
43 Correct 93 ms 30656 KB Output is correct
44 Correct 101 ms 33288 KB Output is correct
45 Correct 96 ms 33240 KB Output is correct
46 Correct 89 ms 35352 KB Output is correct
47 Correct 105 ms 33416 KB Output is correct
48 Correct 20 ms 10776 KB Output is correct
49 Correct 170 ms 34800 KB Output is correct
50 Correct 164 ms 34732 KB Output is correct
51 Correct 169 ms 34772 KB Output is correct
52 Correct 174 ms 34512 KB Output is correct
53 Correct 165 ms 33088 KB Output is correct
54 Correct 143 ms 27224 KB Output is correct
55 Correct 176 ms 35152 KB Output is correct
56 Correct 174 ms 34952 KB Output is correct
57 Correct 230 ms 36448 KB Output is correct
58 Correct 183 ms 35228 KB Output is correct
59 Correct 71 ms 14716 KB Output is correct
60 Correct 172 ms 34404 KB Output is correct
61 Correct 171 ms 33660 KB Output is correct
62 Correct 184 ms 35308 KB Output is correct
63 Correct 270 ms 36836 KB Output is correct
64 Correct 5 ms 7636 KB Output is correct
65 Correct 4 ms 7636 KB Output is correct
66 Correct 4 ms 7636 KB Output is correct
67 Correct 5 ms 7636 KB Output is correct
68 Correct 4 ms 7508 KB Output is correct
69 Correct 5 ms 7696 KB Output is correct
70 Correct 5 ms 7824 KB Output is correct
71 Correct 5 ms 7764 KB Output is correct
72 Correct 5 ms 7636 KB Output is correct
73 Correct 7 ms 7772 KB Output is correct
74 Correct 132 ms 41740 KB Output is correct
75 Correct 101 ms 34624 KB Output is correct
76 Correct 102 ms 34740 KB Output is correct
77 Correct 115 ms 37572 KB Output is correct
78 Correct 101 ms 39828 KB Output is correct
79 Correct 84 ms 32888 KB Output is correct
80 Correct 114 ms 38492 KB Output is correct
81 Correct 148 ms 46600 KB Output is correct
82 Correct 173 ms 49548 KB Output is correct
83 Correct 174 ms 54180 KB Output is correct
84 Correct 84 ms 36504 KB Output is correct
85 Correct 160 ms 49660 KB Output is correct
86 Correct 81 ms 21372 KB Output is correct
87 Correct 186 ms 38880 KB Output is correct
88 Correct 169 ms 38852 KB Output is correct
89 Correct 240 ms 40448 KB Output is correct
90 Correct 194 ms 46392 KB Output is correct
91 Correct 197 ms 43812 KB Output is correct
92 Correct 251 ms 42312 KB Output is correct
93 Correct 231 ms 51816 KB Output is correct
94 Correct 314 ms 55420 KB Output is correct
95 Correct 267 ms 60472 KB Output is correct
96 Correct 229 ms 40636 KB Output is correct
97 Correct 307 ms 47668 KB Output is correct