Submission #891442

# Submission time Handle Problem Language Result Execution time Memory
891442 2023-12-23T04:13:16 Z Zero_OP Swapping Cities (APIO20_swap) C++14
100 / 100
275 ms 64396 KB
#include<bits/stdc++.h>

using namespace std;

#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))

template<class T> bool minimize(T& a, T b){
  if(a > b) return a = b, true;
  return false;
}

template<class T> bool maximize(T& a, T b){
  if(a < b) return a = b, true;
  return false;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int N = 3e5 + 5;

struct edge{
  int u, v, w;
  bool operator < (const edge& o){
    return w < o.w;
  }
};

int n, m, q, nNodes, par[N], h[N], lift[20][N], weight[N], deg[N], lab[N];
bool good[N];
vector<int> adj[N];
vector<edge> edges;

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

void addEdge(int u, int v, int w, int maxDeg){
  u = root(u), v = root(v);
  par[u] = nNodes;
  par[v] = nNodes;
  weight[nNodes] = w;
  good[nNodes] = good[u] || good[v];
  adj[nNodes].push_back(u);
  if(u == v) good[nNodes] = true;
  else{
    adj[nNodes].push_back(v);
    if(maxDeg > 2) good[nNodes] = true;
  }
  ++nNodes;
}

void dfsLCA(int u){
  for(int v : adj[u]){
    h[v] = h[u] + 1;
    lift[0][v] = u;
    for(int i = 1; i <= 18; ++i){
      lift[i][v] = lift[i - 1][lift[i - 1][v]];
    }
    dfsLCA(v);
  }
}

int getLCA(int u, int v){
  if(h[u] != h[v]){
    if(h[u] < h[v]) swap(u, v);
    for(int i = 18; i >= 0; --i){
      if(h[u] - (1 << i) >= h[v]){
        u = lift[i][u];
      }
    }
  }
  if(u == v) return u;
  for(int i = 18; i >= 0; --i){
    if(lift[i][u] != lift[i][v]){
      u = lift[i][u];
      v = lift[i][v];
    }
  }
  return lift[0][u];
}

int findFirstGood(int u){
  if(good[u]) return u;
  for(int i = 18; i >= 0; --i){
    if(lift[i][u] != -1 and !good[lift[i][u]]){
      u = lift[i][u];
    }
  }
  if(!good[lift[0][u]]) return -1;
  return lift[0][u];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W){
  n = N;
  m = M;
  for(int i = 0; i < m; ++i){
    edges.push_back({U[i], V[i], W[i]});
  }

  memset(lift, -1, sizeof(lift));
  sort(range(edges));
  iota(par, par + n + m, 0);
  nNodes = n;

  for(int i = 0; i < m; ++i){
    int u = edges[i].u, v = edges[i].v, w = edges[i].w;
    ++deg[u]; ++deg[v];
    addEdge(u, v, w, max(deg[u], deg[v]));
  }
  dfsLCA(nNodes - 1);
}

int getMinimumFuelCapacity(int X, int Y){
  int mid = findFirstGood(getLCA(X, Y));
  if(mid == -1) return -1;
  else return weight[mid];
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 5 ms 35512 KB Output is correct
4 Correct 6 ms 35420 KB Output is correct
5 Correct 6 ms 35416 KB Output is correct
6 Correct 7 ms 35420 KB Output is correct
7 Correct 6 ms 35420 KB Output is correct
8 Correct 6 ms 35420 KB Output is correct
9 Correct 63 ms 42952 KB Output is correct
10 Correct 80 ms 44728 KB Output is correct
11 Correct 82 ms 44332 KB Output is correct
12 Correct 86 ms 44996 KB Output is correct
13 Correct 74 ms 48064 KB Output is correct
14 Correct 98 ms 43460 KB Output is correct
15 Correct 162 ms 48976 KB Output is correct
16 Correct 163 ms 48628 KB Output is correct
17 Correct 206 ms 49200 KB Output is correct
18 Correct 229 ms 52272 KB Output is correct
19 Correct 74 ms 42004 KB Output is correct
20 Correct 196 ms 49804 KB Output is correct
21 Correct 156 ms 49924 KB Output is correct
22 Correct 185 ms 50476 KB Output is correct
23 Correct 236 ms 53672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 170 ms 55140 KB Output is correct
4 Correct 173 ms 55856 KB Output is correct
5 Correct 222 ms 55692 KB Output is correct
6 Correct 198 ms 55592 KB Output is correct
7 Correct 208 ms 55844 KB Output is correct
8 Correct 191 ms 55248 KB Output is correct
9 Correct 178 ms 55632 KB Output is correct
10 Correct 164 ms 55112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 5 ms 35512 KB Output is correct
4 Correct 6 ms 35420 KB Output is correct
5 Correct 6 ms 35416 KB Output is correct
6 Correct 7 ms 35420 KB Output is correct
7 Correct 6 ms 35420 KB Output is correct
8 Correct 6 ms 35420 KB Output is correct
9 Correct 6 ms 35420 KB Output is correct
10 Correct 6 ms 35420 KB Output is correct
11 Correct 6 ms 35420 KB Output is correct
12 Correct 6 ms 35420 KB Output is correct
13 Correct 6 ms 35420 KB Output is correct
14 Correct 6 ms 35420 KB Output is correct
15 Correct 6 ms 35568 KB Output is correct
16 Correct 7 ms 35420 KB Output is correct
17 Correct 6 ms 35420 KB Output is correct
18 Correct 6 ms 35416 KB Output is correct
19 Correct 6 ms 35420 KB Output is correct
20 Correct 6 ms 35464 KB Output is correct
21 Correct 6 ms 35416 KB Output is correct
22 Correct 6 ms 35676 KB Output is correct
23 Correct 6 ms 35420 KB Output is correct
24 Correct 6 ms 35676 KB Output is correct
25 Correct 6 ms 35676 KB Output is correct
26 Correct 6 ms 35744 KB Output is correct
27 Correct 6 ms 35420 KB Output is correct
28 Correct 6 ms 35624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35420 KB Output is correct
2 Correct 9 ms 35416 KB Output is correct
3 Correct 5 ms 35420 KB Output is correct
4 Correct 5 ms 35512 KB Output is correct
5 Correct 6 ms 35420 KB Output is correct
6 Correct 6 ms 35416 KB Output is correct
7 Correct 7 ms 35420 KB Output is correct
8 Correct 6 ms 35420 KB Output is correct
9 Correct 6 ms 35420 KB Output is correct
10 Correct 63 ms 42952 KB Output is correct
11 Correct 80 ms 44728 KB Output is correct
12 Correct 82 ms 44332 KB Output is correct
13 Correct 86 ms 44996 KB Output is correct
14 Correct 74 ms 48064 KB Output is correct
15 Correct 6 ms 35420 KB Output is correct
16 Correct 6 ms 35420 KB Output is correct
17 Correct 6 ms 35420 KB Output is correct
18 Correct 6 ms 35420 KB Output is correct
19 Correct 6 ms 35420 KB Output is correct
20 Correct 6 ms 35568 KB Output is correct
21 Correct 7 ms 35420 KB Output is correct
22 Correct 6 ms 35420 KB Output is correct
23 Correct 6 ms 35416 KB Output is correct
24 Correct 6 ms 35420 KB Output is correct
25 Correct 6 ms 35464 KB Output is correct
26 Correct 6 ms 35416 KB Output is correct
27 Correct 6 ms 35676 KB Output is correct
28 Correct 6 ms 35420 KB Output is correct
29 Correct 6 ms 35676 KB Output is correct
30 Correct 6 ms 35676 KB Output is correct
31 Correct 6 ms 35744 KB Output is correct
32 Correct 6 ms 35420 KB Output is correct
33 Correct 6 ms 35624 KB Output is correct
34 Correct 13 ms 36888 KB Output is correct
35 Correct 88 ms 44740 KB Output is correct
36 Correct 83 ms 44748 KB Output is correct
37 Correct 85 ms 44832 KB Output is correct
38 Correct 93 ms 44636 KB Output is correct
39 Correct 89 ms 44976 KB Output is correct
40 Correct 73 ms 43832 KB Output is correct
41 Correct 84 ms 44924 KB Output is correct
42 Correct 81 ms 44740 KB Output is correct
43 Correct 81 ms 48976 KB Output is correct
44 Correct 96 ms 45168 KB Output is correct
45 Correct 114 ms 55228 KB Output is correct
46 Correct 92 ms 44744 KB Output is correct
47 Correct 82 ms 44984 KB Output is correct
48 Correct 92 ms 48240 KB Output is correct
49 Correct 81 ms 59068 KB Output is correct
50 Correct 64 ms 53448 KB Output is correct
51 Correct 84 ms 52812 KB Output is correct
52 Correct 119 ms 54712 KB Output is correct
53 Correct 136 ms 56068 KB Output is correct
54 Correct 143 ms 60892 KB Output is correct
55 Correct 72 ms 48580 KB Output is correct
56 Correct 125 ms 59332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 35416 KB Output is correct
2 Correct 5 ms 35420 KB Output is correct
3 Correct 5 ms 35512 KB Output is correct
4 Correct 6 ms 35420 KB Output is correct
5 Correct 6 ms 35416 KB Output is correct
6 Correct 7 ms 35420 KB Output is correct
7 Correct 6 ms 35420 KB Output is correct
8 Correct 6 ms 35420 KB Output is correct
9 Correct 63 ms 42952 KB Output is correct
10 Correct 80 ms 44728 KB Output is correct
11 Correct 82 ms 44332 KB Output is correct
12 Correct 86 ms 44996 KB Output is correct
13 Correct 74 ms 48064 KB Output is correct
14 Correct 98 ms 43460 KB Output is correct
15 Correct 162 ms 48976 KB Output is correct
16 Correct 163 ms 48628 KB Output is correct
17 Correct 206 ms 49200 KB Output is correct
18 Correct 229 ms 52272 KB Output is correct
19 Correct 170 ms 55140 KB Output is correct
20 Correct 173 ms 55856 KB Output is correct
21 Correct 222 ms 55692 KB Output is correct
22 Correct 198 ms 55592 KB Output is correct
23 Correct 208 ms 55844 KB Output is correct
24 Correct 191 ms 55248 KB Output is correct
25 Correct 178 ms 55632 KB Output is correct
26 Correct 164 ms 55112 KB Output is correct
27 Correct 6 ms 35420 KB Output is correct
28 Correct 6 ms 35420 KB Output is correct
29 Correct 6 ms 35420 KB Output is correct
30 Correct 6 ms 35420 KB Output is correct
31 Correct 6 ms 35420 KB Output is correct
32 Correct 6 ms 35568 KB Output is correct
33 Correct 7 ms 35420 KB Output is correct
34 Correct 6 ms 35420 KB Output is correct
35 Correct 6 ms 35416 KB Output is correct
36 Correct 13 ms 36888 KB Output is correct
37 Correct 88 ms 44740 KB Output is correct
38 Correct 83 ms 44748 KB Output is correct
39 Correct 85 ms 44832 KB Output is correct
40 Correct 93 ms 44636 KB Output is correct
41 Correct 89 ms 44976 KB Output is correct
42 Correct 73 ms 43832 KB Output is correct
43 Correct 84 ms 44924 KB Output is correct
44 Correct 81 ms 44740 KB Output is correct
45 Correct 81 ms 48976 KB Output is correct
46 Correct 96 ms 45168 KB Output is correct
47 Correct 20 ms 37144 KB Output is correct
48 Correct 167 ms 49608 KB Output is correct
49 Correct 160 ms 49708 KB Output is correct
50 Correct 161 ms 49548 KB Output is correct
51 Correct 162 ms 49460 KB Output is correct
52 Correct 154 ms 49000 KB Output is correct
53 Correct 129 ms 47544 KB Output is correct
54 Correct 203 ms 50560 KB Output is correct
55 Correct 163 ms 49660 KB Output is correct
56 Correct 216 ms 52884 KB Output is correct
57 Correct 172 ms 50524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 35420 KB Output is correct
2 Correct 9 ms 35416 KB Output is correct
3 Correct 5 ms 35420 KB Output is correct
4 Correct 5 ms 35512 KB Output is correct
5 Correct 6 ms 35420 KB Output is correct
6 Correct 6 ms 35416 KB Output is correct
7 Correct 7 ms 35420 KB Output is correct
8 Correct 6 ms 35420 KB Output is correct
9 Correct 6 ms 35420 KB Output is correct
10 Correct 63 ms 42952 KB Output is correct
11 Correct 80 ms 44728 KB Output is correct
12 Correct 82 ms 44332 KB Output is correct
13 Correct 86 ms 44996 KB Output is correct
14 Correct 74 ms 48064 KB Output is correct
15 Correct 98 ms 43460 KB Output is correct
16 Correct 162 ms 48976 KB Output is correct
17 Correct 163 ms 48628 KB Output is correct
18 Correct 206 ms 49200 KB Output is correct
19 Correct 229 ms 52272 KB Output is correct
20 Correct 170 ms 55140 KB Output is correct
21 Correct 173 ms 55856 KB Output is correct
22 Correct 222 ms 55692 KB Output is correct
23 Correct 198 ms 55592 KB Output is correct
24 Correct 208 ms 55844 KB Output is correct
25 Correct 191 ms 55248 KB Output is correct
26 Correct 178 ms 55632 KB Output is correct
27 Correct 164 ms 55112 KB Output is correct
28 Correct 6 ms 35420 KB Output is correct
29 Correct 6 ms 35420 KB Output is correct
30 Correct 6 ms 35420 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 35568 KB Output is correct
34 Correct 7 ms 35420 KB Output is correct
35 Correct 6 ms 35420 KB Output is correct
36 Correct 6 ms 35416 KB Output is correct
37 Correct 13 ms 36888 KB Output is correct
38 Correct 88 ms 44740 KB Output is correct
39 Correct 83 ms 44748 KB Output is correct
40 Correct 85 ms 44832 KB Output is correct
41 Correct 93 ms 44636 KB Output is correct
42 Correct 89 ms 44976 KB Output is correct
43 Correct 73 ms 43832 KB Output is correct
44 Correct 84 ms 44924 KB Output is correct
45 Correct 81 ms 44740 KB Output is correct
46 Correct 81 ms 48976 KB Output is correct
47 Correct 96 ms 45168 KB Output is correct
48 Correct 20 ms 37144 KB Output is correct
49 Correct 167 ms 49608 KB Output is correct
50 Correct 160 ms 49708 KB Output is correct
51 Correct 161 ms 49548 KB Output is correct
52 Correct 162 ms 49460 KB Output is correct
53 Correct 154 ms 49000 KB Output is correct
54 Correct 129 ms 47544 KB Output is correct
55 Correct 203 ms 50560 KB Output is correct
56 Correct 163 ms 49660 KB Output is correct
57 Correct 216 ms 52884 KB Output is correct
58 Correct 172 ms 50524 KB Output is correct
59 Correct 74 ms 42004 KB Output is correct
60 Correct 196 ms 49804 KB Output is correct
61 Correct 156 ms 49924 KB Output is correct
62 Correct 185 ms 50476 KB Output is correct
63 Correct 236 ms 53672 KB Output is correct
64 Correct 6 ms 35420 KB Output is correct
65 Correct 6 ms 35464 KB Output is correct
66 Correct 6 ms 35416 KB Output is correct
67 Correct 6 ms 35676 KB Output is correct
68 Correct 6 ms 35420 KB Output is correct
69 Correct 6 ms 35676 KB Output is correct
70 Correct 6 ms 35676 KB Output is correct
71 Correct 6 ms 35744 KB Output is correct
72 Correct 6 ms 35420 KB Output is correct
73 Correct 6 ms 35624 KB Output is correct
74 Correct 114 ms 55228 KB Output is correct
75 Correct 92 ms 44744 KB Output is correct
76 Correct 82 ms 44984 KB Output is correct
77 Correct 92 ms 48240 KB Output is correct
78 Correct 81 ms 59068 KB Output is correct
79 Correct 64 ms 53448 KB Output is correct
80 Correct 84 ms 52812 KB Output is correct
81 Correct 119 ms 54712 KB Output is correct
82 Correct 136 ms 56068 KB Output is correct
83 Correct 143 ms 60892 KB Output is correct
84 Correct 72 ms 48580 KB Output is correct
85 Correct 125 ms 59332 KB Output is correct
86 Correct 61 ms 42876 KB Output is correct
87 Correct 172 ms 49688 KB Output is correct
88 Correct 158 ms 49712 KB Output is correct
89 Correct 217 ms 52540 KB Output is correct
90 Correct 152 ms 64280 KB Output is correct
91 Correct 168 ms 62972 KB Output is correct
92 Correct 239 ms 56992 KB Output is correct
93 Correct 240 ms 60284 KB Output is correct
94 Correct 275 ms 60704 KB Output is correct
95 Correct 243 ms 64396 KB Output is correct
96 Correct 191 ms 53068 KB Output is correct
97 Correct 262 ms 58328 KB Output is correct