Submission #508148

# Submission time Handle Problem Language Result Execution time Memory
508148 2022-01-13T06:50:21 Z benson1029 Swapping Cities (APIO20_swap) C++14
100 / 100
276 ms 43368 KB
#include "swap.h"

#include<bits/stdc++.h>
using namespace std;

int par[200005];
int lift[200005][20];
vector<int> krus[200005];
int st[200005], ed[200005];
int dsu[200005];
int ans[200005];
int deg[200005];
int timer = 0;
pair< int, pair< int, int> > edg[200005];

bool anc(int x, int y) {
  return st[x] <= st[y] && ed[x] >= ed[y];
}

int lca(int x, int y) {
  if(anc(x, y)) return x;
  for(int i=19; i>=0; i--) {
    if(lift[x][i]==-1) continue;
    if(!anc(lift[x][i], y)) x = lift[x][i];
  }
  return lift[x][0];
}

int find(int x) {
  if(dsu[x]==x) return x;
  else return dsu[x] = find(dsu[x]);
}

void dfs(int x) {
  st[x] = ++timer;
  lift[x][0] = par[x];
  for(int i=1; i<20; i++) {
    if(lift[x][i-1]==-1) lift[x][i]=-1;
    else lift[x][i] = lift[lift[x][i-1]][i-1];
  }
  for(int i:krus[x]) {
    ans[i] = min(ans[i], ans[x]);
    dfs(i);
  }
  ed[x] = ++timer;
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    for(int i=0; i<M; i++) {
      edg[i] = {W[i], {U[i], V[i]} };
    }
    sort(edg, edg+M);
    for(int i=0; i<N*2; i++) dsu[i] = i, ans[i] = 2e9, deg[i] = 0;
    int tmp = N;
    for(int i=0; i<M; i++) {
      if(find(edg[i].second.first) != find(edg[i].second.second)) {
        krus[tmp].push_back(dsu[edg[i].second.first]);
        krus[tmp].push_back(dsu[edg[i].second.second]);
        par[dsu[edg[i].second.first]] = par[dsu[edg[i].second.second]] = tmp;
        if(ans[dsu[edg[i].second.first]]<2e9 || ans[dsu[edg[i].second.second]]<2e9) ans[tmp] = edg[i].first;
        dsu[dsu[edg[i].second.first]] = dsu[dsu[edg[i].second.second]] = tmp;
        tmp++;
      } else { // cycle
        ans[find(edg[i].second.first)] = min(ans[find(edg[i].second.first)], edg[i].first);
      }
      deg[edg[i].second.first]++; deg[edg[i].second.second]++;
      if(deg[edg[i].second.first] >= 3 || deg[edg[i].second.second] >= 3) {
        ans[find(edg[i].second.first)] = min(ans[find(edg[i].second.first)], edg[i].first);
      }
    }
    par[tmp-1] = -1;
    dfs(tmp-1);
}

int getMinimumFuelCapacity(int X, int Y) {
  return ans[lca(X, Y)] == 2e9 ? -1 : ans[lca(X, Y)];
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5012 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4988 KB Output is correct
4 Correct 3 ms 5132 KB Output is correct
5 Correct 6 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5324 KB Output is correct
8 Correct 5 ms 5324 KB Output is correct
9 Correct 101 ms 27816 KB Output is correct
10 Correct 123 ms 32944 KB Output is correct
11 Correct 109 ms 32464 KB Output is correct
12 Correct 123 ms 34076 KB Output is correct
13 Correct 122 ms 36368 KB Output is correct
14 Correct 116 ms 28164 KB Output is correct
15 Correct 226 ms 37328 KB Output is correct
16 Correct 211 ms 36492 KB Output is correct
17 Correct 202 ms 38152 KB Output is correct
18 Correct 231 ms 40452 KB Output is correct
19 Correct 76 ms 14704 KB Output is correct
20 Correct 196 ms 38380 KB Output is correct
21 Correct 213 ms 37604 KB Output is correct
22 Correct 190 ms 39524 KB Output is correct
23 Correct 231 ms 41916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5012 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 224 ms 41760 KB Output is correct
4 Correct 230 ms 43268 KB Output is correct
5 Correct 260 ms 42660 KB Output is correct
6 Correct 276 ms 43124 KB Output is correct
7 Correct 232 ms 42984 KB Output is correct
8 Correct 251 ms 41652 KB Output is correct
9 Correct 233 ms 42708 KB Output is correct
10 Correct 249 ms 41328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5012 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4988 KB Output is correct
4 Correct 3 ms 5132 KB Output is correct
5 Correct 6 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5324 KB Output is correct
8 Correct 5 ms 5324 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 5 ms 5272 KB Output is correct
11 Correct 4 ms 5280 KB Output is correct
12 Correct 3 ms 5196 KB Output is correct
13 Correct 4 ms 5196 KB Output is correct
14 Correct 5 ms 5272 KB Output is correct
15 Correct 5 ms 5324 KB Output is correct
16 Correct 3 ms 5324 KB Output is correct
17 Correct 3 ms 5276 KB Output is correct
18 Correct 3 ms 5324 KB Output is correct
19 Correct 4 ms 5196 KB Output is correct
20 Correct 3 ms 5272 KB Output is correct
21 Correct 5 ms 5276 KB Output is correct
22 Correct 4 ms 5148 KB Output is correct
23 Correct 3 ms 5196 KB Output is correct
24 Correct 4 ms 5264 KB Output is correct
25 Correct 4 ms 5360 KB Output is correct
26 Correct 4 ms 5324 KB Output is correct
27 Correct 4 ms 5324 KB Output is correct
28 Correct 6 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 5 ms 5012 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 5132 KB Output is correct
6 Correct 6 ms 5196 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5324 KB Output is correct
9 Correct 5 ms 5324 KB Output is correct
10 Correct 101 ms 27816 KB Output is correct
11 Correct 123 ms 32944 KB Output is correct
12 Correct 109 ms 32464 KB Output is correct
13 Correct 123 ms 34076 KB Output is correct
14 Correct 122 ms 36368 KB Output is correct
15 Correct 5 ms 5272 KB Output is correct
16 Correct 4 ms 5280 KB Output is correct
17 Correct 3 ms 5196 KB Output is correct
18 Correct 4 ms 5196 KB Output is correct
19 Correct 5 ms 5272 KB Output is correct
20 Correct 5 ms 5324 KB Output is correct
21 Correct 3 ms 5324 KB Output is correct
22 Correct 3 ms 5276 KB Output is correct
23 Correct 3 ms 5324 KB Output is correct
24 Correct 4 ms 5196 KB Output is correct
25 Correct 3 ms 5272 KB Output is correct
26 Correct 5 ms 5276 KB Output is correct
27 Correct 4 ms 5148 KB Output is correct
28 Correct 3 ms 5196 KB Output is correct
29 Correct 4 ms 5264 KB Output is correct
30 Correct 4 ms 5360 KB Output is correct
31 Correct 4 ms 5324 KB Output is correct
32 Correct 4 ms 5324 KB Output is correct
33 Correct 6 ms 5324 KB Output is correct
34 Correct 18 ms 8956 KB Output is correct
35 Correct 121 ms 33824 KB Output is correct
36 Correct 131 ms 33696 KB Output is correct
37 Correct 107 ms 33860 KB Output is correct
38 Correct 109 ms 33324 KB Output is correct
39 Correct 123 ms 33276 KB Output is correct
40 Correct 111 ms 30932 KB Output is correct
41 Correct 127 ms 34104 KB Output is correct
42 Correct 133 ms 33824 KB Output is correct
43 Correct 103 ms 36876 KB Output is correct
44 Correct 111 ms 34060 KB Output is correct
45 Correct 136 ms 32864 KB Output is correct
46 Correct 121 ms 33728 KB Output is correct
47 Correct 114 ms 33740 KB Output is correct
48 Correct 116 ms 34884 KB Output is correct
49 Correct 65 ms 13556 KB Output is correct
50 Correct 59 ms 13436 KB Output is correct
51 Correct 108 ms 28120 KB Output is correct
52 Correct 143 ms 37444 KB Output is correct
53 Correct 164 ms 39080 KB Output is correct
54 Correct 171 ms 39492 KB Output is correct
55 Correct 105 ms 36628 KB Output is correct
56 Correct 170 ms 39468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5012 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4988 KB Output is correct
4 Correct 3 ms 5132 KB Output is correct
5 Correct 6 ms 5196 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 5 ms 5324 KB Output is correct
8 Correct 5 ms 5324 KB Output is correct
9 Correct 101 ms 27816 KB Output is correct
10 Correct 123 ms 32944 KB Output is correct
11 Correct 109 ms 32464 KB Output is correct
12 Correct 123 ms 34076 KB Output is correct
13 Correct 122 ms 36368 KB Output is correct
14 Correct 116 ms 28164 KB Output is correct
15 Correct 226 ms 37328 KB Output is correct
16 Correct 211 ms 36492 KB Output is correct
17 Correct 202 ms 38152 KB Output is correct
18 Correct 231 ms 40452 KB Output is correct
19 Correct 224 ms 41760 KB Output is correct
20 Correct 230 ms 43268 KB Output is correct
21 Correct 260 ms 42660 KB Output is correct
22 Correct 276 ms 43124 KB Output is correct
23 Correct 232 ms 42984 KB Output is correct
24 Correct 251 ms 41652 KB Output is correct
25 Correct 233 ms 42708 KB Output is correct
26 Correct 249 ms 41328 KB Output is correct
27 Correct 5 ms 5272 KB Output is correct
28 Correct 4 ms 5280 KB Output is correct
29 Correct 3 ms 5196 KB Output is correct
30 Correct 4 ms 5196 KB Output is correct
31 Correct 5 ms 5272 KB Output is correct
32 Correct 5 ms 5324 KB Output is correct
33 Correct 3 ms 5324 KB Output is correct
34 Correct 3 ms 5276 KB Output is correct
35 Correct 3 ms 5324 KB Output is correct
36 Correct 18 ms 8956 KB Output is correct
37 Correct 121 ms 33824 KB Output is correct
38 Correct 131 ms 33696 KB Output is correct
39 Correct 107 ms 33860 KB Output is correct
40 Correct 109 ms 33324 KB Output is correct
41 Correct 123 ms 33276 KB Output is correct
42 Correct 111 ms 30932 KB Output is correct
43 Correct 127 ms 34104 KB Output is correct
44 Correct 133 ms 33824 KB Output is correct
45 Correct 103 ms 36876 KB Output is correct
46 Correct 111 ms 34060 KB Output is correct
47 Correct 22 ms 9080 KB Output is correct
48 Correct 198 ms 38576 KB Output is correct
49 Correct 216 ms 38688 KB Output is correct
50 Correct 207 ms 38552 KB Output is correct
51 Correct 182 ms 38520 KB Output is correct
52 Correct 176 ms 36760 KB Output is correct
53 Correct 149 ms 30044 KB Output is correct
54 Correct 228 ms 39504 KB Output is correct
55 Correct 225 ms 38648 KB Output is correct
56 Correct 243 ms 41068 KB Output is correct
57 Correct 192 ms 39580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 5 ms 5012 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4988 KB Output is correct
5 Correct 3 ms 5132 KB Output is correct
6 Correct 6 ms 5196 KB Output is correct
7 Correct 5 ms 5196 KB Output is correct
8 Correct 5 ms 5324 KB Output is correct
9 Correct 5 ms 5324 KB Output is correct
10 Correct 101 ms 27816 KB Output is correct
11 Correct 123 ms 32944 KB Output is correct
12 Correct 109 ms 32464 KB Output is correct
13 Correct 123 ms 34076 KB Output is correct
14 Correct 122 ms 36368 KB Output is correct
15 Correct 116 ms 28164 KB Output is correct
16 Correct 226 ms 37328 KB Output is correct
17 Correct 211 ms 36492 KB Output is correct
18 Correct 202 ms 38152 KB Output is correct
19 Correct 231 ms 40452 KB Output is correct
20 Correct 224 ms 41760 KB Output is correct
21 Correct 230 ms 43268 KB Output is correct
22 Correct 260 ms 42660 KB Output is correct
23 Correct 276 ms 43124 KB Output is correct
24 Correct 232 ms 42984 KB Output is correct
25 Correct 251 ms 41652 KB Output is correct
26 Correct 233 ms 42708 KB Output is correct
27 Correct 249 ms 41328 KB Output is correct
28 Correct 5 ms 5272 KB Output is correct
29 Correct 4 ms 5280 KB Output is correct
30 Correct 3 ms 5196 KB Output is correct
31 Correct 4 ms 5196 KB Output is correct
32 Correct 5 ms 5272 KB Output is correct
33 Correct 5 ms 5324 KB Output is correct
34 Correct 3 ms 5324 KB Output is correct
35 Correct 3 ms 5276 KB Output is correct
36 Correct 3 ms 5324 KB Output is correct
37 Correct 18 ms 8956 KB Output is correct
38 Correct 121 ms 33824 KB Output is correct
39 Correct 131 ms 33696 KB Output is correct
40 Correct 107 ms 33860 KB Output is correct
41 Correct 109 ms 33324 KB Output is correct
42 Correct 123 ms 33276 KB Output is correct
43 Correct 111 ms 30932 KB Output is correct
44 Correct 127 ms 34104 KB Output is correct
45 Correct 133 ms 33824 KB Output is correct
46 Correct 103 ms 36876 KB Output is correct
47 Correct 111 ms 34060 KB Output is correct
48 Correct 22 ms 9080 KB Output is correct
49 Correct 198 ms 38576 KB Output is correct
50 Correct 216 ms 38688 KB Output is correct
51 Correct 207 ms 38552 KB Output is correct
52 Correct 182 ms 38520 KB Output is correct
53 Correct 176 ms 36760 KB Output is correct
54 Correct 149 ms 30044 KB Output is correct
55 Correct 228 ms 39504 KB Output is correct
56 Correct 225 ms 38648 KB Output is correct
57 Correct 243 ms 41068 KB Output is correct
58 Correct 192 ms 39580 KB Output is correct
59 Correct 76 ms 14704 KB Output is correct
60 Correct 196 ms 38380 KB Output is correct
61 Correct 213 ms 37604 KB Output is correct
62 Correct 190 ms 39524 KB Output is correct
63 Correct 231 ms 41916 KB Output is correct
64 Correct 4 ms 5196 KB Output is correct
65 Correct 3 ms 5272 KB Output is correct
66 Correct 5 ms 5276 KB Output is correct
67 Correct 4 ms 5148 KB Output is correct
68 Correct 3 ms 5196 KB Output is correct
69 Correct 4 ms 5264 KB Output is correct
70 Correct 4 ms 5360 KB Output is correct
71 Correct 4 ms 5324 KB Output is correct
72 Correct 4 ms 5324 KB Output is correct
73 Correct 6 ms 5324 KB Output is correct
74 Correct 136 ms 32864 KB Output is correct
75 Correct 121 ms 33728 KB Output is correct
76 Correct 114 ms 33740 KB Output is correct
77 Correct 116 ms 34884 KB Output is correct
78 Correct 65 ms 13556 KB Output is correct
79 Correct 59 ms 13436 KB Output is correct
80 Correct 108 ms 28120 KB Output is correct
81 Correct 143 ms 37444 KB Output is correct
82 Correct 164 ms 39080 KB Output is correct
83 Correct 171 ms 39492 KB Output is correct
84 Correct 105 ms 36628 KB Output is correct
85 Correct 170 ms 39468 KB Output is correct
86 Correct 65 ms 16648 KB Output is correct
87 Correct 175 ms 38644 KB Output is correct
88 Correct 197 ms 38684 KB Output is correct
89 Correct 269 ms 37980 KB Output is correct
90 Correct 135 ms 18196 KB Output is correct
91 Correct 142 ms 20380 KB Output is correct
92 Correct 207 ms 33088 KB Output is correct
93 Correct 213 ms 42256 KB Output is correct
94 Correct 256 ms 43368 KB Output is correct
95 Correct 275 ms 43336 KB Output is correct
96 Correct 236 ms 41360 KB Output is correct
97 Correct 274 ms 42356 KB Output is correct