Submission #1070682

# Submission time Handle Problem Language Result Execution time Memory
1070682 2024-08-22T16:46:45 Z vjudge1 Swapping Cities (APIO20_swap) C++17
100 / 100
458 ms 102672 KB
///*** Sown_Vipro ***///
/// ->TUYEN QUOC GIA<- ///

#include "swap.h"
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
#define F first
#define S second
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i)
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
#define szz(s) int(s.size())
const int N = 1e6 + 5, MAX = 1e6, oo = 1e9 + 5, MOD = 1e9 + 7;
int n, q, nn, m;
int p[N], b[N], check[N], nxt[25][N], h[N];
vector<int> adj[N];

struct edge{
    int u, v, w;
} e[N];

bool cmp(edge a, edge b){
    return a.w < b.w;
}

int Find(int u){
    if(u == p[u]) return u;
    return p[u] = Find(p[u]);
}

void Union(int u, int v){
    ++b[u]; ++b[v];
    int uu = u, vv = v;
    ++n;
    p[n] = n;
    u = Find(u); v = Find(v);
    if(u == v){
        p[u] = n;
        adj[u].pb(n);
        adj[n].pb(u);
        check[n] = 1;
//        cout << uu << " " << vv << " " << n << " " << Find(4) << " " << Find(5) << "\n";
    }else{
        p[u] = n; p[v] = n;
        check[n] = (check[u] || check[v] || b[uu] > 2 || b[vv] > 2);
        adj[u].pb(n);
        adj[n].pb(u);
        adj[v].pb(n);
        adj[n].pb(v);
//        cout << uu << " " << vv << " " << n << " " << Find(4) << " " << Find(5) << "\n";
    }
}

void dfs(int u){
//    cout << u << " " << h[u] << " " << nxt[0][u] << "\n";
    for(int v : adj[u]){
        if(v == nxt[0][u]) continue;
        h[v] = h[u] + 1;
        nxt[0][v] = u;
        dfs(v);
    }
}

int lca(int u, int v){
    if(h[u] < h[v]) swap(u, v);
    REP(k, 20, 0){
        int pu = nxt[k][u];
//        cout << k << " " << u << " " << pu << "\n";
//        cout << u << " " << v << " " << h[u] << " " << h[v] << "\n";
        if(h[pu] >= h[v]){
            u = pu;
        }
    }
    if(u == v) return u;
    REP(k, 20, 0){
        int pu = nxt[k][u], pv = nxt[k][v];
        if(pu != pv){
            u = pu; v = pv;
        }
    }
    return nxt[0][u];
}

int lift(int u, int mid){
    REP(k, 20, 0){
        if((1 << k) <= mid){
            u = nxt[k][u];
            mid -= (1 << k);
        }
    }
    return u;
}

int getMinimumFuelCapacity(int u, int v){
    int p = lca(u, v);
    int l = 0, r = h[p];
//    cout << lca << "\n";
    while(l <= r){
        int mid = (l + r) / 2;
        if(check[lift(p, mid)]) r = mid - 1;
        else l = mid + 1;
    }

    if(l > h[p]) return -1;
    return e[lift(p, l) - nn].w;
}

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W){
    n = N; m = M;
    nn = N;
    FOR(i, 1, m){
        e[i] = {U[i - 1], V[i - 1], W[i - 1]};
    }
    sort(e + 1, e + 1 + m, cmp);
    FOR(u, 0, n) p[u] = u;
    FOR(i, 1, m){
        int u = e[i].u, v = e[i].v;

        Union(u, v);
    }

    FOR(k, 0, 20){
        FOR(u, 0, n + 1){
            nxt[k][u] = n + 1;
        }
    }

    h[n] = 1;
    dfs(n);

    FOR(k, 1, 20){
        FOR(u, 0, n){
            nxt[k][u] = nxt[k - 1][nxt[k - 1][u]];
        }
    }
}

//int main(){
//    inp("in.txt");
//////    out("out.txt");
//    cin >> n >> m;
//    nn = n;
//    FOR(i, 1, m){
//        int u, v, w; cin >> u >> v >> w;
//        e[i] = {u, v, w};
//    }
//    sort(e + 1, e + 1 + m, cmp);
//    FOR(u, 0, n) p[u] = u;
//    FOR(i, 1, m){
//        int u = e[i].u, v = e[i].v;
//
//        Union(u, v);
//    }
//
//    FOR(k, 0, 20){
//        FOR(u, 0, n + 1){
//            nxt[k][u] = n + 1;
//        }
//    }
//
//    h[n] = 1;
//    dfs(n);
//    FOR(k, 1, 20){
//        FOR(u, 0, n){
//            nxt[k][u] = nxt[k - 1][nxt[k - 1][u]];
//        }
//    }
////    cout << nxt[2][nxt[2][4]] << '\n';
//
//    cin >> q;
//    while(q--){
//        int u, v; cin >> u >> v;
//        cout << getMinimumFuelCapacity(u, v) << "\n";
//    }
//}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 53848 KB Output is correct
2 Correct 8 ms 51804 KB Output is correct
3 Correct 7 ms 51660 KB Output is correct
4 Correct 8 ms 53852 KB Output is correct
5 Correct 6 ms 51804 KB Output is correct
6 Correct 8 ms 55900 KB Output is correct
7 Correct 7 ms 55900 KB Output is correct
8 Correct 7 ms 49780 KB Output is correct
9 Correct 66 ms 67408 KB Output is correct
10 Correct 88 ms 73300 KB Output is correct
11 Correct 79 ms 73044 KB Output is correct
12 Correct 86 ms 72272 KB Output is correct
13 Correct 75 ms 77136 KB Output is correct
14 Correct 77 ms 68948 KB Output is correct
15 Correct 203 ms 75260 KB Output is correct
16 Correct 206 ms 74752 KB Output is correct
17 Correct 221 ms 75964 KB Output is correct
18 Correct 357 ms 77204 KB Output is correct
19 Correct 112 ms 58452 KB Output is correct
20 Correct 196 ms 74628 KB Output is correct
21 Correct 200 ms 76028 KB Output is correct
22 Correct 208 ms 78656 KB Output is correct
23 Correct 377 ms 81596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 53848 KB Output is correct
2 Correct 8 ms 51804 KB Output is correct
3 Correct 357 ms 83188 KB Output is correct
4 Correct 416 ms 88076 KB Output is correct
5 Correct 387 ms 86160 KB Output is correct
6 Correct 416 ms 87484 KB Output is correct
7 Correct 382 ms 86484 KB Output is correct
8 Correct 379 ms 85096 KB Output is correct
9 Correct 374 ms 84980 KB Output is correct
10 Correct 373 ms 83720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 53848 KB Output is correct
2 Correct 8 ms 51804 KB Output is correct
3 Correct 7 ms 51660 KB Output is correct
4 Correct 8 ms 53852 KB Output is correct
5 Correct 6 ms 51804 KB Output is correct
6 Correct 8 ms 55900 KB Output is correct
7 Correct 7 ms 55900 KB Output is correct
8 Correct 7 ms 49780 KB Output is correct
9 Correct 6 ms 53848 KB Output is correct
10 Correct 7 ms 51804 KB Output is correct
11 Correct 8 ms 51804 KB Output is correct
12 Correct 7 ms 51800 KB Output is correct
13 Correct 7 ms 51932 KB Output is correct
14 Correct 7 ms 51804 KB Output is correct
15 Correct 8 ms 53988 KB Output is correct
16 Correct 9 ms 53960 KB Output is correct
17 Correct 7 ms 52060 KB Output is correct
18 Correct 7 ms 52060 KB Output is correct
19 Correct 8 ms 51800 KB Output is correct
20 Correct 8 ms 51936 KB Output is correct
21 Correct 8 ms 52060 KB Output is correct
22 Correct 8 ms 51972 KB Output is correct
23 Correct 8 ms 53852 KB Output is correct
24 Correct 9 ms 52192 KB Output is correct
25 Correct 8 ms 52060 KB Output is correct
26 Correct 9 ms 54108 KB Output is correct
27 Correct 7 ms 52060 KB Output is correct
28 Correct 8 ms 52224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 53848 KB Output is correct
2 Correct 7 ms 53848 KB Output is correct
3 Correct 8 ms 51804 KB Output is correct
4 Correct 7 ms 51660 KB Output is correct
5 Correct 8 ms 53852 KB Output is correct
6 Correct 6 ms 51804 KB Output is correct
7 Correct 8 ms 55900 KB Output is correct
8 Correct 7 ms 55900 KB Output is correct
9 Correct 7 ms 49780 KB Output is correct
10 Correct 66 ms 67408 KB Output is correct
11 Correct 88 ms 73300 KB Output is correct
12 Correct 79 ms 73044 KB Output is correct
13 Correct 86 ms 72272 KB Output is correct
14 Correct 75 ms 77136 KB Output is correct
15 Correct 7 ms 51804 KB Output is correct
16 Correct 8 ms 51804 KB Output is correct
17 Correct 7 ms 51800 KB Output is correct
18 Correct 7 ms 51932 KB Output is correct
19 Correct 7 ms 51804 KB Output is correct
20 Correct 8 ms 53988 KB Output is correct
21 Correct 9 ms 53960 KB Output is correct
22 Correct 7 ms 52060 KB Output is correct
23 Correct 7 ms 52060 KB Output is correct
24 Correct 8 ms 51800 KB Output is correct
25 Correct 8 ms 51936 KB Output is correct
26 Correct 8 ms 52060 KB Output is correct
27 Correct 8 ms 51972 KB Output is correct
28 Correct 8 ms 53852 KB Output is correct
29 Correct 9 ms 52192 KB Output is correct
30 Correct 8 ms 52060 KB Output is correct
31 Correct 9 ms 54108 KB Output is correct
32 Correct 7 ms 52060 KB Output is correct
33 Correct 8 ms 52224 KB Output is correct
34 Correct 18 ms 54620 KB Output is correct
35 Correct 81 ms 75508 KB Output is correct
36 Correct 89 ms 77904 KB Output is correct
37 Correct 93 ms 77772 KB Output is correct
38 Correct 76 ms 80116 KB Output is correct
39 Correct 80 ms 78672 KB Output is correct
40 Correct 75 ms 77104 KB Output is correct
41 Correct 89 ms 79316 KB Output is correct
42 Correct 77 ms 80464 KB Output is correct
43 Correct 78 ms 84604 KB Output is correct
44 Correct 93 ms 80464 KB Output is correct
45 Correct 108 ms 89880 KB Output is correct
46 Correct 91 ms 80352 KB Output is correct
47 Correct 81 ms 80464 KB Output is correct
48 Correct 82 ms 84560 KB Output is correct
49 Correct 83 ms 86868 KB Output is correct
50 Correct 74 ms 81744 KB Output is correct
51 Correct 92 ms 84912 KB Output is correct
52 Correct 118 ms 93520 KB Output is correct
53 Correct 132 ms 94288 KB Output is correct
54 Correct 146 ms 101884 KB Output is correct
55 Correct 71 ms 82952 KB Output is correct
56 Correct 121 ms 97408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 53848 KB Output is correct
2 Correct 8 ms 51804 KB Output is correct
3 Correct 7 ms 51660 KB Output is correct
4 Correct 8 ms 53852 KB Output is correct
5 Correct 6 ms 51804 KB Output is correct
6 Correct 8 ms 55900 KB Output is correct
7 Correct 7 ms 55900 KB Output is correct
8 Correct 7 ms 49780 KB Output is correct
9 Correct 66 ms 67408 KB Output is correct
10 Correct 88 ms 73300 KB Output is correct
11 Correct 79 ms 73044 KB Output is correct
12 Correct 86 ms 72272 KB Output is correct
13 Correct 75 ms 77136 KB Output is correct
14 Correct 77 ms 68948 KB Output is correct
15 Correct 203 ms 75260 KB Output is correct
16 Correct 206 ms 74752 KB Output is correct
17 Correct 221 ms 75964 KB Output is correct
18 Correct 357 ms 77204 KB Output is correct
19 Correct 357 ms 83188 KB Output is correct
20 Correct 416 ms 88076 KB Output is correct
21 Correct 387 ms 86160 KB Output is correct
22 Correct 416 ms 87484 KB Output is correct
23 Correct 382 ms 86484 KB Output is correct
24 Correct 379 ms 85096 KB Output is correct
25 Correct 374 ms 84980 KB Output is correct
26 Correct 373 ms 83720 KB Output is correct
27 Correct 7 ms 51804 KB Output is correct
28 Correct 8 ms 51804 KB Output is correct
29 Correct 7 ms 51800 KB Output is correct
30 Correct 7 ms 51932 KB Output is correct
31 Correct 7 ms 51804 KB Output is correct
32 Correct 8 ms 53988 KB Output is correct
33 Correct 9 ms 53960 KB Output is correct
34 Correct 7 ms 52060 KB Output is correct
35 Correct 7 ms 52060 KB Output is correct
36 Correct 18 ms 54620 KB Output is correct
37 Correct 81 ms 75508 KB Output is correct
38 Correct 89 ms 77904 KB Output is correct
39 Correct 93 ms 77772 KB Output is correct
40 Correct 76 ms 80116 KB Output is correct
41 Correct 80 ms 78672 KB Output is correct
42 Correct 75 ms 77104 KB Output is correct
43 Correct 89 ms 79316 KB Output is correct
44 Correct 77 ms 80464 KB Output is correct
45 Correct 78 ms 84604 KB Output is correct
46 Correct 93 ms 80464 KB Output is correct
47 Correct 26 ms 60764 KB Output is correct
48 Correct 214 ms 82788 KB Output is correct
49 Correct 192 ms 82732 KB Output is correct
50 Correct 201 ms 82660 KB Output is correct
51 Correct 189 ms 83904 KB Output is correct
52 Correct 194 ms 81672 KB Output is correct
53 Correct 181 ms 75844 KB Output is correct
54 Correct 193 ms 84916 KB Output is correct
55 Correct 189 ms 84152 KB Output is correct
56 Correct 311 ms 86236 KB Output is correct
57 Correct 215 ms 83832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 53848 KB Output is correct
2 Correct 7 ms 53848 KB Output is correct
3 Correct 8 ms 51804 KB Output is correct
4 Correct 7 ms 51660 KB Output is correct
5 Correct 8 ms 53852 KB Output is correct
6 Correct 6 ms 51804 KB Output is correct
7 Correct 8 ms 55900 KB Output is correct
8 Correct 7 ms 55900 KB Output is correct
9 Correct 7 ms 49780 KB Output is correct
10 Correct 66 ms 67408 KB Output is correct
11 Correct 88 ms 73300 KB Output is correct
12 Correct 79 ms 73044 KB Output is correct
13 Correct 86 ms 72272 KB Output is correct
14 Correct 75 ms 77136 KB Output is correct
15 Correct 77 ms 68948 KB Output is correct
16 Correct 203 ms 75260 KB Output is correct
17 Correct 206 ms 74752 KB Output is correct
18 Correct 221 ms 75964 KB Output is correct
19 Correct 357 ms 77204 KB Output is correct
20 Correct 357 ms 83188 KB Output is correct
21 Correct 416 ms 88076 KB Output is correct
22 Correct 387 ms 86160 KB Output is correct
23 Correct 416 ms 87484 KB Output is correct
24 Correct 382 ms 86484 KB Output is correct
25 Correct 379 ms 85096 KB Output is correct
26 Correct 374 ms 84980 KB Output is correct
27 Correct 373 ms 83720 KB Output is correct
28 Correct 7 ms 51804 KB Output is correct
29 Correct 8 ms 51804 KB Output is correct
30 Correct 7 ms 51800 KB Output is correct
31 Correct 7 ms 51932 KB Output is correct
32 Correct 7 ms 51804 KB Output is correct
33 Correct 8 ms 53988 KB Output is correct
34 Correct 9 ms 53960 KB Output is correct
35 Correct 7 ms 52060 KB Output is correct
36 Correct 7 ms 52060 KB Output is correct
37 Correct 18 ms 54620 KB Output is correct
38 Correct 81 ms 75508 KB Output is correct
39 Correct 89 ms 77904 KB Output is correct
40 Correct 93 ms 77772 KB Output is correct
41 Correct 76 ms 80116 KB Output is correct
42 Correct 80 ms 78672 KB Output is correct
43 Correct 75 ms 77104 KB Output is correct
44 Correct 89 ms 79316 KB Output is correct
45 Correct 77 ms 80464 KB Output is correct
46 Correct 78 ms 84604 KB Output is correct
47 Correct 93 ms 80464 KB Output is correct
48 Correct 26 ms 60764 KB Output is correct
49 Correct 214 ms 82788 KB Output is correct
50 Correct 192 ms 82732 KB Output is correct
51 Correct 201 ms 82660 KB Output is correct
52 Correct 189 ms 83904 KB Output is correct
53 Correct 194 ms 81672 KB Output is correct
54 Correct 181 ms 75844 KB Output is correct
55 Correct 193 ms 84916 KB Output is correct
56 Correct 189 ms 84152 KB Output is correct
57 Correct 311 ms 86236 KB Output is correct
58 Correct 215 ms 83832 KB Output is correct
59 Correct 112 ms 58452 KB Output is correct
60 Correct 196 ms 74628 KB Output is correct
61 Correct 200 ms 76028 KB Output is correct
62 Correct 208 ms 78656 KB Output is correct
63 Correct 377 ms 81596 KB Output is correct
64 Correct 8 ms 51800 KB Output is correct
65 Correct 8 ms 51936 KB Output is correct
66 Correct 8 ms 52060 KB Output is correct
67 Correct 8 ms 51972 KB Output is correct
68 Correct 8 ms 53852 KB Output is correct
69 Correct 9 ms 52192 KB Output is correct
70 Correct 8 ms 52060 KB Output is correct
71 Correct 9 ms 54108 KB Output is correct
72 Correct 7 ms 52060 KB Output is correct
73 Correct 8 ms 52224 KB Output is correct
74 Correct 108 ms 89880 KB Output is correct
75 Correct 91 ms 80352 KB Output is correct
76 Correct 81 ms 80464 KB Output is correct
77 Correct 82 ms 84560 KB Output is correct
78 Correct 83 ms 86868 KB Output is correct
79 Correct 74 ms 81744 KB Output is correct
80 Correct 92 ms 84912 KB Output is correct
81 Correct 118 ms 93520 KB Output is correct
82 Correct 132 ms 94288 KB Output is correct
83 Correct 146 ms 101884 KB Output is correct
84 Correct 71 ms 82952 KB Output is correct
85 Correct 121 ms 97408 KB Output is correct
86 Correct 112 ms 68868 KB Output is correct
87 Correct 191 ms 82872 KB Output is correct
88 Correct 191 ms 82876 KB Output is correct
89 Correct 355 ms 85168 KB Output is correct
90 Correct 283 ms 92844 KB Output is correct
91 Correct 352 ms 90616 KB Output is correct
92 Correct 406 ms 89024 KB Output is correct
93 Correct 324 ms 94476 KB Output is correct
94 Correct 458 ms 97872 KB Output is correct
95 Correct 311 ms 102672 KB Output is correct
96 Correct 301 ms 87504 KB Output is correct
97 Correct 391 ms 92212 KB Output is correct