Submission #571192

# Submission time Handle Problem Language Result Execution time Memory
571192 2022-06-01T12:48:25 Z 2fat2code Swapping Cities (APIO20_swap) C++17
100 / 100
338 ms 57788 KB
#include "swap.h"
#include <bits/stdc++.h>
#define fr first
#define sc second
#define all(s) s.begin(), s.end()
using namespace std;

const int nmax = 300005;

int pardsu[nmax], last, lca[nmax][20], marked[nmax], grad[nmax], val[nmax], timp, in[nmax], out[nmax], apropiat[nmax], ans[nmax], sub[nmax];
vector<pair<int, pair<int,int>>>edges;
vector<int>nod[nmax];

int findpar(int x){
    if(x != pardsu[x]){
        pardsu[x] = findpar(pardsu[x]);
    }
    return pardsu[x];
}

void dfs(int s, int apr, int par = 0){
    in[s] = ++timp;
    if(sub[s]) apr = val[s];
    ans[s] = apr;
    for(auto it : nod[s]){
        dfs(it, apr, s);
    }
    out[s] = ++timp;
}

bool is_lca(int x, int y){
    if(in[x] <= in[y] && out[x] >= out[y]) return true;
    else return false;
}

void dfs1(int s){
    if(marked[s]) sub[s]++;
    for(auto it : nod[s]){
        dfs1(it);
        sub[s] += sub[it];
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    last = N;
    for(int i=1;i<=N;i++) pardsu[i] = i;
    for(int i=1;i<=N;i++) val[i] = 1e9;
    for(int i=0;i<M;i++){
        U[i]++; V[i]++;
        edges.push_back({W[i], {U[i], V[i]}});
    }
    sort(all(edges));
    for(auto it : edges){
        int x = findpar(it.sc.fr);
        int y = findpar(it.sc.sc);
        ++last;
        pardsu[y] = last;
        pardsu[x] = last;
        pardsu[last] = last;
        val[last] = it.fr;
        grad[it.sc.sc]++;
        grad[it.sc.fr]++;
        lca[x][0] = last;
        lca[y][0] = last;
        nod[last].push_back(x);
        if(x != y) nod[last].push_back(y);
        if(x == y){
            marked[last] = 1;
        }
        else{
            if(grad[it.sc.sc] >= 3 || grad[it.sc.fr] >= 3){
                marked[last] = 1;
            }
        }
    }
    for(int i=1;i<=19;i++){
        for(int j=1;j<=last;j++){
            lca[j][i] = lca[lca[j][i-1]][i-1];
        }
    }
    dfs1(last);
    dfs(last, 1e9);
   // for(int i=1;i<=N+M;i++) cout << ans[i] << ' ';
   // cout << '\n';
}

int getMinimumFuelCapacity(int X, int Y) {
    X++;
    Y++;
    int curr;
    if(is_lca(X, Y)) curr = X;
    else if(is_lca(Y, X)) curr = Y;
    else{
        curr = X;
        for(int i=19;i>=0;i--){
            if(lca[curr][i] != 0 && !is_lca(lca[curr][i], Y)) curr = lca[curr][i];
        }
        curr = lca[curr][0];
    }
    if(ans[curr] < 1e9){
        return ans[curr];
    }
    else{
        return -1;
    }
}


/**
6 5
0 1 1
1 2 2
1 3 3
1 4 4
4 5 5
3
0 4
5 4
0 2



*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7620 KB Output is correct
9 Correct 90 ms 28476 KB Output is correct
10 Correct 137 ms 33256 KB Output is correct
11 Correct 120 ms 32896 KB Output is correct
12 Correct 119 ms 34288 KB Output is correct
13 Correct 111 ms 36668 KB Output is correct
14 Correct 97 ms 28624 KB Output is correct
15 Correct 233 ms 35252 KB Output is correct
16 Correct 177 ms 34512 KB Output is correct
17 Correct 216 ms 36172 KB Output is correct
18 Correct 259 ms 38436 KB Output is correct
19 Correct 64 ms 15084 KB Output is correct
20 Correct 205 ms 36516 KB Output is correct
21 Correct 184 ms 35784 KB Output is correct
22 Correct 205 ms 37584 KB Output is correct
23 Correct 259 ms 39848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 247 ms 40764 KB Output is correct
4 Correct 253 ms 42232 KB Output is correct
5 Correct 276 ms 41420 KB Output is correct
6 Correct 268 ms 42036 KB Output is correct
7 Correct 261 ms 41788 KB Output is correct
8 Correct 237 ms 40476 KB Output is correct
9 Correct 263 ms 41548 KB Output is correct
10 Correct 236 ms 40268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7620 KB Output is correct
9 Correct 3 ms 7380 KB Output is correct
10 Correct 4 ms 7636 KB Output is correct
11 Correct 6 ms 7636 KB Output is correct
12 Correct 5 ms 7636 KB Output is correct
13 Correct 6 ms 7508 KB Output is correct
14 Correct 5 ms 7508 KB Output is correct
15 Correct 5 ms 7636 KB Output is correct
16 Correct 4 ms 7584 KB Output is correct
17 Correct 4 ms 7636 KB Output is correct
18 Correct 5 ms 7636 KB Output is correct
19 Correct 4 ms 7636 KB Output is correct
20 Correct 5 ms 7636 KB Output is correct
21 Correct 4 ms 7636 KB Output is correct
22 Correct 5 ms 7764 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 5 ms 7764 KB Output is correct
25 Correct 5 ms 7724 KB Output is correct
26 Correct 5 ms 7764 KB Output is correct
27 Correct 4 ms 7636 KB Output is correct
28 Correct 5 ms 7764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7636 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 4 ms 7620 KB Output is correct
10 Correct 90 ms 28476 KB Output is correct
11 Correct 137 ms 33256 KB Output is correct
12 Correct 120 ms 32896 KB Output is correct
13 Correct 119 ms 34288 KB Output is correct
14 Correct 111 ms 36668 KB Output is correct
15 Correct 4 ms 7636 KB Output is correct
16 Correct 6 ms 7636 KB Output is correct
17 Correct 5 ms 7636 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 5 ms 7508 KB Output is correct
20 Correct 5 ms 7636 KB Output is correct
21 Correct 4 ms 7584 KB Output is correct
22 Correct 4 ms 7636 KB Output is correct
23 Correct 5 ms 7636 KB Output is correct
24 Correct 4 ms 7636 KB Output is correct
25 Correct 5 ms 7636 KB Output is correct
26 Correct 4 ms 7636 KB Output is correct
27 Correct 5 ms 7764 KB Output is correct
28 Correct 4 ms 7508 KB Output is correct
29 Correct 5 ms 7764 KB Output is correct
30 Correct 5 ms 7724 KB Output is correct
31 Correct 5 ms 7764 KB Output is correct
32 Correct 4 ms 7636 KB Output is correct
33 Correct 5 ms 7764 KB Output is correct
34 Correct 14 ms 11028 KB Output is correct
35 Correct 160 ms 34376 KB Output is correct
36 Correct 120 ms 34380 KB Output is correct
37 Correct 132 ms 34508 KB Output is correct
38 Correct 137 ms 34228 KB Output is correct
39 Correct 134 ms 34348 KB Output is correct
40 Correct 108 ms 31940 KB Output is correct
41 Correct 136 ms 34332 KB Output is correct
42 Correct 136 ms 34400 KB Output is correct
43 Correct 119 ms 37564 KB Output is correct
44 Correct 127 ms 34764 KB Output is correct
45 Correct 152 ms 45020 KB Output is correct
46 Correct 124 ms 34392 KB Output is correct
47 Correct 130 ms 34768 KB Output is correct
48 Correct 150 ms 38152 KB Output is correct
49 Correct 113 ms 42040 KB Output is correct
50 Correct 89 ms 34232 KB Output is correct
51 Correct 130 ms 39956 KB Output is correct
52 Correct 221 ms 47776 KB Output is correct
53 Correct 207 ms 50984 KB Output is correct
54 Correct 205 ms 56252 KB Output is correct
55 Correct 120 ms 37244 KB Output is correct
56 Correct 195 ms 51916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 3 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7636 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7636 KB Output is correct
8 Correct 4 ms 7620 KB Output is correct
9 Correct 90 ms 28476 KB Output is correct
10 Correct 137 ms 33256 KB Output is correct
11 Correct 120 ms 32896 KB Output is correct
12 Correct 119 ms 34288 KB Output is correct
13 Correct 111 ms 36668 KB Output is correct
14 Correct 97 ms 28624 KB Output is correct
15 Correct 233 ms 35252 KB Output is correct
16 Correct 177 ms 34512 KB Output is correct
17 Correct 216 ms 36172 KB Output is correct
18 Correct 259 ms 38436 KB Output is correct
19 Correct 247 ms 40764 KB Output is correct
20 Correct 253 ms 42232 KB Output is correct
21 Correct 276 ms 41420 KB Output is correct
22 Correct 268 ms 42036 KB Output is correct
23 Correct 261 ms 41788 KB Output is correct
24 Correct 237 ms 40476 KB Output is correct
25 Correct 263 ms 41548 KB Output is correct
26 Correct 236 ms 40268 KB Output is correct
27 Correct 4 ms 7636 KB Output is correct
28 Correct 6 ms 7636 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 6 ms 7508 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 4 ms 7584 KB Output is correct
34 Correct 4 ms 7636 KB Output is correct
35 Correct 5 ms 7636 KB Output is correct
36 Correct 14 ms 11028 KB Output is correct
37 Correct 160 ms 34376 KB Output is correct
38 Correct 120 ms 34380 KB Output is correct
39 Correct 132 ms 34508 KB Output is correct
40 Correct 137 ms 34228 KB Output is correct
41 Correct 134 ms 34348 KB Output is correct
42 Correct 108 ms 31940 KB Output is correct
43 Correct 136 ms 34332 KB Output is correct
44 Correct 136 ms 34400 KB Output is correct
45 Correct 119 ms 37564 KB Output is correct
46 Correct 127 ms 34764 KB Output is correct
47 Correct 19 ms 11140 KB Output is correct
48 Correct 202 ms 36920 KB Output is correct
49 Correct 223 ms 36908 KB Output is correct
50 Correct 188 ms 37088 KB Output is correct
51 Correct 212 ms 37060 KB Output is correct
52 Correct 189 ms 35500 KB Output is correct
53 Correct 159 ms 29076 KB Output is correct
54 Correct 202 ms 37536 KB Output is correct
55 Correct 194 ms 36928 KB Output is correct
56 Correct 294 ms 39448 KB Output is correct
57 Correct 233 ms 37960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7636 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 4 ms 7620 KB Output is correct
10 Correct 90 ms 28476 KB Output is correct
11 Correct 137 ms 33256 KB Output is correct
12 Correct 120 ms 32896 KB Output is correct
13 Correct 119 ms 34288 KB Output is correct
14 Correct 111 ms 36668 KB Output is correct
15 Correct 97 ms 28624 KB Output is correct
16 Correct 233 ms 35252 KB Output is correct
17 Correct 177 ms 34512 KB Output is correct
18 Correct 216 ms 36172 KB Output is correct
19 Correct 259 ms 38436 KB Output is correct
20 Correct 247 ms 40764 KB Output is correct
21 Correct 253 ms 42232 KB Output is correct
22 Correct 276 ms 41420 KB Output is correct
23 Correct 268 ms 42036 KB Output is correct
24 Correct 261 ms 41788 KB Output is correct
25 Correct 237 ms 40476 KB Output is correct
26 Correct 263 ms 41548 KB Output is correct
27 Correct 236 ms 40268 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 6 ms 7636 KB Output is correct
30 Correct 5 ms 7636 KB Output is correct
31 Correct 6 ms 7508 KB Output is correct
32 Correct 5 ms 7508 KB Output is correct
33 Correct 5 ms 7636 KB Output is correct
34 Correct 4 ms 7584 KB Output is correct
35 Correct 4 ms 7636 KB Output is correct
36 Correct 5 ms 7636 KB Output is correct
37 Correct 14 ms 11028 KB Output is correct
38 Correct 160 ms 34376 KB Output is correct
39 Correct 120 ms 34380 KB Output is correct
40 Correct 132 ms 34508 KB Output is correct
41 Correct 137 ms 34228 KB Output is correct
42 Correct 134 ms 34348 KB Output is correct
43 Correct 108 ms 31940 KB Output is correct
44 Correct 136 ms 34332 KB Output is correct
45 Correct 136 ms 34400 KB Output is correct
46 Correct 119 ms 37564 KB Output is correct
47 Correct 127 ms 34764 KB Output is correct
48 Correct 19 ms 11140 KB Output is correct
49 Correct 202 ms 36920 KB Output is correct
50 Correct 223 ms 36908 KB Output is correct
51 Correct 188 ms 37088 KB Output is correct
52 Correct 212 ms 37060 KB Output is correct
53 Correct 189 ms 35500 KB Output is correct
54 Correct 159 ms 29076 KB Output is correct
55 Correct 202 ms 37536 KB Output is correct
56 Correct 194 ms 36928 KB Output is correct
57 Correct 294 ms 39448 KB Output is correct
58 Correct 233 ms 37960 KB Output is correct
59 Correct 64 ms 15084 KB Output is correct
60 Correct 205 ms 36516 KB Output is correct
61 Correct 184 ms 35784 KB Output is correct
62 Correct 205 ms 37584 KB Output is correct
63 Correct 259 ms 39848 KB Output is correct
64 Correct 4 ms 7636 KB Output is correct
65 Correct 5 ms 7636 KB Output is correct
66 Correct 4 ms 7636 KB Output is correct
67 Correct 5 ms 7764 KB Output is correct
68 Correct 4 ms 7508 KB Output is correct
69 Correct 5 ms 7764 KB Output is correct
70 Correct 5 ms 7724 KB Output is correct
71 Correct 5 ms 7764 KB Output is correct
72 Correct 4 ms 7636 KB Output is correct
73 Correct 5 ms 7764 KB Output is correct
74 Correct 152 ms 45020 KB Output is correct
75 Correct 124 ms 34392 KB Output is correct
76 Correct 130 ms 34768 KB Output is correct
77 Correct 150 ms 38152 KB Output is correct
78 Correct 113 ms 42040 KB Output is correct
79 Correct 89 ms 34232 KB Output is correct
80 Correct 130 ms 39956 KB Output is correct
81 Correct 221 ms 47776 KB Output is correct
82 Correct 207 ms 50984 KB Output is correct
83 Correct 205 ms 56252 KB Output is correct
84 Correct 120 ms 37244 KB Output is correct
85 Correct 195 ms 51916 KB Output is correct
86 Correct 95 ms 20984 KB Output is correct
87 Correct 215 ms 37032 KB Output is correct
88 Correct 213 ms 37232 KB Output is correct
89 Correct 275 ms 39264 KB Output is correct
90 Correct 214 ms 46064 KB Output is correct
91 Correct 221 ms 43264 KB Output is correct
92 Correct 290 ms 41412 KB Output is correct
93 Correct 295 ms 49660 KB Output is correct
94 Correct 338 ms 53164 KB Output is correct
95 Correct 322 ms 57788 KB Output is correct
96 Correct 245 ms 39744 KB Output is correct
97 Correct 326 ms 46528 KB Output is correct