답안 #894260

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894260 2023-12-28T03:08:46 Z box 자매 도시 (APIO20_swap) C++17
100 / 100
293 ms 53972 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];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 35164 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Correct 5 ms 35332 KB Output is correct
4 Correct 6 ms 35164 KB Output is correct
5 Correct 5 ms 35164 KB Output is correct
6 Correct 5 ms 35164 KB Output is correct
7 Correct 6 ms 35164 KB Output is correct
8 Correct 7 ms 35300 KB Output is correct
9 Correct 79 ms 42064 KB Output is correct
10 Correct 106 ms 43432 KB Output is correct
11 Correct 95 ms 43348 KB Output is correct
12 Correct 105 ms 43756 KB Output is correct
13 Correct 154 ms 47012 KB Output is correct
14 Correct 94 ms 42324 KB Output is correct
15 Correct 203 ms 47436 KB Output is correct
16 Correct 172 ms 47008 KB Output is correct
17 Correct 226 ms 47692 KB Output is correct
18 Correct 237 ms 50680 KB Output is correct
19 Correct 68 ms 41752 KB Output is correct
20 Correct 204 ms 47760 KB Output is correct
21 Correct 202 ms 47916 KB Output is correct
22 Correct 212 ms 48808 KB Output is correct
23 Correct 253 ms 51628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 35164 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Correct 285 ms 53312 KB Output is correct
4 Correct 274 ms 53860 KB Output is correct
5 Correct 219 ms 53732 KB Output is correct
6 Correct 250 ms 53752 KB Output is correct
7 Correct 249 ms 53972 KB Output is correct
8 Correct 214 ms 53344 KB Output is correct
9 Correct 260 ms 53684 KB Output is correct
10 Correct 226 ms 53332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 35164 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Correct 5 ms 35332 KB Output is correct
4 Correct 6 ms 35164 KB Output is correct
5 Correct 5 ms 35164 KB Output is correct
6 Correct 5 ms 35164 KB Output is correct
7 Correct 6 ms 35164 KB Output is correct
8 Correct 7 ms 35300 KB Output is correct
9 Correct 6 ms 35160 KB Output is correct
10 Correct 5 ms 35420 KB Output is correct
11 Correct 5 ms 35372 KB Output is correct
12 Correct 6 ms 35160 KB Output is correct
13 Correct 5 ms 35164 KB Output is correct
14 Correct 5 ms 35348 KB Output is correct
15 Correct 6 ms 35164 KB Output is correct
16 Correct 5 ms 35288 KB Output is correct
17 Correct 5 ms 35420 KB Output is correct
18 Correct 6 ms 35252 KB Output is correct
19 Correct 6 ms 35160 KB Output is correct
20 Correct 6 ms 35288 KB Output is correct
21 Correct 6 ms 35164 KB Output is correct
22 Correct 6 ms 35164 KB Output is correct
23 Correct 5 ms 35164 KB Output is correct
24 Correct 6 ms 35420 KB Output is correct
25 Correct 6 ms 35416 KB Output is correct
26 Correct 6 ms 35420 KB Output is correct
27 Correct 6 ms 35420 KB Output is correct
28 Correct 6 ms 35292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35160 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Correct 5 ms 35164 KB Output is correct
4 Correct 5 ms 35332 KB Output is correct
5 Correct 6 ms 35164 KB Output is correct
6 Correct 5 ms 35164 KB Output is correct
7 Correct 5 ms 35164 KB Output is correct
8 Correct 6 ms 35164 KB Output is correct
9 Correct 7 ms 35300 KB Output is correct
10 Correct 79 ms 42064 KB Output is correct
11 Correct 106 ms 43432 KB Output is correct
12 Correct 95 ms 43348 KB Output is correct
13 Correct 105 ms 43756 KB Output is correct
14 Correct 154 ms 47012 KB Output is correct
15 Correct 5 ms 35420 KB Output is correct
16 Correct 5 ms 35372 KB Output is correct
17 Correct 6 ms 35160 KB Output is correct
18 Correct 5 ms 35164 KB Output is correct
19 Correct 5 ms 35348 KB Output is correct
20 Correct 6 ms 35164 KB Output is correct
21 Correct 5 ms 35288 KB Output is correct
22 Correct 5 ms 35420 KB Output is correct
23 Correct 6 ms 35252 KB Output is correct
24 Correct 6 ms 35160 KB Output is correct
25 Correct 6 ms 35288 KB Output is correct
26 Correct 6 ms 35164 KB Output is correct
27 Correct 6 ms 35164 KB Output is correct
28 Correct 5 ms 35164 KB Output is correct
29 Correct 6 ms 35420 KB Output is correct
30 Correct 6 ms 35416 KB Output is correct
31 Correct 6 ms 35420 KB Output is correct
32 Correct 6 ms 35420 KB Output is correct
33 Correct 6 ms 35292 KB Output is correct
34 Correct 13 ms 36552 KB Output is correct
35 Correct 101 ms 43440 KB Output is correct
36 Correct 103 ms 43432 KB Output is correct
37 Correct 108 ms 43588 KB Output is correct
38 Correct 93 ms 43348 KB Output is correct
39 Correct 104 ms 43436 KB Output is correct
40 Correct 84 ms 42576 KB Output is correct
41 Correct 100 ms 43704 KB Output is correct
42 Correct 111 ms 43496 KB Output is correct
43 Correct 125 ms 47712 KB Output is correct
44 Correct 102 ms 43856 KB Output is correct
45 Correct 120 ms 46756 KB Output is correct
46 Correct 112 ms 43588 KB Output is correct
47 Correct 103 ms 43664 KB Output is correct
48 Correct 108 ms 45536 KB Output is correct
49 Correct 57 ms 42508 KB Output is correct
50 Correct 49 ms 41300 KB Output is correct
51 Correct 109 ms 45216 KB Output is correct
52 Correct 133 ms 46764 KB Output is correct
53 Correct 142 ms 48360 KB Output is correct
54 Correct 144 ms 48756 KB Output is correct
55 Correct 96 ms 47324 KB Output is correct
56 Correct 164 ms 49232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 35164 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Correct 5 ms 35332 KB Output is correct
4 Correct 6 ms 35164 KB Output is correct
5 Correct 5 ms 35164 KB Output is correct
6 Correct 5 ms 35164 KB Output is correct
7 Correct 6 ms 35164 KB Output is correct
8 Correct 7 ms 35300 KB Output is correct
9 Correct 79 ms 42064 KB Output is correct
10 Correct 106 ms 43432 KB Output is correct
11 Correct 95 ms 43348 KB Output is correct
12 Correct 105 ms 43756 KB Output is correct
13 Correct 154 ms 47012 KB Output is correct
14 Correct 94 ms 42324 KB Output is correct
15 Correct 203 ms 47436 KB Output is correct
16 Correct 172 ms 47008 KB Output is correct
17 Correct 226 ms 47692 KB Output is correct
18 Correct 237 ms 50680 KB Output is correct
19 Correct 285 ms 53312 KB Output is correct
20 Correct 274 ms 53860 KB Output is correct
21 Correct 219 ms 53732 KB Output is correct
22 Correct 250 ms 53752 KB Output is correct
23 Correct 249 ms 53972 KB Output is correct
24 Correct 214 ms 53344 KB Output is correct
25 Correct 260 ms 53684 KB Output is correct
26 Correct 226 ms 53332 KB Output is correct
27 Correct 5 ms 35420 KB Output is correct
28 Correct 5 ms 35372 KB Output is correct
29 Correct 6 ms 35160 KB Output is correct
30 Correct 5 ms 35164 KB Output is correct
31 Correct 5 ms 35348 KB Output is correct
32 Correct 6 ms 35164 KB Output is correct
33 Correct 5 ms 35288 KB Output is correct
34 Correct 5 ms 35420 KB Output is correct
35 Correct 6 ms 35252 KB Output is correct
36 Correct 13 ms 36552 KB Output is correct
37 Correct 101 ms 43440 KB Output is correct
38 Correct 103 ms 43432 KB Output is correct
39 Correct 108 ms 43588 KB Output is correct
40 Correct 93 ms 43348 KB Output is correct
41 Correct 104 ms 43436 KB Output is correct
42 Correct 84 ms 42576 KB Output is correct
43 Correct 100 ms 43704 KB Output is correct
44 Correct 111 ms 43496 KB Output is correct
45 Correct 125 ms 47712 KB Output is correct
46 Correct 102 ms 43856 KB Output is correct
47 Correct 19 ms 36952 KB Output is correct
48 Correct 208 ms 47568 KB Output is correct
49 Correct 191 ms 47528 KB Output is correct
50 Correct 191 ms 47528 KB Output is correct
51 Correct 202 ms 47516 KB Output is correct
52 Correct 199 ms 47184 KB Output is correct
53 Correct 148 ms 46420 KB Output is correct
54 Correct 211 ms 48356 KB Output is correct
55 Correct 196 ms 47536 KB Output is correct
56 Correct 293 ms 51092 KB Output is correct
57 Correct 226 ms 48416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 35160 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Correct 5 ms 35164 KB Output is correct
4 Correct 5 ms 35332 KB Output is correct
5 Correct 6 ms 35164 KB Output is correct
6 Correct 5 ms 35164 KB Output is correct
7 Correct 5 ms 35164 KB Output is correct
8 Correct 6 ms 35164 KB Output is correct
9 Correct 7 ms 35300 KB Output is correct
10 Correct 79 ms 42064 KB Output is correct
11 Correct 106 ms 43432 KB Output is correct
12 Correct 95 ms 43348 KB Output is correct
13 Correct 105 ms 43756 KB Output is correct
14 Correct 154 ms 47012 KB Output is correct
15 Correct 94 ms 42324 KB Output is correct
16 Correct 203 ms 47436 KB Output is correct
17 Correct 172 ms 47008 KB Output is correct
18 Correct 226 ms 47692 KB Output is correct
19 Correct 237 ms 50680 KB Output is correct
20 Correct 285 ms 53312 KB Output is correct
21 Correct 274 ms 53860 KB Output is correct
22 Correct 219 ms 53732 KB Output is correct
23 Correct 250 ms 53752 KB Output is correct
24 Correct 249 ms 53972 KB Output is correct
25 Correct 214 ms 53344 KB Output is correct
26 Correct 260 ms 53684 KB Output is correct
27 Correct 226 ms 53332 KB Output is correct
28 Correct 5 ms 35420 KB Output is correct
29 Correct 5 ms 35372 KB Output is correct
30 Correct 6 ms 35160 KB Output is correct
31 Correct 5 ms 35164 KB Output is correct
32 Correct 5 ms 35348 KB Output is correct
33 Correct 6 ms 35164 KB Output is correct
34 Correct 5 ms 35288 KB Output is correct
35 Correct 5 ms 35420 KB Output is correct
36 Correct 6 ms 35252 KB Output is correct
37 Correct 13 ms 36552 KB Output is correct
38 Correct 101 ms 43440 KB Output is correct
39 Correct 103 ms 43432 KB Output is correct
40 Correct 108 ms 43588 KB Output is correct
41 Correct 93 ms 43348 KB Output is correct
42 Correct 104 ms 43436 KB Output is correct
43 Correct 84 ms 42576 KB Output is correct
44 Correct 100 ms 43704 KB Output is correct
45 Correct 111 ms 43496 KB Output is correct
46 Correct 125 ms 47712 KB Output is correct
47 Correct 102 ms 43856 KB Output is correct
48 Correct 19 ms 36952 KB Output is correct
49 Correct 208 ms 47568 KB Output is correct
50 Correct 191 ms 47528 KB Output is correct
51 Correct 191 ms 47528 KB Output is correct
52 Correct 202 ms 47516 KB Output is correct
53 Correct 199 ms 47184 KB Output is correct
54 Correct 148 ms 46420 KB Output is correct
55 Correct 211 ms 48356 KB Output is correct
56 Correct 196 ms 47536 KB Output is correct
57 Correct 293 ms 51092 KB Output is correct
58 Correct 226 ms 48416 KB Output is correct
59 Correct 68 ms 41752 KB Output is correct
60 Correct 204 ms 47760 KB Output is correct
61 Correct 202 ms 47916 KB Output is correct
62 Correct 212 ms 48808 KB Output is correct
63 Correct 253 ms 51628 KB Output is correct
64 Correct 6 ms 35160 KB Output is correct
65 Correct 6 ms 35288 KB Output is correct
66 Correct 6 ms 35164 KB Output is correct
67 Correct 6 ms 35164 KB Output is correct
68 Correct 5 ms 35164 KB Output is correct
69 Correct 6 ms 35420 KB Output is correct
70 Correct 6 ms 35416 KB Output is correct
71 Correct 6 ms 35420 KB Output is correct
72 Correct 6 ms 35420 KB Output is correct
73 Correct 6 ms 35292 KB Output is correct
74 Correct 120 ms 46756 KB Output is correct
75 Correct 112 ms 43588 KB Output is correct
76 Correct 103 ms 43664 KB Output is correct
77 Correct 108 ms 45536 KB Output is correct
78 Correct 57 ms 42508 KB Output is correct
79 Correct 49 ms 41300 KB Output is correct
80 Correct 109 ms 45216 KB Output is correct
81 Correct 133 ms 46764 KB Output is correct
82 Correct 142 ms 48360 KB Output is correct
83 Correct 144 ms 48756 KB Output is correct
84 Correct 96 ms 47324 KB Output is correct
85 Correct 164 ms 49232 KB Output is correct
86 Correct 54 ms 40460 KB Output is correct
87 Correct 188 ms 47524 KB Output is correct
88 Correct 172 ms 47664 KB Output is correct
89 Correct 216 ms 48952 KB Output is correct
90 Correct 133 ms 46416 KB Output is correct
91 Correct 133 ms 46824 KB Output is correct
92 Correct 187 ms 49360 KB Output is correct
93 Correct 237 ms 50432 KB Output is correct
94 Correct 258 ms 51920 KB Output is correct
95 Correct 237 ms 52308 KB Output is correct
96 Correct 225 ms 51064 KB Output is correct
97 Correct 260 ms 51260 KB Output is correct