Submission #981406

# Submission time Handle Problem Language Result Execution time Memory
981406 2024-05-13T07:14:42 Z Tsagana Swapping Cities (APIO20_swap) C++14
100 / 100
299 ms 44560 KB
#include "swap.h"

//OP
#include<bits/stdc++.h>

#define IOS ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define all(x) x.begin(), x.end()
#define pi pair<int, int >
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define mset multiset
#define F first
#define S second

using namespace std;

const int K = 23;

int  N;
int  M;
int  id;
int  fa[300001];
int  val[300001];
int  dep[300001];
int  back[300001];
int  G[300001][2];
int  front[300001];
int  sp[300001][K];
bool chain[300001];
vector<pair<int, pair<int, int>>> edges;

int find(int x) {return (x == fa[x] ? x : fa[x] = find(fa[x]));}

void dfs(int u, int f) {
  if (!u) return;
  dep[u] = dep[f] + 1; sp[u][0] = f;
  for (int i = 1; i < K; i++)  sp[u][i] = sp[sp[u][i-1]][i-1];
  for (int child = 0; child < 2; child++) dfs(G[u][child], u);
}

int lca(int u, int v) {
  if  (dep[v] >= dep[u]) swap(u, v);
  for (int i = K - 1; i >= 0; i--)
  if  (dep[sp[u][i]] >= dep[v]) u = sp[u][i];
  if  (u == v) return u;
  for (int i = K - 1; i >= 0; i--)
  if  (sp[u][i] != sp[v][i]) u = sp[u][i], v = sp[v][i];
  return sp[u][0];
}

void init(int _N, int _M, vector<int> U, vector<int> V, vector<int> W) {
  N = _N; M = _M; id = N;
  for (int i = 0; i < M; i++) edges.pb({W[i], {U[i], V[i]}});
  sort(all(edges));
  for (int i = 0; i < 300001; i++) fa[i] = i;
  for (int i = 1; i <= N; i++) {chain[i] = 1; front[i] = back[i] = i;}
  for (int i = 0; i < M; i++) {
    int w = edges[i].F;
    int u = edges[i].S.F + 1;
    int v = edges[i].S.S + 1;
    int A = find(u);
    int B = find(v);

    if (A == B) {
      if (chain[A]) {
        int new_id = ++id; fa[A] = new_id;
        val[new_id] = w; G[new_id][0] = A;
      }
      continue;
    }
    int new_id = ++id;  fa[A] = new_id;  fa[B] = new_id;
    val[new_id] = w; G[new_id][0] = A; G[new_id][1] = B;
    if (chain[A] && chain[B]) {
      if (front[A] == v || back[A] == v) swap(A, B);
      if (front[A] == u) swap(front[A], back[A]);
      if (back[B]  == v) swap(front[B], back[B]);
      if (back[A]  == u && front[B] == v) {
        chain[new_id] = 1;
        front[new_id] = front[A];
        back[new_id]  = back[B];
      }
    }
  }
  dfs(id, 0);
}

int getMinimumFuelCapacity(int X, int Y) {
  X++; Y++; int l = lca(X, Y);
  if (!chain[l]) return val[l];
  for (int i = K - 1; i >= 0; i--)
    if (sp[l][i] && chain[sp[l][i]]) l = sp[l][i];
  return (l == id ? -1 : val[sp[l][0]]);
}
//ED
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9164 KB Output is correct
8 Correct 2 ms 9052 KB Output is correct
9 Correct 66 ms 27500 KB Output is correct
10 Correct 84 ms 32708 KB Output is correct
11 Correct 78 ms 32484 KB Output is correct
12 Correct 88 ms 32932 KB Output is correct
13 Correct 82 ms 33916 KB Output is correct
14 Correct 87 ms 27904 KB Output is correct
15 Correct 207 ms 35520 KB Output is correct
16 Correct 204 ms 35456 KB Output is correct
17 Correct 236 ms 36136 KB Output is correct
18 Correct 299 ms 37164 KB Output is correct
19 Correct 89 ms 16404 KB Output is correct
20 Correct 254 ms 37604 KB Output is correct
21 Correct 206 ms 37756 KB Output is correct
22 Correct 239 ms 38424 KB Output is correct
23 Correct 291 ms 39472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 265 ms 40644 KB Output is correct
4 Correct 203 ms 40744 KB Output is correct
5 Correct 218 ms 40852 KB Output is correct
6 Correct 208 ms 40796 KB Output is correct
7 Correct 233 ms 40836 KB Output is correct
8 Correct 199 ms 40432 KB Output is correct
9 Correct 212 ms 40744 KB Output is correct
10 Correct 205 ms 40456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9164 KB Output is correct
8 Correct 2 ms 9052 KB Output is correct
9 Correct 2 ms 9052 KB Output is correct
10 Correct 2 ms 9176 KB Output is correct
11 Correct 2 ms 9052 KB Output is correct
12 Correct 2 ms 9052 KB Output is correct
13 Correct 2 ms 9052 KB Output is correct
14 Correct 2 ms 9052 KB Output is correct
15 Correct 2 ms 9052 KB Output is correct
16 Correct 2 ms 9052 KB Output is correct
17 Correct 2 ms 9052 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 2 ms 9048 KB Output is correct
20 Correct 2 ms 9048 KB Output is correct
21 Correct 2 ms 9052 KB Output is correct
22 Correct 2 ms 9052 KB Output is correct
23 Correct 2 ms 9052 KB Output is correct
24 Correct 2 ms 9052 KB Output is correct
25 Correct 3 ms 9052 KB Output is correct
26 Correct 2 ms 9052 KB Output is correct
27 Correct 2 ms 9052 KB Output is correct
28 Correct 2 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 2 ms 9048 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9164 KB Output is correct
9 Correct 2 ms 9052 KB Output is correct
10 Correct 66 ms 27500 KB Output is correct
11 Correct 84 ms 32708 KB Output is correct
12 Correct 78 ms 32484 KB Output is correct
13 Correct 88 ms 32932 KB Output is correct
14 Correct 82 ms 33916 KB Output is correct
15 Correct 2 ms 9176 KB Output is correct
16 Correct 2 ms 9052 KB Output is correct
17 Correct 2 ms 9052 KB Output is correct
18 Correct 2 ms 9052 KB Output is correct
19 Correct 2 ms 9052 KB Output is correct
20 Correct 2 ms 9052 KB Output is correct
21 Correct 2 ms 9052 KB Output is correct
22 Correct 2 ms 9052 KB Output is correct
23 Correct 2 ms 9052 KB Output is correct
24 Correct 2 ms 9048 KB Output is correct
25 Correct 2 ms 9048 KB Output is correct
26 Correct 2 ms 9052 KB Output is correct
27 Correct 2 ms 9052 KB Output is correct
28 Correct 2 ms 9052 KB Output is correct
29 Correct 2 ms 9052 KB Output is correct
30 Correct 3 ms 9052 KB Output is correct
31 Correct 2 ms 9052 KB Output is correct
32 Correct 2 ms 9052 KB Output is correct
33 Correct 2 ms 9052 KB Output is correct
34 Correct 10 ms 12060 KB Output is correct
35 Correct 84 ms 33224 KB Output is correct
36 Correct 81 ms 32964 KB Output is correct
37 Correct 72 ms 33212 KB Output is correct
38 Correct 71 ms 33128 KB Output is correct
39 Correct 78 ms 33120 KB Output is correct
40 Correct 64 ms 30408 KB Output is correct
41 Correct 75 ms 33440 KB Output is correct
42 Correct 73 ms 33224 KB Output is correct
43 Correct 71 ms 34500 KB Output is correct
44 Correct 74 ms 33528 KB Output is correct
45 Correct 80 ms 32348 KB Output is correct
46 Correct 73 ms 33220 KB Output is correct
47 Correct 71 ms 33220 KB Output is correct
48 Correct 77 ms 34248 KB Output is correct
49 Correct 51 ms 17504 KB Output is correct
50 Correct 41 ms 15812 KB Output is correct
51 Correct 66 ms 29128 KB Output is correct
52 Correct 95 ms 37492 KB Output is correct
53 Correct 119 ms 42188 KB Output is correct
54 Correct 105 ms 38840 KB Output is correct
55 Correct 68 ms 34500 KB Output is correct
56 Correct 116 ms 38144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9048 KB Output is correct
2 Correct 2 ms 9052 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9164 KB Output is correct
8 Correct 2 ms 9052 KB Output is correct
9 Correct 66 ms 27500 KB Output is correct
10 Correct 84 ms 32708 KB Output is correct
11 Correct 78 ms 32484 KB Output is correct
12 Correct 88 ms 32932 KB Output is correct
13 Correct 82 ms 33916 KB Output is correct
14 Correct 87 ms 27904 KB Output is correct
15 Correct 207 ms 35520 KB Output is correct
16 Correct 204 ms 35456 KB Output is correct
17 Correct 236 ms 36136 KB Output is correct
18 Correct 299 ms 37164 KB Output is correct
19 Correct 265 ms 40644 KB Output is correct
20 Correct 203 ms 40744 KB Output is correct
21 Correct 218 ms 40852 KB Output is correct
22 Correct 208 ms 40796 KB Output is correct
23 Correct 233 ms 40836 KB Output is correct
24 Correct 199 ms 40432 KB Output is correct
25 Correct 212 ms 40744 KB Output is correct
26 Correct 205 ms 40456 KB Output is correct
27 Correct 2 ms 9176 KB Output is correct
28 Correct 2 ms 9052 KB Output is correct
29 Correct 2 ms 9052 KB Output is correct
30 Correct 2 ms 9052 KB Output is correct
31 Correct 2 ms 9052 KB Output is correct
32 Correct 2 ms 9052 KB Output is correct
33 Correct 2 ms 9052 KB Output is correct
34 Correct 2 ms 9052 KB Output is correct
35 Correct 2 ms 9052 KB Output is correct
36 Correct 10 ms 12060 KB Output is correct
37 Correct 84 ms 33224 KB Output is correct
38 Correct 81 ms 32964 KB Output is correct
39 Correct 72 ms 33212 KB Output is correct
40 Correct 71 ms 33128 KB Output is correct
41 Correct 78 ms 33120 KB Output is correct
42 Correct 64 ms 30408 KB Output is correct
43 Correct 75 ms 33440 KB Output is correct
44 Correct 73 ms 33224 KB Output is correct
45 Correct 71 ms 34500 KB Output is correct
46 Correct 74 ms 33528 KB Output is correct
47 Correct 19 ms 12312 KB Output is correct
48 Correct 193 ms 37932 KB Output is correct
49 Correct 202 ms 38236 KB Output is correct
50 Correct 184 ms 38056 KB Output is correct
51 Correct 176 ms 37952 KB Output is correct
52 Correct 180 ms 37944 KB Output is correct
53 Correct 145 ms 30916 KB Output is correct
54 Correct 191 ms 38960 KB Output is correct
55 Correct 191 ms 37984 KB Output is correct
56 Correct 236 ms 39212 KB Output is correct
57 Correct 209 ms 38812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9052 KB Output is correct
2 Correct 2 ms 9048 KB Output is correct
3 Correct 2 ms 9052 KB Output is correct
4 Correct 2 ms 9052 KB Output is correct
5 Correct 2 ms 9052 KB Output is correct
6 Correct 2 ms 9052 KB Output is correct
7 Correct 2 ms 9052 KB Output is correct
8 Correct 2 ms 9164 KB Output is correct
9 Correct 2 ms 9052 KB Output is correct
10 Correct 66 ms 27500 KB Output is correct
11 Correct 84 ms 32708 KB Output is correct
12 Correct 78 ms 32484 KB Output is correct
13 Correct 88 ms 32932 KB Output is correct
14 Correct 82 ms 33916 KB Output is correct
15 Correct 87 ms 27904 KB Output is correct
16 Correct 207 ms 35520 KB Output is correct
17 Correct 204 ms 35456 KB Output is correct
18 Correct 236 ms 36136 KB Output is correct
19 Correct 299 ms 37164 KB Output is correct
20 Correct 265 ms 40644 KB Output is correct
21 Correct 203 ms 40744 KB Output is correct
22 Correct 218 ms 40852 KB Output is correct
23 Correct 208 ms 40796 KB Output is correct
24 Correct 233 ms 40836 KB Output is correct
25 Correct 199 ms 40432 KB Output is correct
26 Correct 212 ms 40744 KB Output is correct
27 Correct 205 ms 40456 KB Output is correct
28 Correct 2 ms 9176 KB Output is correct
29 Correct 2 ms 9052 KB Output is correct
30 Correct 2 ms 9052 KB Output is correct
31 Correct 2 ms 9052 KB Output is correct
32 Correct 2 ms 9052 KB Output is correct
33 Correct 2 ms 9052 KB Output is correct
34 Correct 2 ms 9052 KB Output is correct
35 Correct 2 ms 9052 KB Output is correct
36 Correct 2 ms 9052 KB Output is correct
37 Correct 10 ms 12060 KB Output is correct
38 Correct 84 ms 33224 KB Output is correct
39 Correct 81 ms 32964 KB Output is correct
40 Correct 72 ms 33212 KB Output is correct
41 Correct 71 ms 33128 KB Output is correct
42 Correct 78 ms 33120 KB Output is correct
43 Correct 64 ms 30408 KB Output is correct
44 Correct 75 ms 33440 KB Output is correct
45 Correct 73 ms 33224 KB Output is correct
46 Correct 71 ms 34500 KB Output is correct
47 Correct 74 ms 33528 KB Output is correct
48 Correct 19 ms 12312 KB Output is correct
49 Correct 193 ms 37932 KB Output is correct
50 Correct 202 ms 38236 KB Output is correct
51 Correct 184 ms 38056 KB Output is correct
52 Correct 176 ms 37952 KB Output is correct
53 Correct 180 ms 37944 KB Output is correct
54 Correct 145 ms 30916 KB Output is correct
55 Correct 191 ms 38960 KB Output is correct
56 Correct 191 ms 37984 KB Output is correct
57 Correct 236 ms 39212 KB Output is correct
58 Correct 209 ms 38812 KB Output is correct
59 Correct 89 ms 16404 KB Output is correct
60 Correct 254 ms 37604 KB Output is correct
61 Correct 206 ms 37756 KB Output is correct
62 Correct 239 ms 38424 KB Output is correct
63 Correct 291 ms 39472 KB Output is correct
64 Correct 2 ms 9048 KB Output is correct
65 Correct 2 ms 9048 KB Output is correct
66 Correct 2 ms 9052 KB Output is correct
67 Correct 2 ms 9052 KB Output is correct
68 Correct 2 ms 9052 KB Output is correct
69 Correct 2 ms 9052 KB Output is correct
70 Correct 3 ms 9052 KB Output is correct
71 Correct 2 ms 9052 KB Output is correct
72 Correct 2 ms 9052 KB Output is correct
73 Correct 2 ms 9052 KB Output is correct
74 Correct 80 ms 32348 KB Output is correct
75 Correct 73 ms 33220 KB Output is correct
76 Correct 71 ms 33220 KB Output is correct
77 Correct 77 ms 34248 KB Output is correct
78 Correct 51 ms 17504 KB Output is correct
79 Correct 41 ms 15812 KB Output is correct
80 Correct 66 ms 29128 KB Output is correct
81 Correct 95 ms 37492 KB Output is correct
82 Correct 119 ms 42188 KB Output is correct
83 Correct 105 ms 38840 KB Output is correct
84 Correct 68 ms 34500 KB Output is correct
85 Correct 116 ms 38144 KB Output is correct
86 Correct 53 ms 19152 KB Output is correct
87 Correct 192 ms 37932 KB Output is correct
88 Correct 202 ms 37928 KB Output is correct
89 Correct 193 ms 38612 KB Output is correct
90 Correct 138 ms 21768 KB Output is correct
91 Correct 150 ms 24808 KB Output is correct
92 Correct 172 ms 34092 KB Output is correct
93 Correct 207 ms 41468 KB Output is correct
94 Correct 238 ms 44560 KB Output is correct
95 Correct 235 ms 42684 KB Output is correct
96 Correct 243 ms 39212 KB Output is correct
97 Correct 224 ms 41604 KB Output is correct