답안 #429647

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
429647 2021-06-16T08:19:39 Z Peti 자매 도시 (APIO20_swap) C++14
100 / 100
523 ms 57384 KB
#include <bits/stdc++.h>
#include "swap.h"

using namespace std;

const int logn = 20;

struct node{
    int l = -1, r = -1, os = -1, be, ki, ert = 0;
    bool jo = false;
};

struct el{
    int u, v, c;
    bool operator < (const el &e) const{
        return c < e.c;
    }
};

vector<node> g;

struct union_find{
    vector<int> vos, vm, ind, fok;
    vector<bool> jo;

    void init(int n){
        vm.assign(n, 1);
        vos.assign(n, -1);
        fok.assign(n, 0);
        jo.assign(n, false);
        ind.resize(n);
        iota(ind.begin(), ind.end(), 0);
    }

    int os(int x){
        return (vos[x] == -1 ? x : vos[x] = os(vos[x]));
    }

    bool unio(int a, int b, int ert){
        //cout<<"union "<<a<<" "<<b<<"\n";
        fok[a]++;
        fok[b]++;
        bool kesz = fok[a] > 2 || fok[b] > 2;
        a = os(a);
        b = os(b);

        if(a == b){
            jo[a] = true;
            g.push_back({ind[a], -1, -1, 0, 0, ert, jo[a]});
            ind[a] = (int)g.size()-1;
            return false;
        }
        if(vm[a] < vm[b]) swap(a, b);

        vos[b] = a;
        vm[a] += (int)(vm[a] == vm[b]);
        /*cout<<"new node: "<<ind[a]<<" "<<ind[b]<<" | "<<g.size()<<"\n";
        cout<<"can solve: "<<jo[a]<<" "<<jo[b]<<" "<<kesz<<"\n";*/
        if(jo[b] || kesz)
            jo[a] = true;
        g.push_back({ind[a], ind[b], -1, 0, 0, ert, jo[a]});
        ind[a] = (int)g.size()-1;
        return true;
    }
};

int ido = 0;
void bejar(int akt, int os){
    //cout<<akt<<" bejar\n";
    if(akt == -1) return;
    g[akt].os = os;
    g[akt].be = ido++;
    bejar(g[akt].l, akt);
    bejar(g[akt].r, akt);
    g[akt].ki = ido++;
}

vector<vector<int>> lca;
union_find csop;
int n;

int ose(int a, int b){
    return g[a].be <= g[b].be && g[b].ki <= g[a].ki;
}

int get_lca(int a, int b){
    if(ose(a, b))
        return a;
    for(int i = logn-1; i >= 0; i--)
        if(!ose(lca[a][i], b))
            a = lca[a][i];
    return lca[a][0];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    vector<el> v(M);
    for(int i = 0; i < M; i++)
        v[i] = {U[i], V[i], W[i]};
    sort(v.begin(), v.end());


    g.assign(N, node());
    csop.init(N);
    for(el e : v)
        csop.unio(e.u, e.v, e.c);
    n = (int)g.size();
    bejar(n-1, n-1);

    /*cout<<n<<" g size\n";
    for(int i = 0; i < n; i++)
        cout<<g[i].os<<" ";
    cout<<"\n";*/

    lca.assign(n, vector<int>(logn, n-1));
    for(int i = 0; i < n; i++)
        lca[i][0] = g[i].os;

    for(int j = 1; j < logn; j++)
        for(int i = 0; i < n; i++)
            lca[i][j] = lca[lca[i][j-1]][j-1];
}

int getMinimumFuelCapacity(int X, int Y) {
    int p = get_lca(X, Y);

    if(g[p].jo)
        return g[p].ert;

    for(int i = logn-1; i >= 0; i--)
        if(!g[lca[p][i]].jo)
            p = lca[p][i];
    p = lca[p][0];

    if(g[p].jo)
        return g[p].ert;
    return -1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 108 ms 27012 KB Output is correct
10 Correct 131 ms 33016 KB Output is correct
11 Correct 128 ms 32468 KB Output is correct
12 Correct 143 ms 34320 KB Output is correct
13 Correct 188 ms 35844 KB Output is correct
14 Correct 120 ms 27068 KB Output is correct
15 Correct 309 ms 34828 KB Output is correct
16 Correct 301 ms 33788 KB Output is correct
17 Correct 320 ms 35936 KB Output is correct
18 Correct 523 ms 37412 KB Output is correct
19 Correct 95 ms 8840 KB Output is correct
20 Correct 264 ms 35128 KB Output is correct
21 Correct 256 ms 34156 KB Output is correct
22 Correct 300 ms 36368 KB Output is correct
23 Correct 508 ms 37900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 455 ms 37388 KB Output is correct
4 Correct 465 ms 39060 KB Output is correct
5 Correct 440 ms 38036 KB Output is correct
6 Correct 450 ms 38896 KB Output is correct
7 Correct 479 ms 38584 KB Output is correct
8 Correct 420 ms 37116 KB Output is correct
9 Correct 461 ms 38264 KB Output is correct
10 Correct 432 ms 36756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 3 ms 588 KB Output is correct
12 Correct 2 ms 588 KB Output is correct
13 Correct 2 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 588 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 2 ms 644 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 2 ms 460 KB Output is correct
24 Correct 2 ms 716 KB Output is correct
25 Correct 2 ms 716 KB Output is correct
26 Correct 2 ms 844 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 600 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 108 ms 27012 KB Output is correct
11 Correct 131 ms 33016 KB Output is correct
12 Correct 128 ms 32468 KB Output is correct
13 Correct 143 ms 34320 KB Output is correct
14 Correct 188 ms 35844 KB Output is correct
15 Correct 2 ms 588 KB Output is correct
16 Correct 3 ms 588 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 2 ms 460 KB Output is correct
19 Correct 1 ms 460 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 1 ms 588 KB Output is correct
22 Correct 2 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 2 ms 644 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 2 ms 460 KB Output is correct
29 Correct 2 ms 716 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 588 KB Output is correct
33 Correct 2 ms 844 KB Output is correct
34 Correct 14 ms 4892 KB Output is correct
35 Correct 136 ms 34320 KB Output is correct
36 Correct 128 ms 34296 KB Output is correct
37 Correct 126 ms 34320 KB Output is correct
38 Correct 128 ms 33968 KB Output is correct
39 Correct 127 ms 33736 KB Output is correct
40 Correct 113 ms 30884 KB Output is correct
41 Correct 130 ms 34368 KB Output is correct
42 Correct 136 ms 34580 KB Output is correct
43 Correct 148 ms 36628 KB Output is correct
44 Correct 139 ms 34664 KB Output is correct
45 Correct 212 ms 42428 KB Output is correct
46 Correct 151 ms 34640 KB Output is correct
47 Correct 162 ms 34612 KB Output is correct
48 Correct 174 ms 37388 KB Output is correct
49 Correct 145 ms 34028 KB Output is correct
50 Correct 113 ms 27036 KB Output is correct
51 Correct 166 ms 36528 KB Output is correct
52 Correct 184 ms 47500 KB Output is correct
53 Correct 245 ms 50612 KB Output is correct
54 Correct 226 ms 55704 KB Output is correct
55 Correct 164 ms 36528 KB Output is correct
56 Correct 280 ms 50812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 108 ms 27012 KB Output is correct
10 Correct 131 ms 33016 KB Output is correct
11 Correct 128 ms 32468 KB Output is correct
12 Correct 143 ms 34320 KB Output is correct
13 Correct 188 ms 35844 KB Output is correct
14 Correct 120 ms 27068 KB Output is correct
15 Correct 309 ms 34828 KB Output is correct
16 Correct 301 ms 33788 KB Output is correct
17 Correct 320 ms 35936 KB Output is correct
18 Correct 523 ms 37412 KB Output is correct
19 Correct 455 ms 37388 KB Output is correct
20 Correct 465 ms 39060 KB Output is correct
21 Correct 440 ms 38036 KB Output is correct
22 Correct 450 ms 38896 KB Output is correct
23 Correct 479 ms 38584 KB Output is correct
24 Correct 420 ms 37116 KB Output is correct
25 Correct 461 ms 38264 KB Output is correct
26 Correct 432 ms 36756 KB Output is correct
27 Correct 2 ms 588 KB Output is correct
28 Correct 3 ms 588 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 2 ms 460 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 2 ms 604 KB Output is correct
33 Correct 1 ms 588 KB Output is correct
34 Correct 2 ms 588 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 14 ms 4892 KB Output is correct
37 Correct 136 ms 34320 KB Output is correct
38 Correct 128 ms 34296 KB Output is correct
39 Correct 126 ms 34320 KB Output is correct
40 Correct 128 ms 33968 KB Output is correct
41 Correct 127 ms 33736 KB Output is correct
42 Correct 113 ms 30884 KB Output is correct
43 Correct 130 ms 34368 KB Output is correct
44 Correct 136 ms 34580 KB Output is correct
45 Correct 148 ms 36628 KB Output is correct
46 Correct 139 ms 34664 KB Output is correct
47 Correct 31 ms 4952 KB Output is correct
48 Correct 262 ms 36148 KB Output is correct
49 Correct 284 ms 36240 KB Output is correct
50 Correct 296 ms 36012 KB Output is correct
51 Correct 295 ms 35832 KB Output is correct
52 Correct 250 ms 33928 KB Output is correct
53 Correct 273 ms 25864 KB Output is correct
54 Correct 268 ms 36576 KB Output is correct
55 Correct 303 ms 36124 KB Output is correct
56 Correct 447 ms 37828 KB Output is correct
57 Correct 300 ms 36708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 2 ms 600 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 588 KB Output is correct
10 Correct 108 ms 27012 KB Output is correct
11 Correct 131 ms 33016 KB Output is correct
12 Correct 128 ms 32468 KB Output is correct
13 Correct 143 ms 34320 KB Output is correct
14 Correct 188 ms 35844 KB Output is correct
15 Correct 120 ms 27068 KB Output is correct
16 Correct 309 ms 34828 KB Output is correct
17 Correct 301 ms 33788 KB Output is correct
18 Correct 320 ms 35936 KB Output is correct
19 Correct 523 ms 37412 KB Output is correct
20 Correct 455 ms 37388 KB Output is correct
21 Correct 465 ms 39060 KB Output is correct
22 Correct 440 ms 38036 KB Output is correct
23 Correct 450 ms 38896 KB Output is correct
24 Correct 479 ms 38584 KB Output is correct
25 Correct 420 ms 37116 KB Output is correct
26 Correct 461 ms 38264 KB Output is correct
27 Correct 432 ms 36756 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 3 ms 588 KB Output is correct
30 Correct 2 ms 588 KB Output is correct
31 Correct 2 ms 460 KB Output is correct
32 Correct 1 ms 460 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 1 ms 588 KB Output is correct
35 Correct 2 ms 588 KB Output is correct
36 Correct 2 ms 588 KB Output is correct
37 Correct 14 ms 4892 KB Output is correct
38 Correct 136 ms 34320 KB Output is correct
39 Correct 128 ms 34296 KB Output is correct
40 Correct 126 ms 34320 KB Output is correct
41 Correct 128 ms 33968 KB Output is correct
42 Correct 127 ms 33736 KB Output is correct
43 Correct 113 ms 30884 KB Output is correct
44 Correct 130 ms 34368 KB Output is correct
45 Correct 136 ms 34580 KB Output is correct
46 Correct 148 ms 36628 KB Output is correct
47 Correct 139 ms 34664 KB Output is correct
48 Correct 31 ms 4952 KB Output is correct
49 Correct 262 ms 36148 KB Output is correct
50 Correct 284 ms 36240 KB Output is correct
51 Correct 296 ms 36012 KB Output is correct
52 Correct 295 ms 35832 KB Output is correct
53 Correct 250 ms 33928 KB Output is correct
54 Correct 273 ms 25864 KB Output is correct
55 Correct 268 ms 36576 KB Output is correct
56 Correct 303 ms 36124 KB Output is correct
57 Correct 447 ms 37828 KB Output is correct
58 Correct 300 ms 36708 KB Output is correct
59 Correct 95 ms 8840 KB Output is correct
60 Correct 264 ms 35128 KB Output is correct
61 Correct 256 ms 34156 KB Output is correct
62 Correct 300 ms 36368 KB Output is correct
63 Correct 508 ms 37900 KB Output is correct
64 Correct 1 ms 460 KB Output is correct
65 Correct 1 ms 588 KB Output is correct
66 Correct 2 ms 644 KB Output is correct
67 Correct 2 ms 588 KB Output is correct
68 Correct 2 ms 460 KB Output is correct
69 Correct 2 ms 716 KB Output is correct
70 Correct 2 ms 716 KB Output is correct
71 Correct 2 ms 844 KB Output is correct
72 Correct 2 ms 588 KB Output is correct
73 Correct 2 ms 844 KB Output is correct
74 Correct 212 ms 42428 KB Output is correct
75 Correct 151 ms 34640 KB Output is correct
76 Correct 162 ms 34612 KB Output is correct
77 Correct 174 ms 37388 KB Output is correct
78 Correct 145 ms 34028 KB Output is correct
79 Correct 113 ms 27036 KB Output is correct
80 Correct 166 ms 36528 KB Output is correct
81 Correct 184 ms 47500 KB Output is correct
82 Correct 245 ms 50612 KB Output is correct
83 Correct 226 ms 55704 KB Output is correct
84 Correct 164 ms 36528 KB Output is correct
85 Correct 280 ms 50812 KB Output is correct
86 Correct 111 ms 15848 KB Output is correct
87 Correct 273 ms 36120 KB Output is correct
88 Correct 273 ms 36148 KB Output is correct
89 Correct 486 ms 37068 KB Output is correct
90 Correct 238 ms 37676 KB Output is correct
91 Correct 276 ms 35040 KB Output is correct
92 Correct 417 ms 36912 KB Output is correct
93 Correct 355 ms 48552 KB Output is correct
94 Correct 487 ms 52012 KB Output is correct
95 Correct 417 ms 57384 KB Output is correct
96 Correct 411 ms 37948 KB Output is correct
97 Correct 480 ms 44508 KB Output is correct