Submission #807534

# Submission time Handle Problem Language Result Execution time Memory
807534 2023-08-04T19:05:04 Z dong_liu Swapping Cities (APIO20_swap) C++17
100 / 100
322 ms 52368 KB
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#include "swap.h"

typedef vector<int> vi;

const int N = 3e5, L = 19, INF = int(1e9) + 1;

int p[N];

int find(int i) { return p[i] == i ? i : p[i] = find(p[i]); }

int tag1[N], tag2[N], ans[N];
vi g[N];

int pp[L][N], dd[N];

void dfs(int p, int i) {
  pp[0][i] = p;
  for (int l = 1; l < L; l++) pp[l][i] = pp[l - 1][pp[l - 1][i]];
  ans[i] = min(ans[i], max(tag1[i], tag2[i]));
  for (int j : g[i]) {
    dd[j] = dd[i] + 1;
    ans[j] = min(ans[j], ans[i]);
    dfs(i, j);
  }
}

int lca(int i, int j) {
  if (dd[i] < dd[j]) swap(i, j);
  int k = dd[i] - dd[j];
  for (int l = 0; l < L; l++)
    if (k >> l & 1) i = pp[l][i];
  if (i == j) return i;
  for (int l = L - 1; l >= 0; l--)
    if (pp[l][i] != pp[l][j]) i = pp[l][i], j = pp[l][j];
  return pp[0][i];
}

void init(int n, int m, vi u, vi v, vi w) {
  vi order(m);
  for (int i = 0; i < m; i++) order[i] = i;
  sort(order.begin(), order.end(), [&](int i, int j) { return w[i] < w[j]; });
  vi d(n);
  for (int i = 0; i < n + m; i++) {
    p[i] = i;
    ans[i] = tag1[i] = tag2[i] = INF;
  }
  int root = -1;
  for (int h : order) {
    int i = u[h], j = v[h];
    d[i]++;
    d[j]++;
    bool swap = d[i] >= 3 || d[j] >= 3;
    if (find(i) != find(j)) {
      g[n + h].push_back(find(i));
      g[n + h].push_back(find(j));
      tag1[n + h] = min(tag1[n + h], w[h]);
      tag2[n + h] = min(tag2[find(i)], tag2[find(j)]);
      if (swap) tag2[n + h] = min(tag2[n + h], w[h]);
      p[find(i)] = p[find(j)] = n + h;
      root = n + h;
    } else {
      int c = find(i);
      tag2[c] = min(tag2[c], w[h]);
    }
  }
  dfs(root, root);
}

int getMinimumFuelCapacity(int i, int j) {
  int p = lca(i, j);
  return ans[p] == INF ? -1 : ans[p];
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7636 KB Output is correct
6 Correct 4 ms 7636 KB Output is correct
7 Correct 4 ms 7756 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 104 ms 28812 KB Output is correct
10 Correct 111 ms 33544 KB Output is correct
11 Correct 111 ms 33100 KB Output is correct
12 Correct 117 ms 34508 KB Output is correct
13 Correct 139 ms 37768 KB Output is correct
14 Correct 97 ms 29016 KB Output is correct
15 Correct 212 ms 37576 KB Output is correct
16 Correct 218 ms 36812 KB Output is correct
17 Correct 213 ms 38512 KB Output is correct
18 Correct 256 ms 41540 KB Output is correct
19 Correct 66 ms 16832 KB Output is correct
20 Correct 207 ms 38120 KB Output is correct
21 Correct 208 ms 37492 KB Output is correct
22 Correct 218 ms 39328 KB Output is correct
23 Correct 270 ms 42400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 232 ms 43152 KB Output is correct
4 Correct 248 ms 44600 KB Output is correct
5 Correct 277 ms 43888 KB Output is correct
6 Correct 257 ms 44476 KB Output is correct
7 Correct 250 ms 44316 KB Output is correct
8 Correct 223 ms 42964 KB Output is correct
9 Correct 265 ms 43924 KB Output is correct
10 Correct 257 ms 42704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7636 KB Output is correct
6 Correct 4 ms 7636 KB Output is correct
7 Correct 4 ms 7756 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 5 ms 7640 KB Output is correct
11 Correct 4 ms 7636 KB Output is correct
12 Correct 3 ms 7636 KB Output is correct
13 Correct 4 ms 7636 KB Output is correct
14 Correct 5 ms 7628 KB Output is correct
15 Correct 4 ms 7764 KB Output is correct
16 Correct 4 ms 7756 KB Output is correct
17 Correct 5 ms 7764 KB Output is correct
18 Correct 4 ms 7756 KB Output is correct
19 Correct 3 ms 7636 KB Output is correct
20 Correct 4 ms 7636 KB Output is correct
21 Correct 4 ms 7756 KB Output is correct
22 Correct 4 ms 7760 KB Output is correct
23 Correct 4 ms 7636 KB Output is correct
24 Correct 4 ms 7764 KB Output is correct
25 Correct 4 ms 7764 KB Output is correct
26 Correct 4 ms 7892 KB Output is correct
27 Correct 4 ms 7764 KB Output is correct
28 Correct 4 ms 7884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7636 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7756 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 104 ms 28812 KB Output is correct
11 Correct 111 ms 33544 KB Output is correct
12 Correct 111 ms 33100 KB Output is correct
13 Correct 117 ms 34508 KB Output is correct
14 Correct 139 ms 37768 KB Output is correct
15 Correct 5 ms 7640 KB Output is correct
16 Correct 4 ms 7636 KB Output is correct
17 Correct 3 ms 7636 KB Output is correct
18 Correct 4 ms 7636 KB Output is correct
19 Correct 5 ms 7628 KB Output is correct
20 Correct 4 ms 7764 KB Output is correct
21 Correct 4 ms 7756 KB Output is correct
22 Correct 5 ms 7764 KB Output is correct
23 Correct 4 ms 7756 KB Output is correct
24 Correct 3 ms 7636 KB Output is correct
25 Correct 4 ms 7636 KB Output is correct
26 Correct 4 ms 7756 KB Output is correct
27 Correct 4 ms 7760 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 4 ms 7764 KB Output is correct
30 Correct 4 ms 7764 KB Output is correct
31 Correct 4 ms 7892 KB Output is correct
32 Correct 4 ms 7764 KB Output is correct
33 Correct 4 ms 7884 KB Output is correct
34 Correct 12 ms 11092 KB Output is correct
35 Correct 132 ms 34248 KB Output is correct
36 Correct 112 ms 34340 KB Output is correct
37 Correct 109 ms 34252 KB Output is correct
38 Correct 109 ms 33956 KB Output is correct
39 Correct 116 ms 33840 KB Output is correct
40 Correct 98 ms 31568 KB Output is correct
41 Correct 117 ms 34616 KB Output is correct
42 Correct 112 ms 34340 KB Output is correct
43 Correct 117 ms 38436 KB Output is correct
44 Correct 115 ms 34608 KB Output is correct
45 Correct 130 ms 40012 KB Output is correct
46 Correct 119 ms 34248 KB Output is correct
47 Correct 133 ms 34380 KB Output is correct
48 Correct 127 ms 37064 KB Output is correct
49 Correct 60 ms 29556 KB Output is correct
50 Correct 52 ms 25408 KB Output is correct
51 Correct 107 ms 35228 KB Output is correct
52 Correct 147 ms 43380 KB Output is correct
53 Correct 164 ms 46540 KB Output is correct
54 Correct 164 ms 48484 KB Output is correct
55 Correct 119 ms 37964 KB Output is correct
56 Correct 168 ms 46748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 4 ms 7636 KB Output is correct
6 Correct 4 ms 7636 KB Output is correct
7 Correct 4 ms 7756 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 104 ms 28812 KB Output is correct
10 Correct 111 ms 33544 KB Output is correct
11 Correct 111 ms 33100 KB Output is correct
12 Correct 117 ms 34508 KB Output is correct
13 Correct 139 ms 37768 KB Output is correct
14 Correct 97 ms 29016 KB Output is correct
15 Correct 212 ms 37576 KB Output is correct
16 Correct 218 ms 36812 KB Output is correct
17 Correct 213 ms 38512 KB Output is correct
18 Correct 256 ms 41540 KB Output is correct
19 Correct 232 ms 43152 KB Output is correct
20 Correct 248 ms 44600 KB Output is correct
21 Correct 277 ms 43888 KB Output is correct
22 Correct 257 ms 44476 KB Output is correct
23 Correct 250 ms 44316 KB Output is correct
24 Correct 223 ms 42964 KB Output is correct
25 Correct 265 ms 43924 KB Output is correct
26 Correct 257 ms 42704 KB Output is correct
27 Correct 5 ms 7640 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 3 ms 7636 KB Output is correct
30 Correct 4 ms 7636 KB Output is correct
31 Correct 5 ms 7628 KB Output is correct
32 Correct 4 ms 7764 KB Output is correct
33 Correct 4 ms 7756 KB Output is correct
34 Correct 5 ms 7764 KB Output is correct
35 Correct 4 ms 7756 KB Output is correct
36 Correct 12 ms 11092 KB Output is correct
37 Correct 132 ms 34248 KB Output is correct
38 Correct 112 ms 34340 KB Output is correct
39 Correct 109 ms 34252 KB Output is correct
40 Correct 109 ms 33956 KB Output is correct
41 Correct 116 ms 33840 KB Output is correct
42 Correct 98 ms 31568 KB Output is correct
43 Correct 117 ms 34616 KB Output is correct
44 Correct 112 ms 34340 KB Output is correct
45 Correct 117 ms 38436 KB Output is correct
46 Correct 115 ms 34608 KB Output is correct
47 Correct 19 ms 11324 KB Output is correct
48 Correct 214 ms 38336 KB Output is correct
49 Correct 211 ms 38324 KB Output is correct
50 Correct 258 ms 38348 KB Output is correct
51 Correct 206 ms 38248 KB Output is correct
52 Correct 197 ms 36772 KB Output is correct
53 Correct 159 ms 30800 KB Output is correct
54 Correct 215 ms 39228 KB Output is correct
55 Correct 258 ms 38420 KB Output is correct
56 Correct 245 ms 41704 KB Output is correct
57 Correct 244 ms 39364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7508 KB Output is correct
6 Correct 4 ms 7636 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7756 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 104 ms 28812 KB Output is correct
11 Correct 111 ms 33544 KB Output is correct
12 Correct 111 ms 33100 KB Output is correct
13 Correct 117 ms 34508 KB Output is correct
14 Correct 139 ms 37768 KB Output is correct
15 Correct 97 ms 29016 KB Output is correct
16 Correct 212 ms 37576 KB Output is correct
17 Correct 218 ms 36812 KB Output is correct
18 Correct 213 ms 38512 KB Output is correct
19 Correct 256 ms 41540 KB Output is correct
20 Correct 232 ms 43152 KB Output is correct
21 Correct 248 ms 44600 KB Output is correct
22 Correct 277 ms 43888 KB Output is correct
23 Correct 257 ms 44476 KB Output is correct
24 Correct 250 ms 44316 KB Output is correct
25 Correct 223 ms 42964 KB Output is correct
26 Correct 265 ms 43924 KB Output is correct
27 Correct 257 ms 42704 KB Output is correct
28 Correct 5 ms 7640 KB Output is correct
29 Correct 4 ms 7636 KB Output is correct
30 Correct 3 ms 7636 KB Output is correct
31 Correct 4 ms 7636 KB Output is correct
32 Correct 5 ms 7628 KB Output is correct
33 Correct 4 ms 7764 KB Output is correct
34 Correct 4 ms 7756 KB Output is correct
35 Correct 5 ms 7764 KB Output is correct
36 Correct 4 ms 7756 KB Output is correct
37 Correct 12 ms 11092 KB Output is correct
38 Correct 132 ms 34248 KB Output is correct
39 Correct 112 ms 34340 KB Output is correct
40 Correct 109 ms 34252 KB Output is correct
41 Correct 109 ms 33956 KB Output is correct
42 Correct 116 ms 33840 KB Output is correct
43 Correct 98 ms 31568 KB Output is correct
44 Correct 117 ms 34616 KB Output is correct
45 Correct 112 ms 34340 KB Output is correct
46 Correct 117 ms 38436 KB Output is correct
47 Correct 115 ms 34608 KB Output is correct
48 Correct 19 ms 11324 KB Output is correct
49 Correct 214 ms 38336 KB Output is correct
50 Correct 211 ms 38324 KB Output is correct
51 Correct 258 ms 38348 KB Output is correct
52 Correct 206 ms 38248 KB Output is correct
53 Correct 197 ms 36772 KB Output is correct
54 Correct 159 ms 30800 KB Output is correct
55 Correct 215 ms 39228 KB Output is correct
56 Correct 258 ms 38420 KB Output is correct
57 Correct 245 ms 41704 KB Output is correct
58 Correct 244 ms 39364 KB Output is correct
59 Correct 66 ms 16832 KB Output is correct
60 Correct 207 ms 38120 KB Output is correct
61 Correct 208 ms 37492 KB Output is correct
62 Correct 218 ms 39328 KB Output is correct
63 Correct 270 ms 42400 KB Output is correct
64 Correct 3 ms 7636 KB Output is correct
65 Correct 4 ms 7636 KB Output is correct
66 Correct 4 ms 7756 KB Output is correct
67 Correct 4 ms 7760 KB Output is correct
68 Correct 4 ms 7636 KB Output is correct
69 Correct 4 ms 7764 KB Output is correct
70 Correct 4 ms 7764 KB Output is correct
71 Correct 4 ms 7892 KB Output is correct
72 Correct 4 ms 7764 KB Output is correct
73 Correct 4 ms 7884 KB Output is correct
74 Correct 130 ms 40012 KB Output is correct
75 Correct 119 ms 34248 KB Output is correct
76 Correct 133 ms 34380 KB Output is correct
77 Correct 127 ms 37064 KB Output is correct
78 Correct 60 ms 29556 KB Output is correct
79 Correct 52 ms 25408 KB Output is correct
80 Correct 107 ms 35228 KB Output is correct
81 Correct 147 ms 43380 KB Output is correct
82 Correct 164 ms 46540 KB Output is correct
83 Correct 164 ms 48484 KB Output is correct
84 Correct 119 ms 37964 KB Output is correct
85 Correct 168 ms 46748 KB Output is correct
86 Correct 71 ms 20148 KB Output is correct
87 Correct 204 ms 38440 KB Output is correct
88 Correct 201 ms 38332 KB Output is correct
89 Correct 236 ms 39720 KB Output is correct
90 Correct 143 ms 34264 KB Output is correct
91 Correct 174 ms 33932 KB Output is correct
92 Correct 233 ms 38872 KB Output is correct
93 Correct 254 ms 46836 KB Output is correct
94 Correct 316 ms 49908 KB Output is correct
95 Correct 322 ms 52368 KB Output is correct
96 Correct 263 ms 41976 KB Output is correct
97 Correct 300 ms 45392 KB Output is correct