Submission #298660

# Submission time Handle Problem Language Result Execution time Memory
298660 2020-09-13T17:58:33 Z Bruteforceman Swapping Cities (APIO20_swap) C++11
100 / 100
766 ms 80632 KB
#include "bits/stdc++.h"
#include "swap.h"
using namespace std;
const int maxn = 3e5 + 10;
const int logn = 19;
typedef pair <int, int> pii;
vector <int> g[maxn];
int l[maxn], r[maxn], w[maxn];
int n, m;

int par[maxn];
int deg[maxn];
int dad[maxn];
bool good[maxn];
multiset <int> cont[maxn];
int anc[logn + 1][maxn];
int opt[maxn], dep[maxn];

struct dsu {
  int par[maxn];
  void init(int sz) {
    for(int i = 0; i < sz; i++) {
      par[i] = i;
    }
  }
  int root(int x) {
    if(par[x] == x) return par[x];
    return par[x] = root(par[x]);
  }
  void join(int x, int y) {
    x = root(x);
    y = root(y);
    if(x != y) {
      par[x] = y;
    }
  }
} T;

int root(int x) {
  if(par[x] == x) return x;
  return par[x] = root(par[x]);
}
void remove(int x) {
  cont[root(x)].erase(cont[root(x)].find(deg[x]));
}
void add(int x) {
  cont[root(x)].insert(deg[x]);
}
void join(int x, int y) {
  remove(x);
  remove(y);
  deg[x] += 1;
  deg[y] += 1;
  add(x);
  add(y);
  x = root(x);
  y = root(y);
  if(x != y) {
    if(cont[x].size() > cont[y].size()) swap(x, y);
    for(int i : cont[x]) cont[y].insert(i);
    cont[x].clear();
    par[x] = y;
  }
}
bool check(int x, int y) {
  x = root(x);
  y = root(y);
  if(x != y) return false;
  return not (*cont[x].begin() <= 1 && *cont[x].rbegin() <= 2);
}

void dfs(int x) {
  if(good[x]) opt[x] = w[x - n];
  for(int i : g[x]) {
    dep[i] = 1 + dep[x];
    opt[i] = min(opt[i], opt[x]);
    dfs(i);
  }
}
int lca(int x, int y) {
  if(dep[x] > dep[y]) swap(x, y);
  for(int i = logn; i >= 0; i--) {
    if(dep[y] - (1 << i) >= dep[x]) {
      y = anc[i][y];
    }
  }
  if(x == y) return x;
  for(int i = logn; i >= 0; i--) {
    if(anc[i][x] != anc[i][y]) {
      x = anc[i][x];
      y = anc[i][y];
    }
  }
  return anc[0][x];
}
int getMinimumFuelCapacity(int X, int Y) {
  if(good[lca(X, Y)]) return opt[lca(X, Y)];
  else return opt[X] == INT_MAX ? -1 : opt[X];
}
void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
  n = N;
  m = M;
  vector <int> ord (m);
  for(int i = 0; i < m; i++) {
    ord[i] = i;
  }
  sort(ord.begin(), ord.end(), [&] (int i, int j) {
      return W[i] < W[j]; } );
  for(int i = 0; i < m; i++) {
    l[i] = U[ord[i]];
    r[i] = V[ord[i]];
    w[i] = W[ord[i]]; 
  }
  T.init(n + m);
  for(int i = 0; i < n; i++) {
    par[i] = i;
    deg[i] = 0;
    cont[i].clear();
    cont[i].insert(0);
  }
  for(int i = 0; i < m; i++) {
    int p = l[i];
    int q = r[i];
    if(root(p) != root(q)) {
      join(p, q);
      dad[T.root(p)] = i + n;
      dad[T.root(q)] = i + n;
      good[i + n] = check(p, q);
      T.join(p, i + n);
      T.join(q, i + n);
    } else {
      join(p, q);
      dad[T.root(p)] = i + n;
      good[i + n] = check(p, q);
      T.join(p, i + n);
    }
  }
  int root = n + m - 1;
  dad[root] = root;
  for(int i = 0; i < n + m; i++) {
    anc[0][i] = dad[i];
    opt[i] = INT_MAX;
    if(i != root) g[dad[i]].push_back(i);
  }
  for(int i = 1; i <= logn; i++) {
    for(int j = 0; j < n + m; j++) {
      anc[i][j] = anc[i - 1][anc[i - 1][j]];
    }
  }
  dep[root] = 0;
  dfs(root);
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21632 KB Output is correct
2 Correct 14 ms 21632 KB Output is correct
3 Correct 13 ms 21632 KB Output is correct
4 Correct 14 ms 21760 KB Output is correct
5 Correct 16 ms 22016 KB Output is correct
6 Correct 15 ms 22016 KB Output is correct
7 Correct 16 ms 22016 KB Output is correct
8 Correct 15 ms 22036 KB Output is correct
9 Correct 262 ms 48152 KB Output is correct
10 Correct 325 ms 54128 KB Output is correct
11 Correct 309 ms 53496 KB Output is correct
12 Correct 348 ms 55328 KB Output is correct
13 Correct 256 ms 57064 KB Output is correct
14 Correct 305 ms 48504 KB Output is correct
15 Correct 646 ms 58232 KB Output is correct
16 Correct 681 ms 57208 KB Output is correct
17 Correct 635 ms 59256 KB Output is correct
18 Correct 536 ms 60792 KB Output is correct
19 Correct 139 ms 32120 KB Output is correct
20 Correct 556 ms 59256 KB Output is correct
21 Correct 569 ms 58436 KB Output is correct
22 Correct 659 ms 60756 KB Output is correct
23 Correct 577 ms 62040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21632 KB Output is correct
2 Correct 14 ms 21632 KB Output is correct
3 Correct 494 ms 60808 KB Output is correct
4 Correct 486 ms 62420 KB Output is correct
5 Correct 522 ms 61592 KB Output is correct
6 Correct 488 ms 62164 KB Output is correct
7 Correct 499 ms 62088 KB Output is correct
8 Correct 478 ms 60596 KB Output is correct
9 Correct 488 ms 61684 KB Output is correct
10 Correct 477 ms 60296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 21632 KB Output is correct
2 Correct 13 ms 21632 KB Output is correct
3 Correct 14 ms 21632 KB Output is correct
4 Correct 13 ms 21632 KB Output is correct
5 Correct 14 ms 21760 KB Output is correct
6 Correct 16 ms 22016 KB Output is correct
7 Correct 15 ms 22016 KB Output is correct
8 Correct 16 ms 22016 KB Output is correct
9 Correct 15 ms 22036 KB Output is correct
10 Correct 17 ms 22016 KB Output is correct
11 Correct 16 ms 22016 KB Output is correct
12 Correct 16 ms 22016 KB Output is correct
13 Correct 16 ms 21888 KB Output is correct
14 Correct 16 ms 22016 KB Output is correct
15 Correct 17 ms 22016 KB Output is correct
16 Correct 16 ms 22016 KB Output is correct
17 Correct 16 ms 22016 KB Output is correct
18 Correct 16 ms 22016 KB Output is correct
19 Correct 16 ms 21888 KB Output is correct
20 Correct 16 ms 22016 KB Output is correct
21 Correct 17 ms 22272 KB Output is correct
22 Correct 16 ms 22016 KB Output is correct
23 Correct 16 ms 21888 KB Output is correct
24 Correct 17 ms 22144 KB Output is correct
25 Correct 17 ms 22144 KB Output is correct
26 Correct 17 ms 22144 KB Output is correct
27 Correct 16 ms 22144 KB Output is correct
28 Correct 17 ms 22144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 21632 KB Output is correct
2 Correct 13 ms 21632 KB Output is correct
3 Correct 14 ms 21632 KB Output is correct
4 Correct 13 ms 21632 KB Output is correct
5 Correct 14 ms 21760 KB Output is correct
6 Correct 16 ms 22016 KB Output is correct
7 Correct 15 ms 22016 KB Output is correct
8 Correct 16 ms 22016 KB Output is correct
9 Correct 15 ms 22036 KB Output is correct
10 Correct 262 ms 48152 KB Output is correct
11 Correct 325 ms 54128 KB Output is correct
12 Correct 309 ms 53496 KB Output is correct
13 Correct 348 ms 55328 KB Output is correct
14 Correct 256 ms 57064 KB Output is correct
15 Correct 17 ms 22016 KB Output is correct
16 Correct 16 ms 22016 KB Output is correct
17 Correct 16 ms 22016 KB Output is correct
18 Correct 16 ms 21888 KB Output is correct
19 Correct 16 ms 22016 KB Output is correct
20 Correct 17 ms 22016 KB Output is correct
21 Correct 16 ms 22016 KB Output is correct
22 Correct 16 ms 22016 KB Output is correct
23 Correct 16 ms 22016 KB Output is correct
24 Correct 40 ms 26232 KB Output is correct
25 Correct 329 ms 55152 KB Output is correct
26 Correct 311 ms 55032 KB Output is correct
27 Correct 312 ms 55160 KB Output is correct
28 Correct 293 ms 54648 KB Output is correct
29 Correct 316 ms 54520 KB Output is correct
30 Correct 300 ms 51988 KB Output is correct
31 Correct 352 ms 55384 KB Output is correct
32 Correct 333 ms 55160 KB Output is correct
33 Correct 228 ms 57208 KB Output is correct
34 Correct 322 ms 55288 KB Output is correct
35 Correct 16 ms 21888 KB Output is correct
36 Correct 16 ms 22016 KB Output is correct
37 Correct 17 ms 22272 KB Output is correct
38 Correct 16 ms 22016 KB Output is correct
39 Correct 16 ms 21888 KB Output is correct
40 Correct 17 ms 22144 KB Output is correct
41 Correct 17 ms 22144 KB Output is correct
42 Correct 17 ms 22144 KB Output is correct
43 Correct 16 ms 22144 KB Output is correct
44 Correct 17 ms 22144 KB Output is correct
45 Correct 346 ms 63736 KB Output is correct
46 Correct 316 ms 55032 KB Output is correct
47 Correct 286 ms 55032 KB Output is correct
48 Correct 282 ms 57848 KB Output is correct
49 Correct 211 ms 55544 KB Output is correct
50 Correct 196 ms 48632 KB Output is correct
51 Correct 281 ms 57720 KB Output is correct
52 Correct 424 ms 68728 KB Output is correct
53 Correct 426 ms 71672 KB Output is correct
54 Correct 508 ms 76792 KB Output is correct
55 Correct 221 ms 57080 KB Output is correct
56 Correct 423 ms 71908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21632 KB Output is correct
2 Correct 14 ms 21632 KB Output is correct
3 Correct 13 ms 21632 KB Output is correct
4 Correct 14 ms 21760 KB Output is correct
5 Correct 16 ms 22016 KB Output is correct
6 Correct 15 ms 22016 KB Output is correct
7 Correct 16 ms 22016 KB Output is correct
8 Correct 15 ms 22036 KB Output is correct
9 Correct 262 ms 48152 KB Output is correct
10 Correct 325 ms 54128 KB Output is correct
11 Correct 309 ms 53496 KB Output is correct
12 Correct 348 ms 55328 KB Output is correct
13 Correct 256 ms 57064 KB Output is correct
14 Correct 305 ms 48504 KB Output is correct
15 Correct 646 ms 58232 KB Output is correct
16 Correct 681 ms 57208 KB Output is correct
17 Correct 635 ms 59256 KB Output is correct
18 Correct 536 ms 60792 KB Output is correct
19 Correct 494 ms 60808 KB Output is correct
20 Correct 486 ms 62420 KB Output is correct
21 Correct 522 ms 61592 KB Output is correct
22 Correct 488 ms 62164 KB Output is correct
23 Correct 499 ms 62088 KB Output is correct
24 Correct 478 ms 60596 KB Output is correct
25 Correct 488 ms 61684 KB Output is correct
26 Correct 477 ms 60296 KB Output is correct
27 Correct 17 ms 22016 KB Output is correct
28 Correct 16 ms 22016 KB Output is correct
29 Correct 16 ms 22016 KB Output is correct
30 Correct 16 ms 21888 KB Output is correct
31 Correct 16 ms 22016 KB Output is correct
32 Correct 17 ms 22016 KB Output is correct
33 Correct 16 ms 22016 KB Output is correct
34 Correct 16 ms 22016 KB Output is correct
35 Correct 16 ms 22016 KB Output is correct
36 Correct 40 ms 26232 KB Output is correct
37 Correct 329 ms 55152 KB Output is correct
38 Correct 311 ms 55032 KB Output is correct
39 Correct 312 ms 55160 KB Output is correct
40 Correct 293 ms 54648 KB Output is correct
41 Correct 316 ms 54520 KB Output is correct
42 Correct 300 ms 51988 KB Output is correct
43 Correct 352 ms 55384 KB Output is correct
44 Correct 333 ms 55160 KB Output is correct
45 Correct 228 ms 57208 KB Output is correct
46 Correct 322 ms 55288 KB Output is correct
47 Correct 51 ms 26360 KB Output is correct
48 Correct 530 ms 59604 KB Output is correct
49 Correct 492 ms 59604 KB Output is correct
50 Correct 500 ms 59604 KB Output is correct
51 Correct 486 ms 59364 KB Output is correct
52 Correct 450 ms 57396 KB Output is correct
53 Correct 341 ms 49768 KB Output is correct
54 Correct 519 ms 60456 KB Output is correct
55 Correct 526 ms 59784 KB Output is correct
56 Correct 592 ms 61268 KB Output is correct
57 Correct 600 ms 60372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 21632 KB Output is correct
2 Correct 13 ms 21632 KB Output is correct
3 Correct 14 ms 21632 KB Output is correct
4 Correct 13 ms 21632 KB Output is correct
5 Correct 14 ms 21760 KB Output is correct
6 Correct 16 ms 22016 KB Output is correct
7 Correct 15 ms 22016 KB Output is correct
8 Correct 16 ms 22016 KB Output is correct
9 Correct 15 ms 22036 KB Output is correct
10 Correct 262 ms 48152 KB Output is correct
11 Correct 325 ms 54128 KB Output is correct
12 Correct 309 ms 53496 KB Output is correct
13 Correct 348 ms 55328 KB Output is correct
14 Correct 256 ms 57064 KB Output is correct
15 Correct 305 ms 48504 KB Output is correct
16 Correct 646 ms 58232 KB Output is correct
17 Correct 681 ms 57208 KB Output is correct
18 Correct 635 ms 59256 KB Output is correct
19 Correct 536 ms 60792 KB Output is correct
20 Correct 494 ms 60808 KB Output is correct
21 Correct 486 ms 62420 KB Output is correct
22 Correct 522 ms 61592 KB Output is correct
23 Correct 488 ms 62164 KB Output is correct
24 Correct 499 ms 62088 KB Output is correct
25 Correct 478 ms 60596 KB Output is correct
26 Correct 488 ms 61684 KB Output is correct
27 Correct 477 ms 60296 KB Output is correct
28 Correct 17 ms 22016 KB Output is correct
29 Correct 16 ms 22016 KB Output is correct
30 Correct 16 ms 22016 KB Output is correct
31 Correct 16 ms 21888 KB Output is correct
32 Correct 16 ms 22016 KB Output is correct
33 Correct 17 ms 22016 KB Output is correct
34 Correct 16 ms 22016 KB Output is correct
35 Correct 16 ms 22016 KB Output is correct
36 Correct 16 ms 22016 KB Output is correct
37 Correct 40 ms 26232 KB Output is correct
38 Correct 329 ms 55152 KB Output is correct
39 Correct 311 ms 55032 KB Output is correct
40 Correct 312 ms 55160 KB Output is correct
41 Correct 293 ms 54648 KB Output is correct
42 Correct 316 ms 54520 KB Output is correct
43 Correct 300 ms 51988 KB Output is correct
44 Correct 352 ms 55384 KB Output is correct
45 Correct 333 ms 55160 KB Output is correct
46 Correct 228 ms 57208 KB Output is correct
47 Correct 322 ms 55288 KB Output is correct
48 Correct 51 ms 26360 KB Output is correct
49 Correct 530 ms 59604 KB Output is correct
50 Correct 492 ms 59604 KB Output is correct
51 Correct 500 ms 59604 KB Output is correct
52 Correct 486 ms 59364 KB Output is correct
53 Correct 450 ms 57396 KB Output is correct
54 Correct 341 ms 49768 KB Output is correct
55 Correct 519 ms 60456 KB Output is correct
56 Correct 526 ms 59784 KB Output is correct
57 Correct 592 ms 61268 KB Output is correct
58 Correct 600 ms 60372 KB Output is correct
59 Correct 139 ms 32120 KB Output is correct
60 Correct 556 ms 59256 KB Output is correct
61 Correct 569 ms 58436 KB Output is correct
62 Correct 659 ms 60756 KB Output is correct
63 Correct 577 ms 62040 KB Output is correct
64 Correct 16 ms 21888 KB Output is correct
65 Correct 16 ms 22016 KB Output is correct
66 Correct 17 ms 22272 KB Output is correct
67 Correct 16 ms 22016 KB Output is correct
68 Correct 16 ms 21888 KB Output is correct
69 Correct 17 ms 22144 KB Output is correct
70 Correct 17 ms 22144 KB Output is correct
71 Correct 17 ms 22144 KB Output is correct
72 Correct 16 ms 22144 KB Output is correct
73 Correct 17 ms 22144 KB Output is correct
74 Correct 346 ms 63736 KB Output is correct
75 Correct 316 ms 55032 KB Output is correct
76 Correct 286 ms 55032 KB Output is correct
77 Correct 282 ms 57848 KB Output is correct
78 Correct 211 ms 55544 KB Output is correct
79 Correct 196 ms 48632 KB Output is correct
80 Correct 281 ms 57720 KB Output is correct
81 Correct 424 ms 68728 KB Output is correct
82 Correct 426 ms 71672 KB Output is correct
83 Correct 508 ms 76792 KB Output is correct
84 Correct 221 ms 57080 KB Output is correct
85 Correct 423 ms 71908 KB Output is correct
86 Correct 164 ms 37368 KB Output is correct
87 Correct 511 ms 59476 KB Output is correct
88 Correct 516 ms 59676 KB Output is correct
89 Correct 729 ms 60376 KB Output is correct
90 Correct 349 ms 61048 KB Output is correct
91 Correct 403 ms 58964 KB Output is correct
92 Correct 595 ms 61176 KB Output is correct
93 Correct 621 ms 72260 KB Output is correct
94 Correct 729 ms 75332 KB Output is correct
95 Correct 763 ms 80632 KB Output is correct
96 Correct 570 ms 61528 KB Output is correct
97 Correct 766 ms 68260 KB Output is correct