Submission #612690

# Submission time Handle Problem Language Result Execution time Memory
612690 2022-07-29T20:33:33 Z AugustinasJucas Swapping Cities (APIO20_swap) C++14
100 / 100
555 ms 51860 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

vector<pair<int, pair<int, int> > > brn;
const int dydis = 1e5 + 10;
int tevas[dydis];
int kadaNelinija[dydis];
bool neraLinija[dydis] = {};
vector<int> nodes[dydis];
int A[dydis], B[dydis];
int n, m;
vector<pair<int, int> > gr[dydis];
void add(int a, int b, int w) {
    //cout << "brn " << a << " -- " << b << ", w = " << w << endl;
    gr[a].push_back({b, w});
    gr[b].push_back({a, w});
}
int fp(int v) {
    if(tevas[v] == v) return v;
    return tevas[v] = fp(tevas[v]);
}
void conn(int a, int b, int t) {
    int inA = a; int inB = b;

    a = fp(a);
    b = fp(b);
 //   //cout << "jungiu " << inA << "(yra_linija = " << !neraLinija[a] <<") su " << inB  << "(yra_linija = " << !neraLinija[b] << ")" << endl;
    if(a == b) {
        if(neraLinija[a]) return ;
        neraLinija[a] = true;
        for(auto x : nodes[a]) kadaNelinija[x] = t;
    }else {
        add(inA, inB, t);
        if(nodes[a].size() > nodes[b].size()) swap(a, b);
        if(neraLinija[a] || neraLinija[b]) {
            if(!neraLinija[a]) for(auto x : nodes[a]) kadaNelinija[x] = t;
            if(!neraLinija[b]) for(auto x : nodes[b]) kadaNelinija[x] = t;
            neraLinija[a] = neraLinija[b] = true;
        }else if(!neraLinija[a] && !neraLinija[b]) {
            set<int> galai = {A[a], B[a], A[b], B[b]};
            ////cout << "linju galai: "; for(auto x : galai) //cout << x << " ";
          //  //cout << ", o as jungiu " << inA << " su " << inB << endl;
            if(galai.count(inA) && galai.count(inB)) {
                /// sujungiau liniju galus
                int na, nb;
                if(A[a] == inA || A[a] == inB) {
                    na = B[a];
                }else {
                    na = A[a];
                }
                if(A[b] == inA || A[b] == inB) {
                    nb = B[b];
                }else {
                    nb = A[b];
                }
                A[b] = na;
                B[b] = nb;
            }else {
                for(auto x : nodes[a]) kadaNelinija[x] = t;
                for(auto x : nodes[b]) kadaNelinija[x] = t;
                neraLinija[b] = true;
            }
        }

        tevas[a] = b;
        for(auto x : nodes[a]) nodes[b].push_back(x);

    }
}
int dbInd = 0;
int enter[dydis], leave[dydis];
int rotW[dydis] = {}, par[dydis];
int h[dydis];
void dfs(int v, int came) {
    enter[v] = dbInd++;
    par[v] = came;
   // //cout << "esi " << v << endl;
    for(auto x : gr[v]) {
        if(x.first == came) continue;
        h[x.first] = h[v] + 1;
        rotW[x.first] = x.second;
        //cout << "rot[" << x.first << "] = " << x.second <<  ", T./Y., " << rotW[x.first] << endl;

        dfs(x.first, v);
    }
    leave[v] = dbInd++;
}
const int dd = 20;
int lft[dydis][dd];
int mx[dydis][dd];
void calc() {
    for(int i = 0; i < n; i++) {
        lft[i][0] = par[i];
        //cout << "rotw[" << i << "] = " << rotW[i] << endl;
        mx[i][0] = rotW[i];
        //cout << "mx[" << i << "][" << 0 << "] = " << mx[i][0] << endl;
    }
    for(int j = 1; j < dd; j++) {
        for(int i = 0; i < n; i++) {
            lft[i][j] = lft[lft[i][j-1]][j-1];
        }
    }
    for(int j = 1; j < dd; j++) {
        for(int i = 0; i < n; i++) {
            mx[i][j] = max(mx[i][j-1], mx[ lft[i][j-1] ][j-1]);
            //cout << "mx[" << i << "][" << j << "] = " << mx[i][j] << endl;
        }
    }

}
bool isAnc(int a, int b) {
    return (enter[a] <= enter[b] && leave[b] <= leave[a]);
}

int lca(int a, int b) {
    if(isAnc(a, b)) return a;
    if(isAnc(b, a)) return b;
    for(int i = dd-1; i > -1; i--) {
        if(!isAnc(lft[a][i], b)) {
            a = lft[a][i];
        }
    }
    return par[a];
}

int mxPerBrn(int a, int kek) {
    int ret = 0;
    for(int i = dd-1; i > -1; i--) {
        if((1 << i) <= kek) {
            ret = max(ret, mx[a][i]);
            a = lft[a][i];
            kek -= (1 << i);
        }
    }
    return ret;
}
int mxInPath(int a, int b) {
    int v = lca(a, b);
    int kair = mxPerBrn(a, h[a] - h[v]);
    int desn = mxPerBrn(b, h[b] - h[v]);
    return max(kair, desn);
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    n = N; m = M;
    for(int i = 0; i < n; i++)  {
        tevas[i] = i;
        nodes[i] = {i};
        A[i] = i; B[i] = i;
        neraLinija[i] = false;
        kadaNelinija[i] = -1;
    }

    for(int i = 0; i < m; i++) {
        brn.push_back({W[i], {U[i], V[i]}});
    }
    sort(brn.begin(), brn.end()); /// nuo maz iki did.

    for(auto x : brn) {
        int a = x.second.first;
        int b = x.second.second;
        int w = x.first;
        conn(a, b, w);
    }
    h[0] = 0;

    dfs(0, 0);
    calc();

    for(int i = 0; i < n; i++) {
        //cout << "virsune nr " << i << " tampa nebe linija, kai w = " << kadaNelinija[i] << endl;
    }

    /*for(int i = 0; i < n; i++) {
        for(int j = i+1; j < n; j++) {
            int res = mxInPath(i, j);
            cout << "min max val tarp " << i << " ir " << j << " yra " << res << endl;
        }
        cout << endl;
    }*/


}

int getMinimumFuelCapacity(int X, int Y) {
    if(kadaNelinija[fp(X)] == -1 || kadaNelinija[fp(Y)] == -1) return -1;
    int ret = mxInPath(X, Y);
    ret = max(ret, max(kadaNelinija[X], kadaNelinija[Y]));
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 5 ms 5076 KB Output is correct
5 Correct 5 ms 5260 KB Output is correct
6 Correct 5 ms 5332 KB Output is correct
7 Correct 4 ms 5332 KB Output is correct
8 Correct 4 ms 5328 KB Output is correct
9 Correct 195 ms 33976 KB Output is correct
10 Correct 218 ms 40912 KB Output is correct
11 Correct 193 ms 40056 KB Output is correct
12 Correct 300 ms 42320 KB Output is correct
13 Correct 249 ms 41052 KB Output is correct
14 Correct 200 ms 33456 KB Output is correct
15 Correct 301 ms 43172 KB Output is correct
16 Correct 259 ms 40700 KB Output is correct
17 Correct 286 ms 45612 KB Output is correct
18 Correct 237 ms 41652 KB Output is correct
19 Correct 114 ms 14512 KB Output is correct
20 Correct 421 ms 43704 KB Output is correct
21 Correct 507 ms 41872 KB Output is correct
22 Correct 555 ms 45192 KB Output is correct
23 Correct 538 ms 42700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 279 ms 36672 KB Output is correct
4 Correct 305 ms 38028 KB Output is correct
5 Correct 316 ms 37252 KB Output is correct
6 Correct 274 ms 38288 KB Output is correct
7 Correct 365 ms 38028 KB Output is correct
8 Correct 302 ms 36972 KB Output is correct
9 Correct 295 ms 38020 KB Output is correct
10 Correct 342 ms 36948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 5 ms 5076 KB Output is correct
5 Correct 5 ms 5260 KB Output is correct
6 Correct 5 ms 5332 KB Output is correct
7 Correct 4 ms 5332 KB Output is correct
8 Correct 4 ms 5328 KB Output is correct
9 Correct 5 ms 4948 KB Output is correct
10 Correct 7 ms 5304 KB Output is correct
11 Correct 6 ms 5332 KB Output is correct
12 Correct 5 ms 5332 KB Output is correct
13 Correct 6 ms 5280 KB Output is correct
14 Correct 6 ms 5332 KB Output is correct
15 Correct 6 ms 5332 KB Output is correct
16 Correct 4 ms 5288 KB Output is correct
17 Correct 6 ms 5376 KB Output is correct
18 Correct 6 ms 5284 KB Output is correct
19 Correct 5 ms 5280 KB Output is correct
20 Correct 5 ms 5332 KB Output is correct
21 Correct 5 ms 5348 KB Output is correct
22 Correct 5 ms 5156 KB Output is correct
23 Correct 5 ms 5204 KB Output is correct
24 Correct 7 ms 5332 KB Output is correct
25 Correct 7 ms 5412 KB Output is correct
26 Correct 6 ms 5424 KB Output is correct
27 Correct 4 ms 5348 KB Output is correct
28 Correct 5 ms 5416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 5 ms 5260 KB Output is correct
7 Correct 5 ms 5332 KB Output is correct
8 Correct 4 ms 5332 KB Output is correct
9 Correct 4 ms 5328 KB Output is correct
10 Correct 195 ms 33976 KB Output is correct
11 Correct 218 ms 40912 KB Output is correct
12 Correct 193 ms 40056 KB Output is correct
13 Correct 300 ms 42320 KB Output is correct
14 Correct 249 ms 41052 KB Output is correct
15 Correct 7 ms 5304 KB Output is correct
16 Correct 6 ms 5332 KB Output is correct
17 Correct 5 ms 5332 KB Output is correct
18 Correct 6 ms 5280 KB Output is correct
19 Correct 6 ms 5332 KB Output is correct
20 Correct 6 ms 5332 KB Output is correct
21 Correct 4 ms 5288 KB Output is correct
22 Correct 6 ms 5376 KB Output is correct
23 Correct 6 ms 5284 KB Output is correct
24 Correct 5 ms 5280 KB Output is correct
25 Correct 5 ms 5332 KB Output is correct
26 Correct 5 ms 5348 KB Output is correct
27 Correct 5 ms 5156 KB Output is correct
28 Correct 5 ms 5204 KB Output is correct
29 Correct 7 ms 5332 KB Output is correct
30 Correct 7 ms 5412 KB Output is correct
31 Correct 6 ms 5424 KB Output is correct
32 Correct 4 ms 5348 KB Output is correct
33 Correct 5 ms 5416 KB Output is correct
34 Correct 22 ms 9584 KB Output is correct
35 Correct 225 ms 43760 KB Output is correct
36 Correct 252 ms 41576 KB Output is correct
37 Correct 223 ms 39644 KB Output is correct
38 Correct 224 ms 38664 KB Output is correct
39 Correct 221 ms 38000 KB Output is correct
40 Correct 213 ms 35308 KB Output is correct
41 Correct 234 ms 42632 KB Output is correct
42 Correct 248 ms 43456 KB Output is correct
43 Correct 167 ms 41448 KB Output is correct
44 Correct 215 ms 39016 KB Output is correct
45 Correct 163 ms 34304 KB Output is correct
46 Correct 188 ms 40720 KB Output is correct
47 Correct 188 ms 37792 KB Output is correct
48 Correct 207 ms 36880 KB Output is correct
49 Correct 64 ms 14652 KB Output is correct
50 Correct 60 ms 13568 KB Output is correct
51 Correct 134 ms 29404 KB Output is correct
52 Correct 235 ms 44132 KB Output is correct
53 Correct 202 ms 42792 KB Output is correct
54 Correct 229 ms 47908 KB Output is correct
55 Correct 149 ms 42524 KB Output is correct
56 Correct 194 ms 41536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 5 ms 5076 KB Output is correct
5 Correct 5 ms 5260 KB Output is correct
6 Correct 5 ms 5332 KB Output is correct
7 Correct 4 ms 5332 KB Output is correct
8 Correct 4 ms 5328 KB Output is correct
9 Correct 195 ms 33976 KB Output is correct
10 Correct 218 ms 40912 KB Output is correct
11 Correct 193 ms 40056 KB Output is correct
12 Correct 300 ms 42320 KB Output is correct
13 Correct 249 ms 41052 KB Output is correct
14 Correct 200 ms 33456 KB Output is correct
15 Correct 301 ms 43172 KB Output is correct
16 Correct 259 ms 40700 KB Output is correct
17 Correct 286 ms 45612 KB Output is correct
18 Correct 237 ms 41652 KB Output is correct
19 Correct 279 ms 36672 KB Output is correct
20 Correct 305 ms 38028 KB Output is correct
21 Correct 316 ms 37252 KB Output is correct
22 Correct 274 ms 38288 KB Output is correct
23 Correct 365 ms 38028 KB Output is correct
24 Correct 302 ms 36972 KB Output is correct
25 Correct 295 ms 38020 KB Output is correct
26 Correct 342 ms 36948 KB Output is correct
27 Correct 7 ms 5304 KB Output is correct
28 Correct 6 ms 5332 KB Output is correct
29 Correct 5 ms 5332 KB Output is correct
30 Correct 6 ms 5280 KB Output is correct
31 Correct 6 ms 5332 KB Output is correct
32 Correct 6 ms 5332 KB Output is correct
33 Correct 4 ms 5288 KB Output is correct
34 Correct 6 ms 5376 KB Output is correct
35 Correct 6 ms 5284 KB Output is correct
36 Correct 22 ms 9584 KB Output is correct
37 Correct 225 ms 43760 KB Output is correct
38 Correct 252 ms 41576 KB Output is correct
39 Correct 223 ms 39644 KB Output is correct
40 Correct 224 ms 38664 KB Output is correct
41 Correct 221 ms 38000 KB Output is correct
42 Correct 213 ms 35308 KB Output is correct
43 Correct 234 ms 42632 KB Output is correct
44 Correct 248 ms 43456 KB Output is correct
45 Correct 167 ms 41448 KB Output is correct
46 Correct 215 ms 39016 KB Output is correct
47 Correct 26 ms 9724 KB Output is correct
48 Correct 437 ms 47232 KB Output is correct
49 Correct 419 ms 45456 KB Output is correct
50 Correct 443 ms 44696 KB Output is correct
51 Correct 414 ms 43812 KB Output is correct
52 Correct 348 ms 41332 KB Output is correct
53 Correct 280 ms 33444 KB Output is correct
54 Correct 418 ms 47496 KB Output is correct
55 Correct 430 ms 47840 KB Output is correct
56 Correct 343 ms 46936 KB Output is correct
57 Correct 362 ms 44288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 5 ms 5076 KB Output is correct
6 Correct 5 ms 5260 KB Output is correct
7 Correct 5 ms 5332 KB Output is correct
8 Correct 4 ms 5332 KB Output is correct
9 Correct 4 ms 5328 KB Output is correct
10 Correct 195 ms 33976 KB Output is correct
11 Correct 218 ms 40912 KB Output is correct
12 Correct 193 ms 40056 KB Output is correct
13 Correct 300 ms 42320 KB Output is correct
14 Correct 249 ms 41052 KB Output is correct
15 Correct 200 ms 33456 KB Output is correct
16 Correct 301 ms 43172 KB Output is correct
17 Correct 259 ms 40700 KB Output is correct
18 Correct 286 ms 45612 KB Output is correct
19 Correct 237 ms 41652 KB Output is correct
20 Correct 279 ms 36672 KB Output is correct
21 Correct 305 ms 38028 KB Output is correct
22 Correct 316 ms 37252 KB Output is correct
23 Correct 274 ms 38288 KB Output is correct
24 Correct 365 ms 38028 KB Output is correct
25 Correct 302 ms 36972 KB Output is correct
26 Correct 295 ms 38020 KB Output is correct
27 Correct 342 ms 36948 KB Output is correct
28 Correct 7 ms 5304 KB Output is correct
29 Correct 6 ms 5332 KB Output is correct
30 Correct 5 ms 5332 KB Output is correct
31 Correct 6 ms 5280 KB Output is correct
32 Correct 6 ms 5332 KB Output is correct
33 Correct 6 ms 5332 KB Output is correct
34 Correct 4 ms 5288 KB Output is correct
35 Correct 6 ms 5376 KB Output is correct
36 Correct 6 ms 5284 KB Output is correct
37 Correct 22 ms 9584 KB Output is correct
38 Correct 225 ms 43760 KB Output is correct
39 Correct 252 ms 41576 KB Output is correct
40 Correct 223 ms 39644 KB Output is correct
41 Correct 224 ms 38664 KB Output is correct
42 Correct 221 ms 38000 KB Output is correct
43 Correct 213 ms 35308 KB Output is correct
44 Correct 234 ms 42632 KB Output is correct
45 Correct 248 ms 43456 KB Output is correct
46 Correct 167 ms 41448 KB Output is correct
47 Correct 215 ms 39016 KB Output is correct
48 Correct 26 ms 9724 KB Output is correct
49 Correct 437 ms 47232 KB Output is correct
50 Correct 419 ms 45456 KB Output is correct
51 Correct 443 ms 44696 KB Output is correct
52 Correct 414 ms 43812 KB Output is correct
53 Correct 348 ms 41332 KB Output is correct
54 Correct 280 ms 33444 KB Output is correct
55 Correct 418 ms 47496 KB Output is correct
56 Correct 430 ms 47840 KB Output is correct
57 Correct 343 ms 46936 KB Output is correct
58 Correct 362 ms 44288 KB Output is correct
59 Correct 114 ms 14512 KB Output is correct
60 Correct 421 ms 43704 KB Output is correct
61 Correct 507 ms 41872 KB Output is correct
62 Correct 555 ms 45192 KB Output is correct
63 Correct 538 ms 42700 KB Output is correct
64 Correct 5 ms 5280 KB Output is correct
65 Correct 5 ms 5332 KB Output is correct
66 Correct 5 ms 5348 KB Output is correct
67 Correct 5 ms 5156 KB Output is correct
68 Correct 5 ms 5204 KB Output is correct
69 Correct 7 ms 5332 KB Output is correct
70 Correct 7 ms 5412 KB Output is correct
71 Correct 6 ms 5424 KB Output is correct
72 Correct 4 ms 5348 KB Output is correct
73 Correct 5 ms 5416 KB Output is correct
74 Correct 163 ms 34304 KB Output is correct
75 Correct 188 ms 40720 KB Output is correct
76 Correct 188 ms 37792 KB Output is correct
77 Correct 207 ms 36880 KB Output is correct
78 Correct 64 ms 14652 KB Output is correct
79 Correct 60 ms 13568 KB Output is correct
80 Correct 134 ms 29404 KB Output is correct
81 Correct 235 ms 44132 KB Output is correct
82 Correct 202 ms 42792 KB Output is correct
83 Correct 229 ms 47908 KB Output is correct
84 Correct 149 ms 42524 KB Output is correct
85 Correct 194 ms 41536 KB Output is correct
86 Correct 80 ms 17248 KB Output is correct
87 Correct 420 ms 44980 KB Output is correct
88 Correct 409 ms 45216 KB Output is correct
89 Correct 351 ms 39824 KB Output is correct
90 Correct 167 ms 18496 KB Output is correct
91 Correct 174 ms 20540 KB Output is correct
92 Correct 285 ms 34336 KB Output is correct
93 Correct 475 ms 48700 KB Output is correct
94 Correct 371 ms 47304 KB Output is correct
95 Correct 464 ms 51860 KB Output is correct
96 Correct 377 ms 45412 KB Output is correct
97 Correct 345 ms 44568 KB Output is correct