Submission #571311

# Submission time Handle Problem Language Result Execution time Memory
571311 2022-06-01T17:04:03 Z Vanilla Swapping Cities (APIO20_swap) C++17
100 / 100
334 ms 62496 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 200;
int extra = 1e5 + 1;
const int lg = 18;
int deg [maxn];
int dad [maxn];
vector <int> val (maxn, 1e9);
vector <bool> good (maxn, 0);
vector <int> cmp [maxn];
vector <int> depth (maxn);
int up[maxn][lg];
 
void dfs (int u, int vs) {
    if (good[u]) vs = val[u];
    else val[u] = vs;
    for (auto v: cmp[u]) {
        depth[v] = depth[u] + 1;
        up[v][0] = u;
        for (int i = 1; i < lg; i++){
            up[v][i] = up[up[v][i-1]][i-1];
        }
        dfs(v, vs);
    }
}
 
int lca (int x, int y) {
    if (depth[x] < depth[y]) {
        swap(x, y);
    }
    int k = depth[x] - depth[y];
    for (int i = 0; i < lg; i++){
        if (k & (1 << i)) {
            x = up[x][i];
        }
    }
    if (x == y) return x;
    for (int i = lg - 1; i >= 0; i--) {
        if (up[x][i] != up[y][i]) {
            x = up[x][i];
            y = up[y][i];
        }
    }
    return up[x][0];
}
 
 
int get_dad (int x) {
  return dad[x] = (dad[x] == x ? x: get_dad(dad[x]));
}
 
bool propagate (int u) {
  for (int v: cmp[u]) {
    good[u]= good[u] | propagate(v);
  }
  return good[u];
}
 
struct edg {
  int x,y,cost;
};
 
bool comp (edg &a, edg &b) {
  return a.cost < b.cost;
}
 
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
  vector <edg> eg;
  for (int i = 0; i < maxn; i++){
    dad[i] = i;
    for (int j = 0; j < lg; j++){
      up[i][j] = maxn-1;
    }
  }
  for (int i = 0; i < m; i++){
    eg.push_back({u[i], v[i], w[i]});
  }
  sort(eg.begin(), eg.end(), comp);
  for (auto [u,v,cost]: eg) {
    int p1 = get_dad(u);
    int p2 = get_dad(v);
    deg[u]++;
    deg[v]++;
    val[++extra] = cost;
    dad[p1] = extra;
    dad[p2] = extra;
    cmp[extra].push_back(p1);
    if (p1 != p2) cmp[extra].push_back(p2);
    if (p1 == p2 || deg[u] >= 3 || deg[v] >= 3) {
      good[extra] = 1;
    }
  
  }
  propagate(extra);
  dfs(extra, 1e9);

}
 
int getMinimumFuelCapacity(int x, int y) {
  int k = lca(x,y);
  if (val[k] == 1e9) return -1;
  return val[k];
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32084 KB Output is correct
2 Correct 16 ms 31992 KB Output is correct
3 Correct 16 ms 31976 KB Output is correct
4 Correct 24 ms 31972 KB Output is correct
5 Correct 16 ms 32084 KB Output is correct
6 Correct 18 ms 32068 KB Output is correct
7 Correct 16 ms 32084 KB Output is correct
8 Correct 16 ms 32084 KB Output is correct
9 Correct 102 ms 37556 KB Output is correct
10 Correct 123 ms 38744 KB Output is correct
11 Correct 122 ms 38708 KB Output is correct
12 Correct 133 ms 39048 KB Output is correct
13 Correct 108 ms 42976 KB Output is correct
14 Correct 112 ms 37720 KB Output is correct
15 Correct 230 ms 40516 KB Output is correct
16 Correct 218 ms 40132 KB Output is correct
17 Correct 230 ms 40672 KB Output is correct
18 Correct 296 ms 44492 KB Output is correct
19 Correct 93 ms 36432 KB Output is correct
20 Correct 222 ms 40640 KB Output is correct
21 Correct 221 ms 40800 KB Output is correct
22 Correct 241 ms 41080 KB Output is correct
23 Correct 283 ms 44944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32084 KB Output is correct
2 Correct 16 ms 31992 KB Output is correct
3 Correct 245 ms 48008 KB Output is correct
4 Correct 254 ms 48524 KB Output is correct
5 Correct 255 ms 48368 KB Output is correct
6 Correct 241 ms 48388 KB Output is correct
7 Correct 245 ms 48524 KB Output is correct
8 Correct 239 ms 47928 KB Output is correct
9 Correct 247 ms 48456 KB Output is correct
10 Correct 238 ms 47892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32084 KB Output is correct
2 Correct 16 ms 31992 KB Output is correct
3 Correct 16 ms 31976 KB Output is correct
4 Correct 24 ms 31972 KB Output is correct
5 Correct 16 ms 32084 KB Output is correct
6 Correct 18 ms 32068 KB Output is correct
7 Correct 16 ms 32084 KB Output is correct
8 Correct 16 ms 32084 KB Output is correct
9 Correct 16 ms 32060 KB Output is correct
10 Correct 17 ms 32076 KB Output is correct
11 Correct 16 ms 32036 KB Output is correct
12 Correct 17 ms 32084 KB Output is correct
13 Correct 17 ms 32024 KB Output is correct
14 Correct 16 ms 32068 KB Output is correct
15 Correct 20 ms 32112 KB Output is correct
16 Correct 17 ms 32084 KB Output is correct
17 Correct 16 ms 32084 KB Output is correct
18 Correct 16 ms 32084 KB Output is correct
19 Correct 18 ms 32164 KB Output is correct
20 Correct 17 ms 32156 KB Output is correct
21 Correct 17 ms 32100 KB Output is correct
22 Correct 18 ms 32248 KB Output is correct
23 Correct 16 ms 32048 KB Output is correct
24 Correct 16 ms 32212 KB Output is correct
25 Correct 17 ms 32160 KB Output is correct
26 Correct 20 ms 32320 KB Output is correct
27 Correct 17 ms 32140 KB Output is correct
28 Correct 17 ms 32308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 32060 KB Output is correct
2 Correct 15 ms 32084 KB Output is correct
3 Correct 16 ms 31992 KB Output is correct
4 Correct 16 ms 31976 KB Output is correct
5 Correct 24 ms 31972 KB Output is correct
6 Correct 16 ms 32084 KB Output is correct
7 Correct 18 ms 32068 KB Output is correct
8 Correct 16 ms 32084 KB Output is correct
9 Correct 16 ms 32084 KB Output is correct
10 Correct 102 ms 37556 KB Output is correct
11 Correct 123 ms 38744 KB Output is correct
12 Correct 122 ms 38708 KB Output is correct
13 Correct 133 ms 39048 KB Output is correct
14 Correct 108 ms 42976 KB Output is correct
15 Correct 17 ms 32076 KB Output is correct
16 Correct 16 ms 32036 KB Output is correct
17 Correct 17 ms 32084 KB Output is correct
18 Correct 17 ms 32024 KB Output is correct
19 Correct 16 ms 32068 KB Output is correct
20 Correct 20 ms 32112 KB Output is correct
21 Correct 17 ms 32084 KB Output is correct
22 Correct 16 ms 32084 KB Output is correct
23 Correct 16 ms 32084 KB Output is correct
24 Correct 18 ms 32164 KB Output is correct
25 Correct 17 ms 32156 KB Output is correct
26 Correct 17 ms 32100 KB Output is correct
27 Correct 18 ms 32248 KB Output is correct
28 Correct 16 ms 32048 KB Output is correct
29 Correct 16 ms 32212 KB Output is correct
30 Correct 17 ms 32160 KB Output is correct
31 Correct 20 ms 32320 KB Output is correct
32 Correct 17 ms 32140 KB Output is correct
33 Correct 17 ms 32308 KB Output is correct
34 Correct 26 ms 32936 KB Output is correct
35 Correct 136 ms 39032 KB Output is correct
36 Correct 131 ms 39088 KB Output is correct
37 Correct 136 ms 39084 KB Output is correct
38 Correct 123 ms 38980 KB Output is correct
39 Correct 126 ms 38868 KB Output is correct
40 Correct 116 ms 38444 KB Output is correct
41 Correct 134 ms 39084 KB Output is correct
42 Correct 127 ms 39028 KB Output is correct
43 Correct 103 ms 44212 KB Output is correct
44 Correct 129 ms 39108 KB Output is correct
45 Correct 157 ms 52536 KB Output is correct
46 Correct 132 ms 40900 KB Output is correct
47 Correct 133 ms 41120 KB Output is correct
48 Correct 143 ms 44836 KB Output is correct
49 Correct 116 ms 57532 KB Output is correct
50 Correct 94 ms 51472 KB Output is correct
51 Correct 138 ms 50156 KB Output is correct
52 Correct 178 ms 51308 KB Output is correct
53 Correct 228 ms 52552 KB Output is correct
54 Correct 209 ms 57508 KB Output is correct
55 Correct 106 ms 45608 KB Output is correct
56 Correct 190 ms 55512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 32084 KB Output is correct
2 Correct 16 ms 31992 KB Output is correct
3 Correct 16 ms 31976 KB Output is correct
4 Correct 24 ms 31972 KB Output is correct
5 Correct 16 ms 32084 KB Output is correct
6 Correct 18 ms 32068 KB Output is correct
7 Correct 16 ms 32084 KB Output is correct
8 Correct 16 ms 32084 KB Output is correct
9 Correct 102 ms 37556 KB Output is correct
10 Correct 123 ms 38744 KB Output is correct
11 Correct 122 ms 38708 KB Output is correct
12 Correct 133 ms 39048 KB Output is correct
13 Correct 108 ms 42976 KB Output is correct
14 Correct 112 ms 37720 KB Output is correct
15 Correct 230 ms 40516 KB Output is correct
16 Correct 218 ms 40132 KB Output is correct
17 Correct 230 ms 40672 KB Output is correct
18 Correct 296 ms 44492 KB Output is correct
19 Correct 245 ms 48008 KB Output is correct
20 Correct 254 ms 48524 KB Output is correct
21 Correct 255 ms 48368 KB Output is correct
22 Correct 241 ms 48388 KB Output is correct
23 Correct 245 ms 48524 KB Output is correct
24 Correct 239 ms 47928 KB Output is correct
25 Correct 247 ms 48456 KB Output is correct
26 Correct 238 ms 47892 KB Output is correct
27 Correct 17 ms 32076 KB Output is correct
28 Correct 16 ms 32036 KB Output is correct
29 Correct 17 ms 32084 KB Output is correct
30 Correct 17 ms 32024 KB Output is correct
31 Correct 16 ms 32068 KB Output is correct
32 Correct 20 ms 32112 KB Output is correct
33 Correct 17 ms 32084 KB Output is correct
34 Correct 16 ms 32084 KB Output is correct
35 Correct 16 ms 32084 KB Output is correct
36 Correct 26 ms 32936 KB Output is correct
37 Correct 136 ms 39032 KB Output is correct
38 Correct 131 ms 39088 KB Output is correct
39 Correct 136 ms 39084 KB Output is correct
40 Correct 123 ms 38980 KB Output is correct
41 Correct 126 ms 38868 KB Output is correct
42 Correct 116 ms 38444 KB Output is correct
43 Correct 134 ms 39084 KB Output is correct
44 Correct 127 ms 39028 KB Output is correct
45 Correct 103 ms 44212 KB Output is correct
46 Correct 129 ms 39108 KB Output is correct
47 Correct 33 ms 33152 KB Output is correct
48 Correct 243 ms 40640 KB Output is correct
49 Correct 229 ms 40628 KB Output is correct
50 Correct 239 ms 40776 KB Output is correct
51 Correct 239 ms 40612 KB Output is correct
52 Correct 234 ms 40376 KB Output is correct
53 Correct 184 ms 39560 KB Output is correct
54 Correct 253 ms 41104 KB Output is correct
55 Correct 228 ms 40624 KB Output is correct
56 Correct 293 ms 44740 KB Output is correct
57 Correct 248 ms 41112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 32060 KB Output is correct
2 Correct 15 ms 32084 KB Output is correct
3 Correct 16 ms 31992 KB Output is correct
4 Correct 16 ms 31976 KB Output is correct
5 Correct 24 ms 31972 KB Output is correct
6 Correct 16 ms 32084 KB Output is correct
7 Correct 18 ms 32068 KB Output is correct
8 Correct 16 ms 32084 KB Output is correct
9 Correct 16 ms 32084 KB Output is correct
10 Correct 102 ms 37556 KB Output is correct
11 Correct 123 ms 38744 KB Output is correct
12 Correct 122 ms 38708 KB Output is correct
13 Correct 133 ms 39048 KB Output is correct
14 Correct 108 ms 42976 KB Output is correct
15 Correct 112 ms 37720 KB Output is correct
16 Correct 230 ms 40516 KB Output is correct
17 Correct 218 ms 40132 KB Output is correct
18 Correct 230 ms 40672 KB Output is correct
19 Correct 296 ms 44492 KB Output is correct
20 Correct 245 ms 48008 KB Output is correct
21 Correct 254 ms 48524 KB Output is correct
22 Correct 255 ms 48368 KB Output is correct
23 Correct 241 ms 48388 KB Output is correct
24 Correct 245 ms 48524 KB Output is correct
25 Correct 239 ms 47928 KB Output is correct
26 Correct 247 ms 48456 KB Output is correct
27 Correct 238 ms 47892 KB Output is correct
28 Correct 17 ms 32076 KB Output is correct
29 Correct 16 ms 32036 KB Output is correct
30 Correct 17 ms 32084 KB Output is correct
31 Correct 17 ms 32024 KB Output is correct
32 Correct 16 ms 32068 KB Output is correct
33 Correct 20 ms 32112 KB Output is correct
34 Correct 17 ms 32084 KB Output is correct
35 Correct 16 ms 32084 KB Output is correct
36 Correct 16 ms 32084 KB Output is correct
37 Correct 26 ms 32936 KB Output is correct
38 Correct 136 ms 39032 KB Output is correct
39 Correct 131 ms 39088 KB Output is correct
40 Correct 136 ms 39084 KB Output is correct
41 Correct 123 ms 38980 KB Output is correct
42 Correct 126 ms 38868 KB Output is correct
43 Correct 116 ms 38444 KB Output is correct
44 Correct 134 ms 39084 KB Output is correct
45 Correct 127 ms 39028 KB Output is correct
46 Correct 103 ms 44212 KB Output is correct
47 Correct 129 ms 39108 KB Output is correct
48 Correct 33 ms 33152 KB Output is correct
49 Correct 243 ms 40640 KB Output is correct
50 Correct 229 ms 40628 KB Output is correct
51 Correct 239 ms 40776 KB Output is correct
52 Correct 239 ms 40612 KB Output is correct
53 Correct 234 ms 40376 KB Output is correct
54 Correct 184 ms 39560 KB Output is correct
55 Correct 253 ms 41104 KB Output is correct
56 Correct 228 ms 40624 KB Output is correct
57 Correct 293 ms 44740 KB Output is correct
58 Correct 248 ms 41112 KB Output is correct
59 Correct 93 ms 36432 KB Output is correct
60 Correct 222 ms 40640 KB Output is correct
61 Correct 221 ms 40800 KB Output is correct
62 Correct 241 ms 41080 KB Output is correct
63 Correct 283 ms 44944 KB Output is correct
64 Correct 18 ms 32164 KB Output is correct
65 Correct 17 ms 32156 KB Output is correct
66 Correct 17 ms 32100 KB Output is correct
67 Correct 18 ms 32248 KB Output is correct
68 Correct 16 ms 32048 KB Output is correct
69 Correct 16 ms 32212 KB Output is correct
70 Correct 17 ms 32160 KB Output is correct
71 Correct 20 ms 32320 KB Output is correct
72 Correct 17 ms 32140 KB Output is correct
73 Correct 17 ms 32308 KB Output is correct
74 Correct 157 ms 52536 KB Output is correct
75 Correct 132 ms 40900 KB Output is correct
76 Correct 133 ms 41120 KB Output is correct
77 Correct 143 ms 44836 KB Output is correct
78 Correct 116 ms 57532 KB Output is correct
79 Correct 94 ms 51472 KB Output is correct
80 Correct 138 ms 50156 KB Output is correct
81 Correct 178 ms 51308 KB Output is correct
82 Correct 228 ms 52552 KB Output is correct
83 Correct 209 ms 57508 KB Output is correct
84 Correct 106 ms 45608 KB Output is correct
85 Correct 190 ms 55512 KB Output is correct
86 Correct 84 ms 39636 KB Output is correct
87 Correct 244 ms 44700 KB Output is correct
88 Correct 245 ms 44768 KB Output is correct
89 Correct 261 ms 48348 KB Output is correct
90 Correct 197 ms 62496 KB Output is correct
91 Correct 200 ms 59088 KB Output is correct
92 Correct 263 ms 52164 KB Output is correct
93 Correct 299 ms 52704 KB Output is correct
94 Correct 334 ms 54196 KB Output is correct
95 Correct 325 ms 58536 KB Output is correct
96 Correct 299 ms 49052 KB Output is correct
97 Correct 305 ms 51480 KB Output is correct