Submission #732534

# Submission time Handle Problem Language Result Execution time Memory
732534 2023-04-29T05:29:47 Z Magikarp4000 Swapping Cities (APIO20_swap) C++17
100 / 100
453 ms 46912 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n' 
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;

const int MAXN = 3e5+10, LOG = 21;
int n,m;
vector<int> v[MAXN];
vector<pair<int,PII>> e;
int p[MAXN], sz[MAXN], x[MAXN], deg[MAXN], p1[MAXN];
int jump[MAXN][LOG], depth[MAXN];
bool good[MAXN];
int cur;

int finds(int a) {
  if (p[a] != a) p[a] = finds(p[a]);
  return p[a];
}

void unite(int a, int b, int w) {
  int oa = a, ob = b;
  a = finds(a);
  b = finds(b);
  deg[oa]++; deg[ob]++;
  if (a != b) {
    p[a] = p[b] = p1[a] = p1[b] = cur;
    v[cur].PB(a);
    v[cur].PB(b);
    sz[cur] += sz[a]+sz[b];
    x[cur] = w;
    if (good[a] || good[b]) good[cur] = 1;
    if (deg[oa] >= 3 || deg[ob] >= 3) good[cur] = 1;
    cur++;
  }
  else {
    if (good[a]) return;
    good[a] = 1;
    x[a] = w;
  }
}

void dfs(int s, int pa) {
  FORX(u,v[s]) {
    if (u == pa) continue;
    depth[u] = depth[s]+1;
    dfs(u,s);
  }
}

int lca(int a, int b) {
    if (depth[a] < depth[b]) swap(a,b);
    FORR(j,LOG-1,-1) {
        if (jump[a][j] && depth[jump[a][j]] >= depth[b]) {
            a = jump[a][j];
        }
    }
    if (a == b && good[a]) return a;
    FORR(j,LOG-1,-1) {
        if (jump[a][j] != jump[b][j]) {
            a = jump[a][j];
            b = jump[b][j];
        }
    }
    a = p1[a];
    if (good[a]) return a;
    FORR(j,LOG-1,-1) {
      if (jump[a][j] && !good[jump[a][j]]) a = jump[a][j];
    }
    a = p1[a];
    return a;
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
  n = N; m = M;
  FOR(i,0,m) e.PB({W[i],{U[i]+1,V[i]+1}});
  sort(ALL(e));
  cur = n+1;
  FOR(i,1,MAXN) {
    p[i] = p1[i] = i;
    sz[i] = 1;
  }
  FORX(u,e) {
    unite(u.S.F, u.S.S, u.F);
  }
  dfs(cur-1,-1);
  FOR(i,1,cur) jump[i][0] = p1[i];
  FOR(j,1,LOG) {
    FOR(i,1,cur) jump[i][j] = jump[jump[i][j-1]][j-1];
  }
}

int getMinimumFuelCapacity(int a, int b) {
  a++; b++;
  int anc = lca(a,b);
  if (!good[anc]) return -1;
  return x[anc];
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 7 ms 10836 KB Output is correct
3 Correct 7 ms 10792 KB Output is correct
4 Correct 7 ms 10920 KB Output is correct
5 Correct 7 ms 11092 KB Output is correct
6 Correct 6 ms 11092 KB Output is correct
7 Correct 9 ms 11120 KB Output is correct
8 Correct 6 ms 11092 KB Output is correct
9 Correct 82 ms 30148 KB Output is correct
10 Correct 110 ms 34496 KB Output is correct
11 Correct 107 ms 34108 KB Output is correct
12 Correct 116 ms 35560 KB Output is correct
13 Correct 112 ms 37184 KB Output is correct
14 Correct 115 ms 30352 KB Output is correct
15 Correct 300 ms 36480 KB Output is correct
16 Correct 311 ms 35808 KB Output is correct
17 Correct 330 ms 37256 KB Output is correct
18 Correct 453 ms 38980 KB Output is correct
19 Correct 119 ms 18200 KB Output is correct
20 Correct 282 ms 37800 KB Output is correct
21 Correct 290 ms 37024 KB Output is correct
22 Correct 304 ms 38668 KB Output is correct
23 Correct 430 ms 40292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 7 ms 10836 KB Output is correct
3 Correct 380 ms 40444 KB Output is correct
4 Correct 376 ms 42988 KB Output is correct
5 Correct 378 ms 42276 KB Output is correct
6 Correct 336 ms 42848 KB Output is correct
7 Correct 358 ms 42680 KB Output is correct
8 Correct 336 ms 41544 KB Output is correct
9 Correct 352 ms 42360 KB Output is correct
10 Correct 331 ms 41260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 7 ms 10836 KB Output is correct
3 Correct 7 ms 10792 KB Output is correct
4 Correct 7 ms 10920 KB Output is correct
5 Correct 7 ms 11092 KB Output is correct
6 Correct 6 ms 11092 KB Output is correct
7 Correct 9 ms 11120 KB Output is correct
8 Correct 6 ms 11092 KB Output is correct
9 Correct 6 ms 10836 KB Output is correct
10 Correct 7 ms 11020 KB Output is correct
11 Correct 7 ms 11072 KB Output is correct
12 Correct 7 ms 11140 KB Output is correct
13 Correct 7 ms 11068 KB Output is correct
14 Correct 7 ms 11072 KB Output is correct
15 Correct 7 ms 11092 KB Output is correct
16 Correct 8 ms 11092 KB Output is correct
17 Correct 7 ms 11092 KB Output is correct
18 Correct 7 ms 11076 KB Output is correct
19 Correct 9 ms 10988 KB Output is correct
20 Correct 8 ms 11092 KB Output is correct
21 Correct 7 ms 11092 KB Output is correct
22 Correct 8 ms 10948 KB Output is correct
23 Correct 8 ms 11056 KB Output is correct
24 Correct 9 ms 11200 KB Output is correct
25 Correct 9 ms 11092 KB Output is correct
26 Correct 9 ms 11200 KB Output is correct
27 Correct 8 ms 11092 KB Output is correct
28 Correct 9 ms 11124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 6 ms 10836 KB Output is correct
3 Correct 7 ms 10836 KB Output is correct
4 Correct 7 ms 10792 KB Output is correct
5 Correct 7 ms 10920 KB Output is correct
6 Correct 7 ms 11092 KB Output is correct
7 Correct 6 ms 11092 KB Output is correct
8 Correct 9 ms 11120 KB Output is correct
9 Correct 6 ms 11092 KB Output is correct
10 Correct 82 ms 30148 KB Output is correct
11 Correct 110 ms 34496 KB Output is correct
12 Correct 107 ms 34108 KB Output is correct
13 Correct 116 ms 35560 KB Output is correct
14 Correct 112 ms 37184 KB Output is correct
15 Correct 7 ms 11020 KB Output is correct
16 Correct 7 ms 11072 KB Output is correct
17 Correct 7 ms 11140 KB Output is correct
18 Correct 7 ms 11068 KB Output is correct
19 Correct 7 ms 11072 KB Output is correct
20 Correct 7 ms 11092 KB Output is correct
21 Correct 8 ms 11092 KB Output is correct
22 Correct 7 ms 11092 KB Output is correct
23 Correct 7 ms 11076 KB Output is correct
24 Correct 9 ms 10988 KB Output is correct
25 Correct 8 ms 11092 KB Output is correct
26 Correct 7 ms 11092 KB Output is correct
27 Correct 8 ms 10948 KB Output is correct
28 Correct 8 ms 11056 KB Output is correct
29 Correct 9 ms 11200 KB Output is correct
30 Correct 9 ms 11092 KB Output is correct
31 Correct 9 ms 11200 KB Output is correct
32 Correct 8 ms 11092 KB Output is correct
33 Correct 9 ms 11124 KB Output is correct
34 Correct 21 ms 14476 KB Output is correct
35 Correct 136 ms 37416 KB Output is correct
36 Correct 140 ms 37424 KB Output is correct
37 Correct 131 ms 37424 KB Output is correct
38 Correct 124 ms 37184 KB Output is correct
39 Correct 128 ms 36868 KB Output is correct
40 Correct 138 ms 34728 KB Output is correct
41 Correct 124 ms 37564 KB Output is correct
42 Correct 124 ms 37384 KB Output is correct
43 Correct 104 ms 39608 KB Output is correct
44 Correct 125 ms 37672 KB Output is correct
45 Correct 131 ms 36564 KB Output is correct
46 Correct 109 ms 37304 KB Output is correct
47 Correct 115 ms 37408 KB Output is correct
48 Correct 121 ms 38324 KB Output is correct
49 Correct 65 ms 19524 KB Output is correct
50 Correct 60 ms 19008 KB Output is correct
51 Correct 109 ms 32420 KB Output is correct
52 Correct 159 ms 41128 KB Output is correct
53 Correct 153 ms 42616 KB Output is correct
54 Correct 153 ms 42972 KB Output is correct
55 Correct 109 ms 39460 KB Output is correct
56 Correct 164 ms 42732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 7 ms 10836 KB Output is correct
3 Correct 7 ms 10792 KB Output is correct
4 Correct 7 ms 10920 KB Output is correct
5 Correct 7 ms 11092 KB Output is correct
6 Correct 6 ms 11092 KB Output is correct
7 Correct 9 ms 11120 KB Output is correct
8 Correct 6 ms 11092 KB Output is correct
9 Correct 82 ms 30148 KB Output is correct
10 Correct 110 ms 34496 KB Output is correct
11 Correct 107 ms 34108 KB Output is correct
12 Correct 116 ms 35560 KB Output is correct
13 Correct 112 ms 37184 KB Output is correct
14 Correct 115 ms 30352 KB Output is correct
15 Correct 300 ms 36480 KB Output is correct
16 Correct 311 ms 35808 KB Output is correct
17 Correct 330 ms 37256 KB Output is correct
18 Correct 453 ms 38980 KB Output is correct
19 Correct 380 ms 40444 KB Output is correct
20 Correct 376 ms 42988 KB Output is correct
21 Correct 378 ms 42276 KB Output is correct
22 Correct 336 ms 42848 KB Output is correct
23 Correct 358 ms 42680 KB Output is correct
24 Correct 336 ms 41544 KB Output is correct
25 Correct 352 ms 42360 KB Output is correct
26 Correct 331 ms 41260 KB Output is correct
27 Correct 7 ms 11020 KB Output is correct
28 Correct 7 ms 11072 KB Output is correct
29 Correct 7 ms 11140 KB Output is correct
30 Correct 7 ms 11068 KB Output is correct
31 Correct 7 ms 11072 KB Output is correct
32 Correct 7 ms 11092 KB Output is correct
33 Correct 8 ms 11092 KB Output is correct
34 Correct 7 ms 11092 KB Output is correct
35 Correct 7 ms 11076 KB Output is correct
36 Correct 21 ms 14476 KB Output is correct
37 Correct 136 ms 37416 KB Output is correct
38 Correct 140 ms 37424 KB Output is correct
39 Correct 131 ms 37424 KB Output is correct
40 Correct 124 ms 37184 KB Output is correct
41 Correct 128 ms 36868 KB Output is correct
42 Correct 138 ms 34728 KB Output is correct
43 Correct 124 ms 37564 KB Output is correct
44 Correct 124 ms 37384 KB Output is correct
45 Correct 104 ms 39608 KB Output is correct
46 Correct 125 ms 37672 KB Output is correct
47 Correct 27 ms 14688 KB Output is correct
48 Correct 284 ms 42160 KB Output is correct
49 Correct 258 ms 42252 KB Output is correct
50 Correct 262 ms 42280 KB Output is correct
51 Correct 272 ms 42148 KB Output is correct
52 Correct 244 ms 40588 KB Output is correct
53 Correct 227 ms 34504 KB Output is correct
54 Correct 255 ms 43040 KB Output is correct
55 Correct 283 ms 42256 KB Output is correct
56 Correct 369 ms 44088 KB Output is correct
57 Correct 270 ms 43312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 6 ms 10836 KB Output is correct
3 Correct 7 ms 10836 KB Output is correct
4 Correct 7 ms 10792 KB Output is correct
5 Correct 7 ms 10920 KB Output is correct
6 Correct 7 ms 11092 KB Output is correct
7 Correct 6 ms 11092 KB Output is correct
8 Correct 9 ms 11120 KB Output is correct
9 Correct 6 ms 11092 KB Output is correct
10 Correct 82 ms 30148 KB Output is correct
11 Correct 110 ms 34496 KB Output is correct
12 Correct 107 ms 34108 KB Output is correct
13 Correct 116 ms 35560 KB Output is correct
14 Correct 112 ms 37184 KB Output is correct
15 Correct 115 ms 30352 KB Output is correct
16 Correct 300 ms 36480 KB Output is correct
17 Correct 311 ms 35808 KB Output is correct
18 Correct 330 ms 37256 KB Output is correct
19 Correct 453 ms 38980 KB Output is correct
20 Correct 380 ms 40444 KB Output is correct
21 Correct 376 ms 42988 KB Output is correct
22 Correct 378 ms 42276 KB Output is correct
23 Correct 336 ms 42848 KB Output is correct
24 Correct 358 ms 42680 KB Output is correct
25 Correct 336 ms 41544 KB Output is correct
26 Correct 352 ms 42360 KB Output is correct
27 Correct 331 ms 41260 KB Output is correct
28 Correct 7 ms 11020 KB Output is correct
29 Correct 7 ms 11072 KB Output is correct
30 Correct 7 ms 11140 KB Output is correct
31 Correct 7 ms 11068 KB Output is correct
32 Correct 7 ms 11072 KB Output is correct
33 Correct 7 ms 11092 KB Output is correct
34 Correct 8 ms 11092 KB Output is correct
35 Correct 7 ms 11092 KB Output is correct
36 Correct 7 ms 11076 KB Output is correct
37 Correct 21 ms 14476 KB Output is correct
38 Correct 136 ms 37416 KB Output is correct
39 Correct 140 ms 37424 KB Output is correct
40 Correct 131 ms 37424 KB Output is correct
41 Correct 124 ms 37184 KB Output is correct
42 Correct 128 ms 36868 KB Output is correct
43 Correct 138 ms 34728 KB Output is correct
44 Correct 124 ms 37564 KB Output is correct
45 Correct 124 ms 37384 KB Output is correct
46 Correct 104 ms 39608 KB Output is correct
47 Correct 125 ms 37672 KB Output is correct
48 Correct 27 ms 14688 KB Output is correct
49 Correct 284 ms 42160 KB Output is correct
50 Correct 258 ms 42252 KB Output is correct
51 Correct 262 ms 42280 KB Output is correct
52 Correct 272 ms 42148 KB Output is correct
53 Correct 244 ms 40588 KB Output is correct
54 Correct 227 ms 34504 KB Output is correct
55 Correct 255 ms 43040 KB Output is correct
56 Correct 283 ms 42256 KB Output is correct
57 Correct 369 ms 44088 KB Output is correct
58 Correct 270 ms 43312 KB Output is correct
59 Correct 119 ms 18200 KB Output is correct
60 Correct 282 ms 37800 KB Output is correct
61 Correct 290 ms 37024 KB Output is correct
62 Correct 304 ms 38668 KB Output is correct
63 Correct 430 ms 40292 KB Output is correct
64 Correct 9 ms 10988 KB Output is correct
65 Correct 8 ms 11092 KB Output is correct
66 Correct 7 ms 11092 KB Output is correct
67 Correct 8 ms 10948 KB Output is correct
68 Correct 8 ms 11056 KB Output is correct
69 Correct 9 ms 11200 KB Output is correct
70 Correct 9 ms 11092 KB Output is correct
71 Correct 9 ms 11200 KB Output is correct
72 Correct 8 ms 11092 KB Output is correct
73 Correct 9 ms 11124 KB Output is correct
74 Correct 131 ms 36564 KB Output is correct
75 Correct 109 ms 37304 KB Output is correct
76 Correct 115 ms 37408 KB Output is correct
77 Correct 121 ms 38324 KB Output is correct
78 Correct 65 ms 19524 KB Output is correct
79 Correct 60 ms 19008 KB Output is correct
80 Correct 109 ms 32420 KB Output is correct
81 Correct 159 ms 41128 KB Output is correct
82 Correct 153 ms 42616 KB Output is correct
83 Correct 153 ms 42972 KB Output is correct
84 Correct 109 ms 39460 KB Output is correct
85 Correct 164 ms 42732 KB Output is correct
86 Correct 71 ms 21668 KB Output is correct
87 Correct 318 ms 42212 KB Output is correct
88 Correct 272 ms 42300 KB Output is correct
89 Correct 321 ms 41524 KB Output is correct
90 Correct 174 ms 23936 KB Output is correct
91 Correct 197 ms 25876 KB Output is correct
92 Correct 270 ms 37316 KB Output is correct
93 Correct 312 ms 45852 KB Output is correct
94 Correct 342 ms 46900 KB Output is correct
95 Correct 335 ms 46912 KB Output is correct
96 Correct 392 ms 44196 KB Output is correct
97 Correct 332 ms 45684 KB Output is correct