Submission #975813

# Submission time Handle Problem Language Result Execution time Memory
975813 2024-05-05T23:23:36 Z OAleksa Swapping Cities (APIO20_swap) C++14
100 / 100
331 ms 40760 KB
#include "swap.h"
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 1e5 + 69;
const int inf = 1e9 + 69;
vector<tuple<int, int, int>> edges;
int n, m, p[N], sz[N], deg[N], lan[N];
vector<pair<int, int>> par[N];
vector<int> who[N];
int root(int v) {
  if (p[v] == v)
    return v;
  return p[v] = root(p[v]);
}
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({w[i], u[i], v[i]});
  sort(edges.begin(), edges.end());
  for (int i = 0;i < n;i++) {
    sz[i] = 1;
    p[i] = i;
    par[i].push_back({0, i});
    who[i].push_back(i);
    lan[i] = inf;
  }
  for (auto u : edges) {
    int c, b, a;
    tie(c, a, b) = u;
    int x = root(a);
    int y = root(b);
    if (x != y) {
      if (sz[x] < sz[y])
        swap(x, y);
      for (auto j : who[y]) {
        par[j].push_back({c, x});
        who[x].push_back(j);
      }
      p[y] = x;
      sz[x] += sz[y];
    }
  }
  for (int i = 0;i < n;i++) {
    p[i] = i;
    sz[i] = 1;
    who[i].clear();
    who[i].push_back(i);
    par[i].push_back({inf, par[i].back().s});
  }
  //lanac i lanac->lanac
  //nelanac i lanac->nelanac
  for (auto u : edges) {
    int a, b, c;
    tie(c, a, b) = u;
    int x = root(a);
    int y = root(b);
    if (x != y) {
      if (lan[x] != inf && lan[y] == inf) {
        for (auto j : who[y])
          lan[j] = c;
        who[y].clear();
      }
      else if (lan[y] != inf && lan[x] == inf) {
        for (auto j : who[x])
          lan[j] = c;
        who[x].clear();
      }
      else if (lan[x] == inf && lan[y] == inf && (deg[a] == 2 || deg[b] == 2)) {
        for (auto j : who[x])
          lan[j] = c;
        who[x].clear();
        for (auto j : who[y])
          lan[j] = c;
        who[y].clear();
      }
      if (sz[x] < sz[y])
        swap(x, y);
      for (auto j : who[y])
        who[x].push_back(j);
      sz[x] += sz[y];
      p[y] = x;
    }
    else {
      if (lan[x] != inf)
        continue;
      for (auto j : who[x])
        lan[j] = c;
      who[x].clear();
    }
    deg[a] += 1;
    deg[b] += 1;
  }
  /*
  for (int i = 0;i < n;i++)
    cout << lan[i] << ' ';*/
}

int getMinimumFuelCapacity(int x, int y) {
  int l = 1, r = 1e9, same = -1;
  auto check = [&](int mid) {
    pair<int, int> s = make_pair(mid, inf);
    auto u = upper_bound(par[x].begin(), par[x].end(), s);
    auto u1 = upper_bound(par[y].begin(), par[y].end(), s);
    int i = u - par[x].begin();
    int j = u1 - par[y].begin();
    if (i == 0 || j == 0)
      return false;
    --i, --j;
    return par[x][i].s == par[y][j].s;
  };
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      same = mid;
      r = mid - 1;
    }
    else
      l = mid + 1;
  }
  int ans = max(same, max(lan[x], lan[y]));
  if (ans > 1e9)
    ans = -1;
  return ans;
}
/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1

3 2
0 1 5
0 2 5
1
1 2

5 4
0 1 3
1 4 4
1 2 1
2 3 2
1
1 3
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 5208 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 81 ms 25288 KB Output is correct
10 Correct 103 ms 29636 KB Output is correct
11 Correct 129 ms 29128 KB Output is correct
12 Correct 145 ms 30724 KB Output is correct
13 Correct 76 ms 23704 KB Output is correct
14 Correct 123 ms 25368 KB Output is correct
15 Correct 331 ms 34536 KB Output is correct
16 Correct 260 ms 33396 KB Output is correct
17 Correct 297 ms 35328 KB Output is correct
18 Correct 199 ms 27948 KB Output is correct
19 Correct 131 ms 13808 KB Output is correct
20 Correct 279 ms 35492 KB Output is correct
21 Correct 274 ms 34672 KB Output is correct
22 Correct 309 ms 36392 KB Output is correct
23 Correct 187 ms 29232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 180 ms 27496 KB Output is correct
4 Correct 206 ms 28052 KB Output is correct
5 Correct 186 ms 27992 KB Output is correct
6 Correct 189 ms 28204 KB Output is correct
7 Correct 192 ms 28240 KB Output is correct
8 Correct 187 ms 27348 KB Output is correct
9 Correct 181 ms 28004 KB Output is correct
10 Correct 179 ms 27224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 5208 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5240 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 2 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 2 ms 5208 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 2 ms 5340 KB Output is correct
26 Correct 2 ms 5208 KB Output is correct
27 Correct 2 ms 5208 KB Output is correct
28 Correct 3 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 5208 KB Output is correct
6 Correct 2 ms 5208 KB Output is correct
7 Correct 3 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 81 ms 25288 KB Output is correct
11 Correct 103 ms 29636 KB Output is correct
12 Correct 129 ms 29128 KB Output is correct
13 Correct 145 ms 30724 KB Output is correct
14 Correct 76 ms 23704 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 2 ms 5240 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5208 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 2 ms 5340 KB Output is correct
31 Correct 2 ms 5208 KB Output is correct
32 Correct 2 ms 5208 KB Output is correct
33 Correct 3 ms 5208 KB Output is correct
34 Correct 10 ms 7704 KB Output is correct
35 Correct 106 ms 28252 KB Output is correct
36 Correct 130 ms 27888 KB Output is correct
37 Correct 103 ms 27072 KB Output is correct
38 Correct 102 ms 25800 KB Output is correct
39 Correct 93 ms 25284 KB Output is correct
40 Correct 80 ms 23296 KB Output is correct
41 Correct 117 ms 28912 KB Output is correct
42 Correct 108 ms 28868 KB Output is correct
43 Correct 68 ms 21704 KB Output is correct
44 Correct 97 ms 25284 KB Output is correct
45 Correct 82 ms 20544 KB Output is correct
46 Correct 119 ms 27848 KB Output is correct
47 Correct 91 ms 24360 KB Output is correct
48 Correct 86 ms 21508 KB Output is correct
49 Correct 57 ms 13284 KB Output is correct
50 Correct 39 ms 10180 KB Output is correct
51 Correct 66 ms 17864 KB Output is correct
52 Correct 126 ms 28848 KB Output is correct
53 Correct 128 ms 26560 KB Output is correct
54 Correct 144 ms 32452 KB Output is correct
55 Correct 72 ms 21444 KB Output is correct
56 Correct 123 ms 24440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 5208 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 81 ms 25288 KB Output is correct
10 Correct 103 ms 29636 KB Output is correct
11 Correct 129 ms 29128 KB Output is correct
12 Correct 145 ms 30724 KB Output is correct
13 Correct 76 ms 23704 KB Output is correct
14 Correct 123 ms 25368 KB Output is correct
15 Correct 331 ms 34536 KB Output is correct
16 Correct 260 ms 33396 KB Output is correct
17 Correct 297 ms 35328 KB Output is correct
18 Correct 199 ms 27948 KB Output is correct
19 Correct 180 ms 27496 KB Output is correct
20 Correct 206 ms 28052 KB Output is correct
21 Correct 186 ms 27992 KB Output is correct
22 Correct 189 ms 28204 KB Output is correct
23 Correct 192 ms 28240 KB Output is correct
24 Correct 187 ms 27348 KB Output is correct
25 Correct 181 ms 28004 KB Output is correct
26 Correct 179 ms 27224 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 2 ms 5240 KB Output is correct
31 Correct 2 ms 5212 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 2 ms 5212 KB Output is correct
36 Correct 10 ms 7704 KB Output is correct
37 Correct 106 ms 28252 KB Output is correct
38 Correct 130 ms 27888 KB Output is correct
39 Correct 103 ms 27072 KB Output is correct
40 Correct 102 ms 25800 KB Output is correct
41 Correct 93 ms 25284 KB Output is correct
42 Correct 80 ms 23296 KB Output is correct
43 Correct 117 ms 28912 KB Output is correct
44 Correct 108 ms 28868 KB Output is correct
45 Correct 68 ms 21704 KB Output is correct
46 Correct 97 ms 25284 KB Output is correct
47 Correct 25 ms 8428 KB Output is correct
48 Correct 261 ms 35240 KB Output is correct
49 Correct 241 ms 34132 KB Output is correct
50 Correct 272 ms 34348 KB Output is correct
51 Correct 238 ms 33024 KB Output is correct
52 Correct 241 ms 31260 KB Output is correct
53 Correct 229 ms 25464 KB Output is correct
54 Correct 249 ms 35968 KB Output is correct
55 Correct 249 ms 35884 KB Output is correct
56 Correct 177 ms 28200 KB Output is correct
57 Correct 273 ms 32840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 2 ms 5208 KB Output is correct
6 Correct 2 ms 5208 KB Output is correct
7 Correct 3 ms 5212 KB Output is correct
8 Correct 2 ms 5208 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 81 ms 25288 KB Output is correct
11 Correct 103 ms 29636 KB Output is correct
12 Correct 129 ms 29128 KB Output is correct
13 Correct 145 ms 30724 KB Output is correct
14 Correct 76 ms 23704 KB Output is correct
15 Correct 123 ms 25368 KB Output is correct
16 Correct 331 ms 34536 KB Output is correct
17 Correct 260 ms 33396 KB Output is correct
18 Correct 297 ms 35328 KB Output is correct
19 Correct 199 ms 27948 KB Output is correct
20 Correct 180 ms 27496 KB Output is correct
21 Correct 206 ms 28052 KB Output is correct
22 Correct 186 ms 27992 KB Output is correct
23 Correct 189 ms 28204 KB Output is correct
24 Correct 192 ms 28240 KB Output is correct
25 Correct 187 ms 27348 KB Output is correct
26 Correct 181 ms 28004 KB Output is correct
27 Correct 179 ms 27224 KB Output is correct
28 Correct 2 ms 5212 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 2 ms 5212 KB Output is correct
31 Correct 2 ms 5240 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 2 ms 5212 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 10 ms 7704 KB Output is correct
38 Correct 106 ms 28252 KB Output is correct
39 Correct 130 ms 27888 KB Output is correct
40 Correct 103 ms 27072 KB Output is correct
41 Correct 102 ms 25800 KB Output is correct
42 Correct 93 ms 25284 KB Output is correct
43 Correct 80 ms 23296 KB Output is correct
44 Correct 117 ms 28912 KB Output is correct
45 Correct 108 ms 28868 KB Output is correct
46 Correct 68 ms 21704 KB Output is correct
47 Correct 97 ms 25284 KB Output is correct
48 Correct 25 ms 8428 KB Output is correct
49 Correct 261 ms 35240 KB Output is correct
50 Correct 241 ms 34132 KB Output is correct
51 Correct 272 ms 34348 KB Output is correct
52 Correct 238 ms 33024 KB Output is correct
53 Correct 241 ms 31260 KB Output is correct
54 Correct 229 ms 25464 KB Output is correct
55 Correct 249 ms 35968 KB Output is correct
56 Correct 249 ms 35884 KB Output is correct
57 Correct 177 ms 28200 KB Output is correct
58 Correct 273 ms 32840 KB Output is correct
59 Correct 131 ms 13808 KB Output is correct
60 Correct 279 ms 35492 KB Output is correct
61 Correct 274 ms 34672 KB Output is correct
62 Correct 309 ms 36392 KB Output is correct
63 Correct 187 ms 29232 KB Output is correct
64 Correct 2 ms 5212 KB Output is correct
65 Correct 2 ms 5212 KB Output is correct
66 Correct 3 ms 5212 KB Output is correct
67 Correct 2 ms 5208 KB Output is correct
68 Correct 2 ms 5212 KB Output is correct
69 Correct 2 ms 5212 KB Output is correct
70 Correct 2 ms 5340 KB Output is correct
71 Correct 2 ms 5208 KB Output is correct
72 Correct 2 ms 5208 KB Output is correct
73 Correct 3 ms 5208 KB Output is correct
74 Correct 82 ms 20544 KB Output is correct
75 Correct 119 ms 27848 KB Output is correct
76 Correct 91 ms 24360 KB Output is correct
77 Correct 86 ms 21508 KB Output is correct
78 Correct 57 ms 13284 KB Output is correct
79 Correct 39 ms 10180 KB Output is correct
80 Correct 66 ms 17864 KB Output is correct
81 Correct 126 ms 28848 KB Output is correct
82 Correct 128 ms 26560 KB Output is correct
83 Correct 144 ms 32452 KB Output is correct
84 Correct 72 ms 21444 KB Output is correct
85 Correct 123 ms 24440 KB Output is correct
86 Correct 69 ms 12940 KB Output is correct
87 Correct 249 ms 33324 KB Output is correct
88 Correct 256 ms 33684 KB Output is correct
89 Correct 195 ms 27368 KB Output is correct
90 Correct 173 ms 19912 KB Output is correct
91 Correct 173 ms 20236 KB Output is correct
92 Correct 202 ms 25636 KB Output is correct
93 Correct 269 ms 36124 KB Output is correct
94 Correct 262 ms 34208 KB Output is correct
95 Correct 301 ms 40760 KB Output is correct
96 Correct 181 ms 28332 KB Output is correct
97 Correct 237 ms 31232 KB Output is correct