답안 #1070671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1070671 2024-08-22T16:43:53 Z TVSown 자매 도시 (APIO20_swap) C++17
100 / 100
642 ms 136844 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<long long, 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;
long long p[N], b[N], check[N], nxt[25][N], h[N];
vector<long long> adj[N];

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

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

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

void Union(long long u, long long v){
    ++b[u]; ++b[v];
    long long 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(long long u){
//    cout << u << " " << h[u] << " " << nxt[0][u] << "\n";
    for(long long v : adj[u]){
        if(v == nxt[0][u]) continue;
        h[v] = h[u] + 1;
        nxt[0][v] = u;
        dfs(v);
    }
}

long long lca(long long u, long long v){
    if(h[u] < h[v]) swap(u, v);
    REP(k, 20, 0){
        long long 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){
        long long pu = nxt[k][u], pv = nxt[k][v];
        if(pu != pv){
            u = pu; v = pv;
        }
    }
    return nxt[0][u];
}

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

int getMinimumFuelCapacity(int u, int v){
    long long p = lca(u, v);
    long long l = 0, r = h[p];
//    cout << lca << "\n";
    while(l <= r){
        long long 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){
        long long 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){
//        long long 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";
//    }
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 64100 KB Output is correct
2 Correct 10 ms 61924 KB Output is correct
3 Correct 8 ms 64092 KB Output is correct
4 Correct 8 ms 60012 KB Output is correct
5 Correct 10 ms 64204 KB Output is correct
6 Correct 8 ms 60068 KB Output is correct
7 Correct 11 ms 64092 KB Output is correct
8 Correct 8 ms 62040 KB Output is correct
9 Correct 97 ms 88700 KB Output is correct
10 Correct 83 ms 97204 KB Output is correct
11 Correct 114 ms 97300 KB Output is correct
12 Correct 97 ms 103876 KB Output is correct
13 Correct 79 ms 102612 KB Output is correct
14 Correct 103 ms 96380 KB Output is correct
15 Correct 320 ms 101596 KB Output is correct
16 Correct 288 ms 99324 KB Output is correct
17 Correct 314 ms 103352 KB Output is correct
18 Correct 480 ms 109752 KB Output is correct
19 Correct 110 ms 85592 KB Output is correct
20 Correct 308 ms 112076 KB Output is correct
21 Correct 294 ms 111512 KB Output is correct
22 Correct 305 ms 114256 KB Output is correct
23 Correct 470 ms 116336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 64100 KB Output is correct
2 Correct 10 ms 61924 KB Output is correct
3 Correct 498 ms 114676 KB Output is correct
4 Correct 504 ms 117732 KB Output is correct
5 Correct 509 ms 112356 KB Output is correct
6 Correct 489 ms 114356 KB Output is correct
7 Correct 521 ms 112080 KB Output is correct
8 Correct 557 ms 110556 KB Output is correct
9 Correct 544 ms 111824 KB Output is correct
10 Correct 533 ms 112772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 64100 KB Output is correct
2 Correct 10 ms 61924 KB Output is correct
3 Correct 8 ms 64092 KB Output is correct
4 Correct 8 ms 60012 KB Output is correct
5 Correct 10 ms 64204 KB Output is correct
6 Correct 8 ms 60068 KB Output is correct
7 Correct 11 ms 64092 KB Output is correct
8 Correct 8 ms 62040 KB Output is correct
9 Correct 8 ms 72028 KB Output is correct
10 Correct 10 ms 72388 KB Output is correct
11 Correct 10 ms 72284 KB Output is correct
12 Correct 9 ms 72420 KB Output is correct
13 Correct 9 ms 72284 KB Output is correct
14 Correct 9 ms 72404 KB Output is correct
15 Correct 9 ms 72284 KB Output is correct
16 Correct 10 ms 72280 KB Output is correct
17 Correct 9 ms 72444 KB Output is correct
18 Correct 10 ms 72284 KB Output is correct
19 Correct 9 ms 72280 KB Output is correct
20 Correct 9 ms 72408 KB Output is correct
21 Correct 9 ms 72284 KB Output is correct
22 Correct 9 ms 70492 KB Output is correct
23 Correct 8 ms 72144 KB Output is correct
24 Correct 9 ms 70488 KB Output is correct
25 Correct 10 ms 70492 KB Output is correct
26 Correct 9 ms 72540 KB Output is correct
27 Correct 8 ms 72284 KB Output is correct
28 Correct 10 ms 70492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 72028 KB Output is correct
2 Correct 9 ms 64100 KB Output is correct
3 Correct 10 ms 61924 KB Output is correct
4 Correct 8 ms 64092 KB Output is correct
5 Correct 8 ms 60012 KB Output is correct
6 Correct 10 ms 64204 KB Output is correct
7 Correct 8 ms 60068 KB Output is correct
8 Correct 11 ms 64092 KB Output is correct
9 Correct 8 ms 62040 KB Output is correct
10 Correct 97 ms 88700 KB Output is correct
11 Correct 83 ms 97204 KB Output is correct
12 Correct 114 ms 97300 KB Output is correct
13 Correct 97 ms 103876 KB Output is correct
14 Correct 79 ms 102612 KB Output is correct
15 Correct 10 ms 72388 KB Output is correct
16 Correct 10 ms 72284 KB Output is correct
17 Correct 9 ms 72420 KB Output is correct
18 Correct 9 ms 72284 KB Output is correct
19 Correct 9 ms 72404 KB Output is correct
20 Correct 9 ms 72284 KB Output is correct
21 Correct 10 ms 72280 KB Output is correct
22 Correct 9 ms 72444 KB Output is correct
23 Correct 10 ms 72284 KB Output is correct
24 Correct 9 ms 72280 KB Output is correct
25 Correct 9 ms 72408 KB Output is correct
26 Correct 9 ms 72284 KB Output is correct
27 Correct 9 ms 70492 KB Output is correct
28 Correct 8 ms 72144 KB Output is correct
29 Correct 9 ms 70488 KB Output is correct
30 Correct 10 ms 70492 KB Output is correct
31 Correct 9 ms 72540 KB Output is correct
32 Correct 8 ms 72284 KB Output is correct
33 Correct 10 ms 70492 KB Output is correct
34 Correct 28 ms 74488 KB Output is correct
35 Correct 89 ms 103504 KB Output is correct
36 Correct 96 ms 105464 KB Output is correct
37 Correct 92 ms 105460 KB Output is correct
38 Correct 89 ms 103504 KB Output is correct
39 Correct 93 ms 100180 KB Output is correct
40 Correct 88 ms 100176 KB Output is correct
41 Correct 92 ms 103764 KB Output is correct
42 Correct 90 ms 105300 KB Output is correct
43 Correct 90 ms 108116 KB Output is correct
44 Correct 97 ms 107604 KB Output is correct
45 Correct 121 ms 113312 KB Output is correct
46 Correct 94 ms 103504 KB Output is correct
47 Correct 124 ms 105552 KB Output is correct
48 Correct 97 ms 108368 KB Output is correct
49 Correct 93 ms 106580 KB Output is correct
50 Correct 76 ms 96740 KB Output is correct
51 Correct 134 ms 107856 KB Output is correct
52 Correct 130 ms 119892 KB Output is correct
53 Correct 144 ms 124888 KB Output is correct
54 Correct 152 ms 132868 KB Output is correct
55 Correct 78 ms 105964 KB Output is correct
56 Correct 138 ms 125524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 64100 KB Output is correct
2 Correct 10 ms 61924 KB Output is correct
3 Correct 8 ms 64092 KB Output is correct
4 Correct 8 ms 60012 KB Output is correct
5 Correct 10 ms 64204 KB Output is correct
6 Correct 8 ms 60068 KB Output is correct
7 Correct 11 ms 64092 KB Output is correct
8 Correct 8 ms 62040 KB Output is correct
9 Correct 97 ms 88700 KB Output is correct
10 Correct 83 ms 97204 KB Output is correct
11 Correct 114 ms 97300 KB Output is correct
12 Correct 97 ms 103876 KB Output is correct
13 Correct 79 ms 102612 KB Output is correct
14 Correct 103 ms 96380 KB Output is correct
15 Correct 320 ms 101596 KB Output is correct
16 Correct 288 ms 99324 KB Output is correct
17 Correct 314 ms 103352 KB Output is correct
18 Correct 480 ms 109752 KB Output is correct
19 Correct 498 ms 114676 KB Output is correct
20 Correct 504 ms 117732 KB Output is correct
21 Correct 509 ms 112356 KB Output is correct
22 Correct 489 ms 114356 KB Output is correct
23 Correct 521 ms 112080 KB Output is correct
24 Correct 557 ms 110556 KB Output is correct
25 Correct 544 ms 111824 KB Output is correct
26 Correct 533 ms 112772 KB Output is correct
27 Correct 10 ms 72388 KB Output is correct
28 Correct 10 ms 72284 KB Output is correct
29 Correct 9 ms 72420 KB Output is correct
30 Correct 9 ms 72284 KB Output is correct
31 Correct 9 ms 72404 KB Output is correct
32 Correct 9 ms 72284 KB Output is correct
33 Correct 10 ms 72280 KB Output is correct
34 Correct 9 ms 72444 KB Output is correct
35 Correct 10 ms 72284 KB Output is correct
36 Correct 28 ms 74488 KB Output is correct
37 Correct 89 ms 103504 KB Output is correct
38 Correct 96 ms 105464 KB Output is correct
39 Correct 92 ms 105460 KB Output is correct
40 Correct 89 ms 103504 KB Output is correct
41 Correct 93 ms 100180 KB Output is correct
42 Correct 88 ms 100176 KB Output is correct
43 Correct 92 ms 103764 KB Output is correct
44 Correct 90 ms 105300 KB Output is correct
45 Correct 90 ms 108116 KB Output is correct
46 Correct 97 ms 107604 KB Output is correct
47 Correct 28 ms 74796 KB Output is correct
48 Correct 285 ms 108492 KB Output is correct
49 Correct 298 ms 112468 KB Output is correct
50 Correct 279 ms 110776 KB Output is correct
51 Correct 249 ms 112204 KB Output is correct
52 Correct 284 ms 104780 KB Output is correct
53 Correct 237 ms 97092 KB Output is correct
54 Correct 323 ms 109236 KB Output is correct
55 Correct 299 ms 108476 KB Output is correct
56 Correct 468 ms 112456 KB Output is correct
57 Correct 275 ms 106676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 72028 KB Output is correct
2 Correct 9 ms 64100 KB Output is correct
3 Correct 10 ms 61924 KB Output is correct
4 Correct 8 ms 64092 KB Output is correct
5 Correct 8 ms 60012 KB Output is correct
6 Correct 10 ms 64204 KB Output is correct
7 Correct 8 ms 60068 KB Output is correct
8 Correct 11 ms 64092 KB Output is correct
9 Correct 8 ms 62040 KB Output is correct
10 Correct 97 ms 88700 KB Output is correct
11 Correct 83 ms 97204 KB Output is correct
12 Correct 114 ms 97300 KB Output is correct
13 Correct 97 ms 103876 KB Output is correct
14 Correct 79 ms 102612 KB Output is correct
15 Correct 103 ms 96380 KB Output is correct
16 Correct 320 ms 101596 KB Output is correct
17 Correct 288 ms 99324 KB Output is correct
18 Correct 314 ms 103352 KB Output is correct
19 Correct 480 ms 109752 KB Output is correct
20 Correct 498 ms 114676 KB Output is correct
21 Correct 504 ms 117732 KB Output is correct
22 Correct 509 ms 112356 KB Output is correct
23 Correct 489 ms 114356 KB Output is correct
24 Correct 521 ms 112080 KB Output is correct
25 Correct 557 ms 110556 KB Output is correct
26 Correct 544 ms 111824 KB Output is correct
27 Correct 533 ms 112772 KB Output is correct
28 Correct 10 ms 72388 KB Output is correct
29 Correct 10 ms 72284 KB Output is correct
30 Correct 9 ms 72420 KB Output is correct
31 Correct 9 ms 72284 KB Output is correct
32 Correct 9 ms 72404 KB Output is correct
33 Correct 9 ms 72284 KB Output is correct
34 Correct 10 ms 72280 KB Output is correct
35 Correct 9 ms 72444 KB Output is correct
36 Correct 10 ms 72284 KB Output is correct
37 Correct 28 ms 74488 KB Output is correct
38 Correct 89 ms 103504 KB Output is correct
39 Correct 96 ms 105464 KB Output is correct
40 Correct 92 ms 105460 KB Output is correct
41 Correct 89 ms 103504 KB Output is correct
42 Correct 93 ms 100180 KB Output is correct
43 Correct 88 ms 100176 KB Output is correct
44 Correct 92 ms 103764 KB Output is correct
45 Correct 90 ms 105300 KB Output is correct
46 Correct 90 ms 108116 KB Output is correct
47 Correct 97 ms 107604 KB Output is correct
48 Correct 28 ms 74796 KB Output is correct
49 Correct 285 ms 108492 KB Output is correct
50 Correct 298 ms 112468 KB Output is correct
51 Correct 279 ms 110776 KB Output is correct
52 Correct 249 ms 112204 KB Output is correct
53 Correct 284 ms 104780 KB Output is correct
54 Correct 237 ms 97092 KB Output is correct
55 Correct 323 ms 109236 KB Output is correct
56 Correct 299 ms 108476 KB Output is correct
57 Correct 468 ms 112456 KB Output is correct
58 Correct 275 ms 106676 KB Output is correct
59 Correct 110 ms 85592 KB Output is correct
60 Correct 308 ms 112076 KB Output is correct
61 Correct 294 ms 111512 KB Output is correct
62 Correct 305 ms 114256 KB Output is correct
63 Correct 470 ms 116336 KB Output is correct
64 Correct 9 ms 72280 KB Output is correct
65 Correct 9 ms 72408 KB Output is correct
66 Correct 9 ms 72284 KB Output is correct
67 Correct 9 ms 70492 KB Output is correct
68 Correct 8 ms 72144 KB Output is correct
69 Correct 9 ms 70488 KB Output is correct
70 Correct 10 ms 70492 KB Output is correct
71 Correct 9 ms 72540 KB Output is correct
72 Correct 8 ms 72284 KB Output is correct
73 Correct 10 ms 70492 KB Output is correct
74 Correct 121 ms 113312 KB Output is correct
75 Correct 94 ms 103504 KB Output is correct
76 Correct 124 ms 105552 KB Output is correct
77 Correct 97 ms 108368 KB Output is correct
78 Correct 93 ms 106580 KB Output is correct
79 Correct 76 ms 96740 KB Output is correct
80 Correct 134 ms 107856 KB Output is correct
81 Correct 130 ms 119892 KB Output is correct
82 Correct 144 ms 124888 KB Output is correct
83 Correct 152 ms 132868 KB Output is correct
84 Correct 78 ms 105964 KB Output is correct
85 Correct 138 ms 125524 KB Output is correct
86 Correct 120 ms 84228 KB Output is correct
87 Correct 261 ms 108280 KB Output is correct
88 Correct 263 ms 105400 KB Output is correct
89 Correct 472 ms 104448 KB Output is correct
90 Correct 343 ms 106152 KB Output is correct
91 Correct 430 ms 103944 KB Output is correct
92 Correct 569 ms 106264 KB Output is correct
93 Correct 482 ms 121664 KB Output is correct
94 Correct 642 ms 129180 KB Output is correct
95 Correct 469 ms 136844 KB Output is correct
96 Correct 486 ms 113124 KB Output is correct
97 Correct 550 ms 121848 KB Output is correct