Submission #293419

# Submission time Handle Problem Language Result Execution time Memory
293419 2020-09-08T04:45:54 Z rama_pang Swapping Cities (APIO20_swap) C++14
100 / 100
811 ms 34532 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

class DisjointSet {
 public:
  int n, t = -1;
  vector<vector<pair<int, int>>> p; // (time, data)
  vector<vector<pair<int, int>>> sz;
  vector<vector<pair<int, int>>> deg;
  vector<vector<pair<int, int>>> cycle;

  DisjointSet() {}
  DisjointSet(int n) : n(n), p(n), sz(n), deg(n), cycle(n) {
    for (int i = 0; i < n; i++) {
      p[i].emplace_back(-1, i);
      sz[i].emplace_back(-1, 1);
      deg[i].emplace_back(-1, 0);
      cycle[i].emplace_back(-1, 0);
    }
  }

  int Find(int x) {
    return p[x].back().second == x ? x : Find(p[x].back().second);
  }
  int Unite(int x, int y) {
    t++;
    x = Find(x), y = Find(y);
    if (x != y) {
      if (sz[x].back().second > sz[y].back().second) {
        swap(x, y);
      }
      p[x].emplace_back(t, y);
      sz[y].emplace_back(t, sz[y].back().second + sz[x].back().second);
      if (deg[x].back().second > deg[y].back().second) {
        deg[y].emplace_back(t, deg[x].back().second);
      }
      if (cycle[y].back().second == 0 && cycle[x].back().second == 1) {
        cycle[y].emplace_back(t, 1);
      }
      return 1;
    }
    return 0;
  }
  void UpdateCycle(int x) {
    x = Find(x);
    if (cycle[x].back().second == 0) {
      cycle[x].emplace_back(t, 1);
    }
  }
  void UpdateDegree(int x, int v) {
    x = Find(x);
    if (v > deg[x].back().second) {
      deg[x].emplace_back(t, v);
    }
  }
  int Find(int x, int tt) {
    assert(p[x].size() <= 2);
    int pp = p[x][0].second;
    if (p[x].size() > 1 && p[x][1].first <= tt) {
      pp = p[x][1].second;
    }
    return pp == x ? x : Find(pp, tt);
  }
  int Degree(int x, int tt) {
    int pos = upper_bound(begin(deg[x]), end(deg[x]), make_pair(tt, int(1e9))) - begin(deg[x]) - 1;
    return deg[x][pos].second;
  }
  int Cycle(int x, int tt) {
    int pos = upper_bound(begin(cycle[x]), end(cycle[x]), make_pair(tt, int(1e9))) - begin(cycle[x]) - 1;
    return cycle[x][pos].second;
  }
};

DisjointSet D;
vector<int> values;

bool Check(int x, int y, int m) {
  x = D.Find(x, m), y = D.Find(y, m);
  return x == y && (D.Degree(x, m) >= 3 || D.Cycle(x, m) == 1);
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
  values = W;
  sort(begin(values), end(values));
  vector<array<int, 3>> E;
  for (int i = 0; i < M; i++) {
    E.push_back({W[i], U[i], V[i]});
  }
  sort(begin(E), end(E));
  D = DisjointSet(N);
  for (int i = 0; i < M; i++) {
    static vector<int> deg(N);
    deg[E[i][1]]++, deg[E[i][2]]++;
    if (!D.Unite(E[i][1], E[i][2])) {
      D.UpdateCycle(E[i][1]);
    }
    D.UpdateDegree(E[i][1], min(3, max(deg[E[i][1]], deg[E[i][2]])));
  }
}

int getMinimumFuelCapacity(int X, int Y) {
  int lo = 0, hi = int(values.size()) - 1;
  if (!Check(X, Y, hi)) {
    return -1;
  }
  while (lo < hi) {
    int md = (lo + hi) / 2;
    if (Check(X, Y, md)) {
      hi = md;
    } else {
      lo = md + 1;
    }
  }
  return values[lo];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 150 ms 22816 KB Output is correct
10 Correct 187 ms 27880 KB Output is correct
11 Correct 186 ms 27500 KB Output is correct
12 Correct 197 ms 29036 KB Output is correct
13 Correct 143 ms 28140 KB Output is correct
14 Correct 165 ms 22892 KB Output is correct
15 Correct 415 ms 29804 KB Output is correct
16 Correct 387 ms 28908 KB Output is correct
17 Correct 414 ms 30700 KB Output is correct
18 Correct 257 ms 29420 KB Output is correct
19 Correct 208 ms 8188 KB Output is correct
20 Correct 560 ms 30120 KB Output is correct
21 Correct 554 ms 29284 KB Output is correct
22 Correct 587 ms 31168 KB Output is correct
23 Correct 332 ms 29760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 296 ms 28524 KB Output is correct
4 Correct 309 ms 29928 KB Output is correct
5 Correct 315 ms 28908 KB Output is correct
6 Correct 296 ms 29676 KB Output is correct
7 Correct 316 ms 29292 KB Output is correct
8 Correct 303 ms 28140 KB Output is correct
9 Correct 300 ms 29120 KB Output is correct
10 Correct 306 ms 27884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 1 ms 640 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 2 ms 640 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 640 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 640 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 1 ms 512 KB Output is correct
24 Correct 2 ms 640 KB Output is correct
25 Correct 2 ms 640 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 2 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 150 ms 22816 KB Output is correct
11 Correct 187 ms 27880 KB Output is correct
12 Correct 186 ms 27500 KB Output is correct
13 Correct 197 ms 29036 KB Output is correct
14 Correct 143 ms 28140 KB Output is correct
15 Correct 1 ms 640 KB Output is correct
16 Correct 2 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 2 ms 640 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 640 KB Output is correct
24 Correct 18 ms 4192 KB Output is correct
25 Correct 188 ms 29036 KB Output is correct
26 Correct 195 ms 29036 KB Output is correct
27 Correct 194 ms 29036 KB Output is correct
28 Correct 185 ms 28524 KB Output is correct
29 Correct 189 ms 28264 KB Output is correct
30 Correct 166 ms 25836 KB Output is correct
31 Correct 193 ms 29052 KB Output is correct
32 Correct 193 ms 29036 KB Output is correct
33 Correct 137 ms 28140 KB Output is correct
34 Correct 185 ms 28652 KB Output is correct
35 Correct 1 ms 512 KB Output is correct
36 Correct 1 ms 640 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 1 ms 512 KB Output is correct
40 Correct 2 ms 640 KB Output is correct
41 Correct 2 ms 640 KB Output is correct
42 Correct 2 ms 640 KB Output is correct
43 Correct 1 ms 640 KB Output is correct
44 Correct 2 ms 640 KB Output is correct
45 Correct 217 ms 25192 KB Output is correct
46 Correct 187 ms 29028 KB Output is correct
47 Correct 199 ms 28780 KB Output is correct
48 Correct 185 ms 28268 KB Output is correct
49 Correct 107 ms 7916 KB Output is correct
50 Correct 93 ms 6892 KB Output is correct
51 Correct 172 ms 20856 KB Output is correct
52 Correct 263 ms 31460 KB Output is correct
53 Correct 293 ms 33380 KB Output is correct
54 Correct 320 ms 33128 KB Output is correct
55 Correct 139 ms 28140 KB Output is correct
56 Correct 260 ms 31336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 150 ms 22816 KB Output is correct
10 Correct 187 ms 27880 KB Output is correct
11 Correct 186 ms 27500 KB Output is correct
12 Correct 197 ms 29036 KB Output is correct
13 Correct 143 ms 28140 KB Output is correct
14 Correct 165 ms 22892 KB Output is correct
15 Correct 415 ms 29804 KB Output is correct
16 Correct 387 ms 28908 KB Output is correct
17 Correct 414 ms 30700 KB Output is correct
18 Correct 257 ms 29420 KB Output is correct
19 Correct 296 ms 28524 KB Output is correct
20 Correct 309 ms 29928 KB Output is correct
21 Correct 315 ms 28908 KB Output is correct
22 Correct 296 ms 29676 KB Output is correct
23 Correct 316 ms 29292 KB Output is correct
24 Correct 303 ms 28140 KB Output is correct
25 Correct 300 ms 29120 KB Output is correct
26 Correct 306 ms 27884 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 2 ms 640 KB Output is correct
29 Correct 1 ms 640 KB Output is correct
30 Correct 1 ms 512 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 1 ms 640 KB Output is correct
34 Correct 1 ms 640 KB Output is correct
35 Correct 1 ms 640 KB Output is correct
36 Correct 18 ms 4192 KB Output is correct
37 Correct 188 ms 29036 KB Output is correct
38 Correct 195 ms 29036 KB Output is correct
39 Correct 194 ms 29036 KB Output is correct
40 Correct 185 ms 28524 KB Output is correct
41 Correct 189 ms 28264 KB Output is correct
42 Correct 166 ms 25836 KB Output is correct
43 Correct 193 ms 29052 KB Output is correct
44 Correct 193 ms 29036 KB Output is correct
45 Correct 137 ms 28140 KB Output is correct
46 Correct 185 ms 28652 KB Output is correct
47 Correct 39 ms 4032 KB Output is correct
48 Correct 578 ms 30700 KB Output is correct
49 Correct 579 ms 30588 KB Output is correct
50 Correct 570 ms 30572 KB Output is correct
51 Correct 562 ms 30444 KB Output is correct
52 Correct 522 ms 28668 KB Output is correct
53 Correct 443 ms 21844 KB Output is correct
54 Correct 602 ms 31088 KB Output is correct
55 Correct 594 ms 30572 KB Output is correct
56 Correct 326 ms 29676 KB Output is correct
57 Correct 538 ms 30656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 150 ms 22816 KB Output is correct
11 Correct 187 ms 27880 KB Output is correct
12 Correct 186 ms 27500 KB Output is correct
13 Correct 197 ms 29036 KB Output is correct
14 Correct 143 ms 28140 KB Output is correct
15 Correct 165 ms 22892 KB Output is correct
16 Correct 415 ms 29804 KB Output is correct
17 Correct 387 ms 28908 KB Output is correct
18 Correct 414 ms 30700 KB Output is correct
19 Correct 257 ms 29420 KB Output is correct
20 Correct 296 ms 28524 KB Output is correct
21 Correct 309 ms 29928 KB Output is correct
22 Correct 315 ms 28908 KB Output is correct
23 Correct 296 ms 29676 KB Output is correct
24 Correct 316 ms 29292 KB Output is correct
25 Correct 303 ms 28140 KB Output is correct
26 Correct 300 ms 29120 KB Output is correct
27 Correct 306 ms 27884 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 2 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 1 ms 512 KB Output is correct
33 Correct 2 ms 640 KB Output is correct
34 Correct 1 ms 640 KB Output is correct
35 Correct 1 ms 640 KB Output is correct
36 Correct 1 ms 640 KB Output is correct
37 Correct 18 ms 4192 KB Output is correct
38 Correct 188 ms 29036 KB Output is correct
39 Correct 195 ms 29036 KB Output is correct
40 Correct 194 ms 29036 KB Output is correct
41 Correct 185 ms 28524 KB Output is correct
42 Correct 189 ms 28264 KB Output is correct
43 Correct 166 ms 25836 KB Output is correct
44 Correct 193 ms 29052 KB Output is correct
45 Correct 193 ms 29036 KB Output is correct
46 Correct 137 ms 28140 KB Output is correct
47 Correct 185 ms 28652 KB Output is correct
48 Correct 39 ms 4032 KB Output is correct
49 Correct 578 ms 30700 KB Output is correct
50 Correct 579 ms 30588 KB Output is correct
51 Correct 570 ms 30572 KB Output is correct
52 Correct 562 ms 30444 KB Output is correct
53 Correct 522 ms 28668 KB Output is correct
54 Correct 443 ms 21844 KB Output is correct
55 Correct 602 ms 31088 KB Output is correct
56 Correct 594 ms 30572 KB Output is correct
57 Correct 326 ms 29676 KB Output is correct
58 Correct 538 ms 30656 KB Output is correct
59 Correct 208 ms 8188 KB Output is correct
60 Correct 560 ms 30120 KB Output is correct
61 Correct 554 ms 29284 KB Output is correct
62 Correct 587 ms 31168 KB Output is correct
63 Correct 332 ms 29760 KB Output is correct
64 Correct 1 ms 512 KB Output is correct
65 Correct 1 ms 640 KB Output is correct
66 Correct 1 ms 512 KB Output is correct
67 Correct 2 ms 384 KB Output is correct
68 Correct 1 ms 512 KB Output is correct
69 Correct 2 ms 640 KB Output is correct
70 Correct 2 ms 640 KB Output is correct
71 Correct 2 ms 640 KB Output is correct
72 Correct 1 ms 640 KB Output is correct
73 Correct 2 ms 640 KB Output is correct
74 Correct 217 ms 25192 KB Output is correct
75 Correct 187 ms 29028 KB Output is correct
76 Correct 199 ms 28780 KB Output is correct
77 Correct 185 ms 28268 KB Output is correct
78 Correct 107 ms 7916 KB Output is correct
79 Correct 93 ms 6892 KB Output is correct
80 Correct 172 ms 20856 KB Output is correct
81 Correct 263 ms 31460 KB Output is correct
82 Correct 293 ms 33380 KB Output is correct
83 Correct 320 ms 33128 KB Output is correct
84 Correct 139 ms 28140 KB Output is correct
85 Correct 260 ms 31336 KB Output is correct
86 Correct 127 ms 10352 KB Output is correct
87 Correct 590 ms 30704 KB Output is correct
88 Correct 615 ms 30568 KB Output is correct
89 Correct 423 ms 28140 KB Output is correct
90 Correct 299 ms 9708 KB Output is correct
91 Correct 324 ms 10472 KB Output is correct
92 Correct 413 ms 22384 KB Output is correct
93 Correct 663 ms 32860 KB Output is correct
94 Correct 605 ms 34148 KB Output is correct
95 Correct 811 ms 34532 KB Output is correct
96 Correct 315 ms 29624 KB Output is correct
97 Correct 486 ms 31516 KB Output is correct