답안 #569604

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
569604 2022-05-27T14:37:53 Z tranxuanbach 자매 도시 (APIO20_swap) C++17
100 / 100
378 ms 55880 KB
#ifndef FEXT

#include "swap.h"

#endif

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// #define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())

using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;

const int N = 1e5 + 5, M = 2e5 + 5, K = 3e5 + 5, LK = 19;

struct edge_t{
    int u, v, w;

    edge_t(){

    }

    edge_t(int u, int v, int w): u(u), v(v), w(w){

    }

    friend bool operator< (const edge_t &lhs, const edge_t &rhs){
        return lhs.w < rhs.w;
    }
};

struct disjoint_set_union{
    int par[N], node[N];
    vi path[N];

    void reset(){
        For(i, 0, N){
            par[i] = -1;
            node[i] = i;
            path[i].emplace_back(i);
        }
    }

    disjoint_set_union(){
        reset();
    }

    int root(int x){
        return par[x] < 0 ? x : (par[x] = root(par[x]));
    }

    void merge(int x, int y){
        if ((x = root(x)) == (y = root(y))){
            return;
        }
        if (par[x] > par[y]){
            swap(x, y);
        }
        par[x] += par[y];
        par[y] = x;
        while (!path[y].empty()){
            path[x].emplace_back(path[y].back());
            path[y].pop_back();
        }
    }
} dsu;

int n, m;
edge_t a[M];

bool b[N];
int deg[N];

int k;
int c[K];
vi adj[K];

int par[K][LK];
int h[K];

void dfs(int u){
    For(j, 1, LK){
        par[u][j] = par[par[u][j - 1]][j - 1];
    }
    Fora(v, adj[u]){
        par[v][0] = u;
        h[v] = h[u] + 1;
        dfs(v);
    }
}

void init(int _n, int _m, vi _u, vi _v, vi _w){
    n = _n; m = _m;
    ForE(i, 1, m){
        a[i] = edge_t(_u[i - 1] + 1, _v[i - 1] + 1, _w[i - 1]);
    }

    k = n;
    memset(c, -1, sizeof(c));
    dsu.reset();

    sort(a + 1, a + m + 1);
    ForE(i, 1, m){
        int u = a[i].u, v = a[i].v, w = a[i].w;
        if (dsu.root(u) == dsu.root(v)){
            if (!b[u]){
                int tu = dsu.node[dsu.root(u)];
                c[tu] = w;
                Fora(t, dsu.path[dsu.root(u)]){
                    b[t] = 1;
                }
            }
        }
        else{
            if (deg[u] < deg[v]){
                swap(u, v);
            }

            int tu = dsu.node[dsu.root(u)], tv = dsu.node[dsu.root(v)];
            k++;
            adj[k].emplace_back(tu);
            adj[k].emplace_back(tv);

            if (b[u] and b[v]){
                c[k] = w;
            }
            else if (b[u]){
                c[k] = w;
                Fora(t, dsu.path[dsu.root(v)]){
                    b[t] = 1;
                }
            }
            else if (b[v]){
                c[k] = w;
                Fora(t, dsu.path[dsu.root(u)]){
                    b[t] = 1;
                }
            }
            else{
                if (deg[u] >= 2){
                    c[k] = w;
                    Fora(t, dsu.path[dsu.root(u)]){
                        b[t] = 1;
                    }
                    Fora(t, dsu.path[dsu.root(v)]){
                        b[t] = 1;
                    }
                }
            }
            dsu.merge(u, v);
            dsu.node[dsu.root(u)] = k;
        }
        deg[u]++; deg[v]++;
    }
    dfs(k);
}

int lca(int u, int v){
    if (h[u] < h[v]){
        swap(u, v);
    }
    FordE(i, LK - 1, 0){
        if (h[u] - (1 << i) >= h[v]){
            u = par[u][i];
        }
    }
    if (u == v){
        return u;
    }
    FordE(i, LK - 1, 0){
        if (par[u][i] != par[v][i]){
            u = par[u][i]; v = par[v][i];
        }
    }
    return par[u][0];
}

int getMinimumFuelCapacity(int u, int v){
    u++; v++;

    int t = lca(u, v);
    if (c[t] != -1){
        return c[t];
    }
    FordE(i, LK - 1, 0){
        if (par[t][i] != 0 and c[par[t][i]] == -1){
            t = par[t][i];
        }
    }
    t = par[t][0];
    if (t != 0){
        return c[t];
    }
    return -1;
}

#ifdef FEXT

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    freopen("KEK.inp", "r", stdin);
    freopen("KEK.out", "w", stdout);
    int N, M;
    assert(2 == scanf("%d %d", &N, &M));

    std::vector<int> U(M), V(M), W(M);
    for (int i = 0; i < M; ++i) {
        assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
    }

    int Q;
    assert(1 == scanf("%d", &Q));

    std::vector<int> X(Q), Y(Q);
    for (int i = 0; i < Q; ++i) {
        assert(2 == scanf("%d %d", &X[i], &Y[i]));
    }

    init(N, M, U, V, W);

    std::vector<int> minimum_fuel_capacities(Q);
    for (int i = 0; i < Q; ++i) {
        minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
    }

    for (int i = 0; i < Q; ++i) {
        printf("%d\n", minimum_fuel_capacities[i]);
    }

    return 0;
}

#endif

/*
==================================================+
INPUT:                                            |
--------------------------------------------------|
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1
--------------------------------------------------|
3 2
0 1 5
0 2 5
1
1 2
--------------------------------------------------|
==================================================+
OUTPUT:                                           |
--------------------------------------------------|
3
10
4
--------------------------------------------------|
-1
--------------------------------------------------|
==================================================+
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14804 KB Output is correct
2 Correct 14 ms 14824 KB Output is correct
3 Correct 23 ms 14784 KB Output is correct
4 Correct 15 ms 14916 KB Output is correct
5 Correct 16 ms 15060 KB Output is correct
6 Correct 16 ms 15008 KB Output is correct
7 Correct 15 ms 14996 KB Output is correct
8 Correct 15 ms 15060 KB Output is correct
9 Correct 133 ms 39032 KB Output is correct
10 Correct 166 ms 43940 KB Output is correct
11 Correct 160 ms 43188 KB Output is correct
12 Correct 175 ms 44856 KB Output is correct
13 Correct 124 ms 43004 KB Output is correct
14 Correct 167 ms 39032 KB Output is correct
15 Correct 289 ms 45976 KB Output is correct
16 Correct 305 ms 45292 KB Output is correct
17 Correct 313 ms 47400 KB Output is correct
18 Correct 355 ms 44960 KB Output is correct
19 Correct 93 ms 24016 KB Output is correct
20 Correct 291 ms 49592 KB Output is correct
21 Correct 254 ms 49052 KB Output is correct
22 Correct 300 ms 50724 KB Output is correct
23 Correct 378 ms 48600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14804 KB Output is correct
2 Correct 14 ms 14824 KB Output is correct
3 Correct 285 ms 47300 KB Output is correct
4 Correct 303 ms 51368 KB Output is correct
5 Correct 285 ms 50588 KB Output is correct
6 Correct 281 ms 50984 KB Output is correct
7 Correct 318 ms 51128 KB Output is correct
8 Correct 288 ms 49708 KB Output is correct
9 Correct 340 ms 50604 KB Output is correct
10 Correct 268 ms 49424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14804 KB Output is correct
2 Correct 14 ms 14824 KB Output is correct
3 Correct 23 ms 14784 KB Output is correct
4 Correct 15 ms 14916 KB Output is correct
5 Correct 16 ms 15060 KB Output is correct
6 Correct 16 ms 15008 KB Output is correct
7 Correct 15 ms 14996 KB Output is correct
8 Correct 15 ms 15060 KB Output is correct
9 Correct 14 ms 14768 KB Output is correct
10 Correct 17 ms 15128 KB Output is correct
11 Correct 15 ms 15060 KB Output is correct
12 Correct 14 ms 15028 KB Output is correct
13 Correct 14 ms 15044 KB Output is correct
14 Correct 14 ms 15060 KB Output is correct
15 Correct 15 ms 15048 KB Output is correct
16 Correct 14 ms 15060 KB Output is correct
17 Correct 14 ms 15060 KB Output is correct
18 Correct 14 ms 15080 KB Output is correct
19 Correct 15 ms 14932 KB Output is correct
20 Correct 14 ms 15020 KB Output is correct
21 Correct 13 ms 15060 KB Output is correct
22 Correct 14 ms 14932 KB Output is correct
23 Correct 15 ms 15040 KB Output is correct
24 Correct 14 ms 15060 KB Output is correct
25 Correct 15 ms 15060 KB Output is correct
26 Correct 17 ms 15048 KB Output is correct
27 Correct 14 ms 15052 KB Output is correct
28 Correct 15 ms 15048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14768 KB Output is correct
2 Correct 14 ms 14804 KB Output is correct
3 Correct 14 ms 14824 KB Output is correct
4 Correct 23 ms 14784 KB Output is correct
5 Correct 15 ms 14916 KB Output is correct
6 Correct 16 ms 15060 KB Output is correct
7 Correct 16 ms 15008 KB Output is correct
8 Correct 15 ms 14996 KB Output is correct
9 Correct 15 ms 15060 KB Output is correct
10 Correct 133 ms 39032 KB Output is correct
11 Correct 166 ms 43940 KB Output is correct
12 Correct 160 ms 43188 KB Output is correct
13 Correct 175 ms 44856 KB Output is correct
14 Correct 124 ms 43004 KB Output is correct
15 Correct 17 ms 15128 KB Output is correct
16 Correct 15 ms 15060 KB Output is correct
17 Correct 14 ms 15028 KB Output is correct
18 Correct 14 ms 15044 KB Output is correct
19 Correct 14 ms 15060 KB Output is correct
20 Correct 15 ms 15048 KB Output is correct
21 Correct 14 ms 15060 KB Output is correct
22 Correct 14 ms 15060 KB Output is correct
23 Correct 14 ms 15080 KB Output is correct
24 Correct 15 ms 14932 KB Output is correct
25 Correct 14 ms 15020 KB Output is correct
26 Correct 13 ms 15060 KB Output is correct
27 Correct 14 ms 14932 KB Output is correct
28 Correct 15 ms 15040 KB Output is correct
29 Correct 14 ms 15060 KB Output is correct
30 Correct 15 ms 15060 KB Output is correct
31 Correct 17 ms 15048 KB Output is correct
32 Correct 14 ms 15052 KB Output is correct
33 Correct 15 ms 15048 KB Output is correct
34 Correct 26 ms 18764 KB Output is correct
35 Correct 152 ms 45608 KB Output is correct
36 Correct 179 ms 45668 KB Output is correct
37 Correct 184 ms 44992 KB Output is correct
38 Correct 154 ms 43824 KB Output is correct
39 Correct 154 ms 43516 KB Output is correct
40 Correct 144 ms 40724 KB Output is correct
41 Correct 167 ms 46132 KB Output is correct
42 Correct 162 ms 45992 KB Output is correct
43 Correct 144 ms 44868 KB Output is correct
44 Correct 162 ms 44008 KB Output is correct
45 Correct 145 ms 41048 KB Output is correct
46 Correct 164 ms 45376 KB Output is correct
47 Correct 162 ms 42680 KB Output is correct
48 Correct 151 ms 42756 KB Output is correct
49 Correct 70 ms 23280 KB Output is correct
50 Correct 69 ms 23084 KB Output is correct
51 Correct 123 ms 36624 KB Output is correct
52 Correct 185 ms 48256 KB Output is correct
53 Correct 193 ms 47480 KB Output is correct
54 Correct 204 ms 51572 KB Output is correct
55 Correct 115 ms 44192 KB Output is correct
56 Correct 167 ms 47464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14804 KB Output is correct
2 Correct 14 ms 14824 KB Output is correct
3 Correct 23 ms 14784 KB Output is correct
4 Correct 15 ms 14916 KB Output is correct
5 Correct 16 ms 15060 KB Output is correct
6 Correct 16 ms 15008 KB Output is correct
7 Correct 15 ms 14996 KB Output is correct
8 Correct 15 ms 15060 KB Output is correct
9 Correct 133 ms 39032 KB Output is correct
10 Correct 166 ms 43940 KB Output is correct
11 Correct 160 ms 43188 KB Output is correct
12 Correct 175 ms 44856 KB Output is correct
13 Correct 124 ms 43004 KB Output is correct
14 Correct 167 ms 39032 KB Output is correct
15 Correct 289 ms 45976 KB Output is correct
16 Correct 305 ms 45292 KB Output is correct
17 Correct 313 ms 47400 KB Output is correct
18 Correct 355 ms 44960 KB Output is correct
19 Correct 285 ms 47300 KB Output is correct
20 Correct 303 ms 51368 KB Output is correct
21 Correct 285 ms 50588 KB Output is correct
22 Correct 281 ms 50984 KB Output is correct
23 Correct 318 ms 51128 KB Output is correct
24 Correct 288 ms 49708 KB Output is correct
25 Correct 340 ms 50604 KB Output is correct
26 Correct 268 ms 49424 KB Output is correct
27 Correct 17 ms 15128 KB Output is correct
28 Correct 15 ms 15060 KB Output is correct
29 Correct 14 ms 15028 KB Output is correct
30 Correct 14 ms 15044 KB Output is correct
31 Correct 14 ms 15060 KB Output is correct
32 Correct 15 ms 15048 KB Output is correct
33 Correct 14 ms 15060 KB Output is correct
34 Correct 14 ms 15060 KB Output is correct
35 Correct 14 ms 15080 KB Output is correct
36 Correct 26 ms 18764 KB Output is correct
37 Correct 152 ms 45608 KB Output is correct
38 Correct 179 ms 45668 KB Output is correct
39 Correct 184 ms 44992 KB Output is correct
40 Correct 154 ms 43824 KB Output is correct
41 Correct 154 ms 43516 KB Output is correct
42 Correct 144 ms 40724 KB Output is correct
43 Correct 167 ms 46132 KB Output is correct
44 Correct 162 ms 45992 KB Output is correct
45 Correct 144 ms 44868 KB Output is correct
46 Correct 162 ms 44008 KB Output is correct
47 Correct 34 ms 18940 KB Output is correct
48 Correct 300 ms 50604 KB Output is correct
49 Correct 282 ms 49828 KB Output is correct
50 Correct 273 ms 50004 KB Output is correct
51 Correct 274 ms 49184 KB Output is correct
52 Correct 276 ms 46904 KB Output is correct
53 Correct 219 ms 39736 KB Output is correct
54 Correct 287 ms 51492 KB Output is correct
55 Correct 280 ms 50696 KB Output is correct
56 Correct 342 ms 48612 KB Output is correct
57 Correct 269 ms 49212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 14768 KB Output is correct
2 Correct 14 ms 14804 KB Output is correct
3 Correct 14 ms 14824 KB Output is correct
4 Correct 23 ms 14784 KB Output is correct
5 Correct 15 ms 14916 KB Output is correct
6 Correct 16 ms 15060 KB Output is correct
7 Correct 16 ms 15008 KB Output is correct
8 Correct 15 ms 14996 KB Output is correct
9 Correct 15 ms 15060 KB Output is correct
10 Correct 133 ms 39032 KB Output is correct
11 Correct 166 ms 43940 KB Output is correct
12 Correct 160 ms 43188 KB Output is correct
13 Correct 175 ms 44856 KB Output is correct
14 Correct 124 ms 43004 KB Output is correct
15 Correct 167 ms 39032 KB Output is correct
16 Correct 289 ms 45976 KB Output is correct
17 Correct 305 ms 45292 KB Output is correct
18 Correct 313 ms 47400 KB Output is correct
19 Correct 355 ms 44960 KB Output is correct
20 Correct 285 ms 47300 KB Output is correct
21 Correct 303 ms 51368 KB Output is correct
22 Correct 285 ms 50588 KB Output is correct
23 Correct 281 ms 50984 KB Output is correct
24 Correct 318 ms 51128 KB Output is correct
25 Correct 288 ms 49708 KB Output is correct
26 Correct 340 ms 50604 KB Output is correct
27 Correct 268 ms 49424 KB Output is correct
28 Correct 17 ms 15128 KB Output is correct
29 Correct 15 ms 15060 KB Output is correct
30 Correct 14 ms 15028 KB Output is correct
31 Correct 14 ms 15044 KB Output is correct
32 Correct 14 ms 15060 KB Output is correct
33 Correct 15 ms 15048 KB Output is correct
34 Correct 14 ms 15060 KB Output is correct
35 Correct 14 ms 15060 KB Output is correct
36 Correct 14 ms 15080 KB Output is correct
37 Correct 26 ms 18764 KB Output is correct
38 Correct 152 ms 45608 KB Output is correct
39 Correct 179 ms 45668 KB Output is correct
40 Correct 184 ms 44992 KB Output is correct
41 Correct 154 ms 43824 KB Output is correct
42 Correct 154 ms 43516 KB Output is correct
43 Correct 144 ms 40724 KB Output is correct
44 Correct 167 ms 46132 KB Output is correct
45 Correct 162 ms 45992 KB Output is correct
46 Correct 144 ms 44868 KB Output is correct
47 Correct 162 ms 44008 KB Output is correct
48 Correct 34 ms 18940 KB Output is correct
49 Correct 300 ms 50604 KB Output is correct
50 Correct 282 ms 49828 KB Output is correct
51 Correct 273 ms 50004 KB Output is correct
52 Correct 274 ms 49184 KB Output is correct
53 Correct 276 ms 46904 KB Output is correct
54 Correct 219 ms 39736 KB Output is correct
55 Correct 287 ms 51492 KB Output is correct
56 Correct 280 ms 50696 KB Output is correct
57 Correct 342 ms 48612 KB Output is correct
58 Correct 269 ms 49212 KB Output is correct
59 Correct 93 ms 24016 KB Output is correct
60 Correct 291 ms 49592 KB Output is correct
61 Correct 254 ms 49052 KB Output is correct
62 Correct 300 ms 50724 KB Output is correct
63 Correct 378 ms 48600 KB Output is correct
64 Correct 15 ms 14932 KB Output is correct
65 Correct 14 ms 15020 KB Output is correct
66 Correct 13 ms 15060 KB Output is correct
67 Correct 14 ms 14932 KB Output is correct
68 Correct 15 ms 15040 KB Output is correct
69 Correct 14 ms 15060 KB Output is correct
70 Correct 15 ms 15060 KB Output is correct
71 Correct 17 ms 15048 KB Output is correct
72 Correct 14 ms 15052 KB Output is correct
73 Correct 15 ms 15048 KB Output is correct
74 Correct 145 ms 41048 KB Output is correct
75 Correct 164 ms 45376 KB Output is correct
76 Correct 162 ms 42680 KB Output is correct
77 Correct 151 ms 42756 KB Output is correct
78 Correct 70 ms 23280 KB Output is correct
79 Correct 69 ms 23084 KB Output is correct
80 Correct 123 ms 36624 KB Output is correct
81 Correct 185 ms 48256 KB Output is correct
82 Correct 193 ms 47480 KB Output is correct
83 Correct 204 ms 51572 KB Output is correct
84 Correct 115 ms 44192 KB Output is correct
85 Correct 167 ms 47464 KB Output is correct
86 Correct 81 ms 25756 KB Output is correct
87 Correct 267 ms 49444 KB Output is correct
88 Correct 270 ms 49688 KB Output is correct
89 Correct 283 ms 46056 KB Output is correct
90 Correct 148 ms 27828 KB Output is correct
91 Correct 180 ms 29924 KB Output is correct
92 Correct 260 ms 41628 KB Output is correct
93 Correct 293 ms 52356 KB Output is correct
94 Correct 325 ms 51708 KB Output is correct
95 Correct 351 ms 55880 KB Output is correct
96 Correct 313 ms 48900 KB Output is correct
97 Correct 312 ms 50456 KB Output is correct