Submission #571191

# Submission time Handle Problem Language Result Execution time Memory
571191 2022-06-01T12:42:56 Z 2fat2code Swapping Cities (APIO20_swap) C++17
50 / 100
312 ms 58584 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];
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(marked[s]) apr = val[s];
    ans[s] = max(val[s], apr);
    for(auto it : nod[s]){
        dfs(it, apr, s);
        ans[s] = min(ans[s], max(val[s], ans[it]));
    }
    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 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];
        }
    }
    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 3 ms 7380 KB Output is correct
3 Correct 5 ms 7288 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 6 ms 7636 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 97 ms 28364 KB Output is correct
10 Correct 121 ms 32832 KB Output is correct
11 Correct 109 ms 32472 KB Output is correct
12 Correct 127 ms 33992 KB Output is correct
13 Correct 105 ms 37840 KB Output is correct
14 Correct 104 ms 28352 KB Output is correct
15 Correct 185 ms 34844 KB Output is correct
16 Correct 174 ms 34216 KB Output is correct
17 Correct 182 ms 35860 KB Output is correct
18 Correct 257 ms 39624 KB Output is correct
19 Correct 65 ms 14960 KB Output is correct
20 Correct 179 ms 36180 KB Output is correct
21 Correct 174 ms 35340 KB Output is correct
22 Correct 192 ms 37156 KB Output is correct
23 Correct 243 ms 41012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 264 ms 43364 KB Output is correct
4 Correct 250 ms 45004 KB Output is correct
5 Correct 312 ms 44112 KB Output is correct
6 Correct 234 ms 44716 KB Output is correct
7 Correct 290 ms 44452 KB Output is correct
8 Correct 228 ms 43044 KB Output is correct
9 Correct 256 ms 44244 KB Output is correct
10 Correct 254 ms 42696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 5 ms 7288 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 6 ms 7636 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7636 KB Output is correct
11 Correct 4 ms 7620 KB Output is correct
12 Correct 5 ms 7648 KB Output is correct
13 Correct 5 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 5 ms 7636 KB Output is correct
17 Correct 5 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 4 ms 7636 KB Output is correct
21 Correct 5 ms 7636 KB Output is correct
22 Correct 4 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 7764 KB Output is correct
26 Correct 5 ms 7892 KB Output is correct
27 Correct 5 ms 7636 KB Output is correct
28 Correct 6 ms 7892 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 5 ms 7288 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 6 ms 7636 KB Output is correct
9 Correct 4 ms 7636 KB Output is correct
10 Correct 97 ms 28364 KB Output is correct
11 Correct 121 ms 32832 KB Output is correct
12 Correct 109 ms 32472 KB Output is correct
13 Correct 127 ms 33992 KB Output is correct
14 Correct 105 ms 37840 KB Output is correct
15 Correct 4 ms 7636 KB Output is correct
16 Correct 4 ms 7620 KB Output is correct
17 Correct 5 ms 7648 KB Output is correct
18 Correct 5 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 5 ms 7636 KB Output is correct
22 Correct 5 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 4 ms 7636 KB Output is correct
26 Correct 5 ms 7636 KB Output is correct
27 Correct 4 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 7764 KB Output is correct
31 Correct 5 ms 7892 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 6 ms 7892 KB Output is correct
34 Correct 20 ms 11028 KB Output is correct
35 Correct 123 ms 33984 KB Output is correct
36 Correct 124 ms 33940 KB Output is correct
37 Correct 115 ms 34148 KB Output is correct
38 Correct 130 ms 33948 KB Output is correct
39 Correct 130 ms 33876 KB Output is correct
40 Correct 105 ms 31568 KB Output is correct
41 Correct 132 ms 33948 KB Output is correct
42 Correct 125 ms 33928 KB Output is correct
43 Correct 107 ms 39204 KB Output is correct
44 Correct 122 ms 34328 KB Output is correct
45 Correct 161 ms 47288 KB Output is correct
46 Correct 139 ms 33992 KB Output is correct
47 Correct 119 ms 34440 KB Output is correct
48 Correct 156 ms 39068 KB Output is correct
49 Correct 113 ms 46268 KB Output is correct
50 Correct 101 ms 37408 KB Output is correct
51 Correct 127 ms 42012 KB Output is correct
52 Correct 189 ms 49172 KB Output is correct
53 Correct 201 ms 52088 KB Output is correct
54 Correct 214 ms 58584 KB Output is correct
55 Correct 119 ms 38672 KB Output is correct
56 Correct 197 ms 54472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7380 KB Output is correct
3 Correct 5 ms 7288 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 6 ms 7636 KB Output is correct
8 Correct 4 ms 7636 KB Output is correct
9 Correct 97 ms 28364 KB Output is correct
10 Correct 121 ms 32832 KB Output is correct
11 Correct 109 ms 32472 KB Output is correct
12 Correct 127 ms 33992 KB Output is correct
13 Correct 105 ms 37840 KB Output is correct
14 Correct 104 ms 28352 KB Output is correct
15 Correct 185 ms 34844 KB Output is correct
16 Correct 174 ms 34216 KB Output is correct
17 Correct 182 ms 35860 KB Output is correct
18 Correct 257 ms 39624 KB Output is correct
19 Correct 264 ms 43364 KB Output is correct
20 Correct 250 ms 45004 KB Output is correct
21 Correct 312 ms 44112 KB Output is correct
22 Correct 234 ms 44716 KB Output is correct
23 Correct 290 ms 44452 KB Output is correct
24 Correct 228 ms 43044 KB Output is correct
25 Correct 256 ms 44244 KB Output is correct
26 Correct 254 ms 42696 KB Output is correct
27 Correct 4 ms 7636 KB Output is correct
28 Correct 4 ms 7620 KB Output is correct
29 Correct 5 ms 7648 KB Output is correct
30 Correct 5 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 5 ms 7636 KB Output is correct
34 Correct 5 ms 7636 KB Output is correct
35 Correct 5 ms 7636 KB Output is correct
36 Correct 20 ms 11028 KB Output is correct
37 Correct 123 ms 33984 KB Output is correct
38 Correct 124 ms 33940 KB Output is correct
39 Correct 115 ms 34148 KB Output is correct
40 Correct 130 ms 33948 KB Output is correct
41 Correct 130 ms 33876 KB Output is correct
42 Correct 105 ms 31568 KB Output is correct
43 Correct 132 ms 33948 KB Output is correct
44 Correct 125 ms 33928 KB Output is correct
45 Correct 107 ms 39204 KB Output is correct
46 Correct 122 ms 34328 KB Output is correct
47 Correct 21 ms 11092 KB Output is correct
48 Incorrect 195 ms 36400 KB Output isn't correct
49 Halted 0 ms 0 KB -
# 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 5 ms 7288 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 6 ms 7636 KB Output is correct
9 Correct 4 ms 7636 KB Output is correct
10 Correct 97 ms 28364 KB Output is correct
11 Correct 121 ms 32832 KB Output is correct
12 Correct 109 ms 32472 KB Output is correct
13 Correct 127 ms 33992 KB Output is correct
14 Correct 105 ms 37840 KB Output is correct
15 Correct 104 ms 28352 KB Output is correct
16 Correct 185 ms 34844 KB Output is correct
17 Correct 174 ms 34216 KB Output is correct
18 Correct 182 ms 35860 KB Output is correct
19 Correct 257 ms 39624 KB Output is correct
20 Correct 264 ms 43364 KB Output is correct
21 Correct 250 ms 45004 KB Output is correct
22 Correct 312 ms 44112 KB Output is correct
23 Correct 234 ms 44716 KB Output is correct
24 Correct 290 ms 44452 KB Output is correct
25 Correct 228 ms 43044 KB Output is correct
26 Correct 256 ms 44244 KB Output is correct
27 Correct 254 ms 42696 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 4 ms 7620 KB Output is correct
30 Correct 5 ms 7648 KB Output is correct
31 Correct 5 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 5 ms 7636 KB Output is correct
35 Correct 5 ms 7636 KB Output is correct
36 Correct 5 ms 7636 KB Output is correct
37 Correct 20 ms 11028 KB Output is correct
38 Correct 123 ms 33984 KB Output is correct
39 Correct 124 ms 33940 KB Output is correct
40 Correct 115 ms 34148 KB Output is correct
41 Correct 130 ms 33948 KB Output is correct
42 Correct 130 ms 33876 KB Output is correct
43 Correct 105 ms 31568 KB Output is correct
44 Correct 132 ms 33948 KB Output is correct
45 Correct 125 ms 33928 KB Output is correct
46 Correct 107 ms 39204 KB Output is correct
47 Correct 122 ms 34328 KB Output is correct
48 Correct 21 ms 11092 KB Output is correct
49 Incorrect 195 ms 36400 KB Output isn't correct
50 Halted 0 ms 0 KB -