답안 #392512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
392512 2021-04-21T09:30:55 Z arujbansal 자매 도시 (APIO20_swap) C++17
100 / 100
382 ms 96528 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>
#include "swap.h"

using namespace std;

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long

struct LCA {
    int n, k;
    vector<vector<int>> adj, sparse_table;
    vector<int> euler, first_occurrence;
    int timer;
    bool lca_built;

    LCA(int _n = 0) { 
        lca_built = false;

        if (_n > 0)
            init(_n);
    }

    void init(int _n) {
        lca_built = false;

        n = _n;

        adj.resize(n);
        for (int i = 0; i < n; i++)
            adj[i].clear();

        euler.clear();
        first_occurrence.resize(n);
        timer = 0;
    }

    void add_edge(int u, int v) {
        assert(n > 0);

        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    void run(int root = 0) {
        assert(n > 0);

        dfs(root, -1);
        build();

        lca_built = true;
    }

    void dfs(int u = 0, int p = -1) {
        first_occurrence[u] = timer++;
        euler.push_back(u);

        for (const auto &v : adj[u]) {
            if (v == p) continue;
            
            dfs(v, u);
            euler.push_back(u);
            timer++;
        }
    }

    void build() {
        k = 31 - __builtin_clz(timer) + 1;

        sparse_table.resize(k);

        for (int i = 0; i < k; i++)
            sparse_table[i].resize(timer - (1 << i) + 1);

        for (int j = 0; j < timer; j++)
            sparse_table[0][j] = euler[j];

        for (int i = 1; i < k; i++) {
            for (int j = 0; j + (1 << i) - 1 < timer; j++) {
                int x = sparse_table[i - 1][j];
                int y = sparse_table[i - 1][j + (1 << (i - 1))];

                sparse_table[i][j] = (first_occurrence[x] < first_occurrence[y] ? x : y);
            }
        }
    }

    int query(int u, int v) {
        assert(lca_built == true);

        int l = first_occurrence[u], r = first_occurrence[v];
        if (l > r)
            swap(l, r);

        int i = 31 - __builtin_clz(r - l + 1);

        int x = sparse_table[i][l], y = sparse_table[i][r - (1 << i) + 1];
        return first_occurrence[x] < first_occurrence[y] ? x : y;
    }
};

const int MXN = 5e5 + 5, INF = 1e9 + 5;
int par[MXN], degree[MXN], valid[MXN], cost[MXN];
int fake_node;
vector<array<int, 3>> edges;
LCA reach_lca;

int find_par(int x) {
    if (par[x] == x) return x;
    return par[x] = find_par(par[x]);
}

void unite(int u, int v, int w) {
    degree[u]++;
    degree[v]++;

    valid[fake_node] |= (degree[u] >= 3) | (degree[v] >= 3);

    u = find_par(u), v = find_par(v);

    par[u] = fake_node;
    par[v] = fake_node;

    valid[fake_node] |= valid[u] | valid[v];
    if (valid[fake_node])
        cost[fake_node] = w;
    
    if (u == v) {
        valid[fake_node] = 1;
        cost[fake_node] = w;
        reach_lca.add_edge(fake_node++, u);

        return;
    }

    reach_lca.add_edge(fake_node, u);
    reach_lca.add_edge(fake_node, v);

    fake_node++;
}

void dfs(int u, int p) {
    for (const auto &v : reach_lca.adj[u]) {
        if (v == p) continue;
        cost[v] = min(cost[v], cost[u]);

        dfs(v, u);
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    for (int i = 0; i < M; i++)
        edges.emplace_back(array<int, 3>{U[i], V[i], W[i]});

    sort(all(edges), [&](const array<int, 3> &x, const array<int, 3> &y) { return x[2] < y[2]; });

    fake_node = N;

    for (int i = 0; i < MXN; i++) {
        cost[i] = INF;
        par[i] = i;
    }

    reach_lca.init(MXN);

    for (const auto &[u, v, w] : edges)
        unite(u, v, w);

    for (int i = 0; i < fake_node; i++) {
        if (par[i] == i) {
            reach_lca.add_edge(i, fake_node + 1);
        }
    }

    fake_node++;
    reach_lca.run(fake_node);
    dfs(fake_node, -1);
}

int getMinimumFuelCapacity(int X, int Y) {
    int ans = cost[reach_lca.query(X, Y)];
    return (ans < INF ? ans : -1);
}

#ifdef local
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;

    vector<int> U(M), V(M), W(M);

    for (int i = 0; i < M; i++)
        cin >> U[i] >> V[i] >> W[i];

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

    int Q;
    cin >> Q;

    while (Q--) {
        int X, Y;
        cin >> X >> Y;

        cout << getMinimumFuelCapacity(X, Y) << "\n";
    }
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 17868 KB Output is correct
2 Correct 14 ms 17868 KB Output is correct
3 Correct 13 ms 17840 KB Output is correct
4 Correct 14 ms 17956 KB Output is correct
5 Correct 14 ms 18124 KB Output is correct
6 Correct 14 ms 18124 KB Output is correct
7 Correct 14 ms 18204 KB Output is correct
8 Correct 14 ms 18252 KB Output is correct
9 Correct 150 ms 50324 KB Output is correct
10 Correct 178 ms 58096 KB Output is correct
11 Correct 201 ms 57500 KB Output is correct
12 Correct 187 ms 59952 KB Output is correct
13 Correct 138 ms 63924 KB Output is correct
14 Correct 157 ms 50608 KB Output is correct
15 Correct 262 ms 62488 KB Output is correct
16 Correct 248 ms 61328 KB Output is correct
17 Correct 266 ms 63876 KB Output is correct
18 Correct 221 ms 67868 KB Output is correct
19 Correct 94 ms 29064 KB Output is correct
20 Correct 257 ms 63744 KB Output is correct
21 Correct 255 ms 62320 KB Output is correct
22 Correct 277 ms 65288 KB Output is correct
23 Correct 224 ms 69404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 17868 KB Output is correct
2 Correct 14 ms 17868 KB Output is correct
3 Correct 194 ms 69776 KB Output is correct
4 Correct 199 ms 72200 KB Output is correct
5 Correct 202 ms 70860 KB Output is correct
6 Correct 202 ms 71944 KB Output is correct
7 Correct 212 ms 71604 KB Output is correct
8 Correct 194 ms 69364 KB Output is correct
9 Correct 207 ms 71056 KB Output is correct
10 Correct 195 ms 69008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 17868 KB Output is correct
2 Correct 14 ms 17868 KB Output is correct
3 Correct 13 ms 17840 KB Output is correct
4 Correct 14 ms 17956 KB Output is correct
5 Correct 14 ms 18124 KB Output is correct
6 Correct 14 ms 18124 KB Output is correct
7 Correct 14 ms 18204 KB Output is correct
8 Correct 14 ms 18252 KB Output is correct
9 Correct 12 ms 17908 KB Output is correct
10 Correct 12 ms 18152 KB Output is correct
11 Correct 13 ms 18124 KB Output is correct
12 Correct 13 ms 18232 KB Output is correct
13 Correct 13 ms 18152 KB Output is correct
14 Correct 14 ms 18160 KB Output is correct
15 Correct 13 ms 18124 KB Output is correct
16 Correct 13 ms 18224 KB Output is correct
17 Correct 13 ms 18252 KB Output is correct
18 Correct 13 ms 18124 KB Output is correct
19 Correct 13 ms 18168 KB Output is correct
20 Correct 13 ms 18124 KB Output is correct
21 Correct 13 ms 18252 KB Output is correct
22 Correct 13 ms 18380 KB Output is correct
23 Correct 13 ms 18124 KB Output is correct
24 Correct 13 ms 18380 KB Output is correct
25 Correct 13 ms 18380 KB Output is correct
26 Correct 14 ms 18508 KB Output is correct
27 Correct 13 ms 18252 KB Output is correct
28 Correct 15 ms 18484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 17908 KB Output is correct
2 Correct 13 ms 17868 KB Output is correct
3 Correct 14 ms 17868 KB Output is correct
4 Correct 13 ms 17840 KB Output is correct
5 Correct 14 ms 17956 KB Output is correct
6 Correct 14 ms 18124 KB Output is correct
7 Correct 14 ms 18124 KB Output is correct
8 Correct 14 ms 18204 KB Output is correct
9 Correct 14 ms 18252 KB Output is correct
10 Correct 150 ms 50324 KB Output is correct
11 Correct 178 ms 58096 KB Output is correct
12 Correct 201 ms 57500 KB Output is correct
13 Correct 187 ms 59952 KB Output is correct
14 Correct 138 ms 63924 KB Output is correct
15 Correct 12 ms 18152 KB Output is correct
16 Correct 13 ms 18124 KB Output is correct
17 Correct 13 ms 18232 KB Output is correct
18 Correct 13 ms 18152 KB Output is correct
19 Correct 14 ms 18160 KB Output is correct
20 Correct 13 ms 18124 KB Output is correct
21 Correct 13 ms 18224 KB Output is correct
22 Correct 13 ms 18252 KB Output is correct
23 Correct 13 ms 18124 KB Output is correct
24 Correct 13 ms 18168 KB Output is correct
25 Correct 13 ms 18124 KB Output is correct
26 Correct 13 ms 18252 KB Output is correct
27 Correct 13 ms 18380 KB Output is correct
28 Correct 13 ms 18124 KB Output is correct
29 Correct 13 ms 18380 KB Output is correct
30 Correct 13 ms 18380 KB Output is correct
31 Correct 14 ms 18508 KB Output is correct
32 Correct 13 ms 18252 KB Output is correct
33 Correct 15 ms 18484 KB Output is correct
34 Correct 28 ms 23052 KB Output is correct
35 Correct 189 ms 59608 KB Output is correct
36 Correct 184 ms 59572 KB Output is correct
37 Correct 182 ms 59544 KB Output is correct
38 Correct 181 ms 58932 KB Output is correct
39 Correct 178 ms 58632 KB Output is correct
40 Correct 169 ms 55220 KB Output is correct
41 Correct 186 ms 59820 KB Output is correct
42 Correct 192 ms 59516 KB Output is correct
43 Correct 134 ms 64668 KB Output is correct
44 Correct 187 ms 59912 KB Output is correct
45 Correct 213 ms 74580 KB Output is correct
46 Correct 185 ms 59572 KB Output is correct
47 Correct 181 ms 59624 KB Output is correct
48 Correct 192 ms 64704 KB Output is correct
49 Correct 157 ms 66888 KB Output is correct
50 Correct 126 ms 55992 KB Output is correct
51 Correct 182 ms 66692 KB Output is correct
52 Correct 263 ms 79920 KB Output is correct
53 Correct 289 ms 83992 KB Output is correct
54 Correct 299 ms 92540 KB Output is correct
55 Correct 140 ms 64288 KB Output is correct
56 Correct 258 ms 85744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 17868 KB Output is correct
2 Correct 14 ms 17868 KB Output is correct
3 Correct 13 ms 17840 KB Output is correct
4 Correct 14 ms 17956 KB Output is correct
5 Correct 14 ms 18124 KB Output is correct
6 Correct 14 ms 18124 KB Output is correct
7 Correct 14 ms 18204 KB Output is correct
8 Correct 14 ms 18252 KB Output is correct
9 Correct 150 ms 50324 KB Output is correct
10 Correct 178 ms 58096 KB Output is correct
11 Correct 201 ms 57500 KB Output is correct
12 Correct 187 ms 59952 KB Output is correct
13 Correct 138 ms 63924 KB Output is correct
14 Correct 157 ms 50608 KB Output is correct
15 Correct 262 ms 62488 KB Output is correct
16 Correct 248 ms 61328 KB Output is correct
17 Correct 266 ms 63876 KB Output is correct
18 Correct 221 ms 67868 KB Output is correct
19 Correct 194 ms 69776 KB Output is correct
20 Correct 199 ms 72200 KB Output is correct
21 Correct 202 ms 70860 KB Output is correct
22 Correct 202 ms 71944 KB Output is correct
23 Correct 212 ms 71604 KB Output is correct
24 Correct 194 ms 69364 KB Output is correct
25 Correct 207 ms 71056 KB Output is correct
26 Correct 195 ms 69008 KB Output is correct
27 Correct 12 ms 18152 KB Output is correct
28 Correct 13 ms 18124 KB Output is correct
29 Correct 13 ms 18232 KB Output is correct
30 Correct 13 ms 18152 KB Output is correct
31 Correct 14 ms 18160 KB Output is correct
32 Correct 13 ms 18124 KB Output is correct
33 Correct 13 ms 18224 KB Output is correct
34 Correct 13 ms 18252 KB Output is correct
35 Correct 13 ms 18124 KB Output is correct
36 Correct 28 ms 23052 KB Output is correct
37 Correct 189 ms 59608 KB Output is correct
38 Correct 184 ms 59572 KB Output is correct
39 Correct 182 ms 59544 KB Output is correct
40 Correct 181 ms 58932 KB Output is correct
41 Correct 178 ms 58632 KB Output is correct
42 Correct 169 ms 55220 KB Output is correct
43 Correct 186 ms 59820 KB Output is correct
44 Correct 192 ms 59516 KB Output is correct
45 Correct 134 ms 64668 KB Output is correct
46 Correct 187 ms 59912 KB Output is correct
47 Correct 34 ms 23076 KB Output is correct
48 Correct 266 ms 64404 KB Output is correct
49 Correct 270 ms 64476 KB Output is correct
50 Correct 269 ms 64384 KB Output is correct
51 Correct 268 ms 64168 KB Output is correct
52 Correct 255 ms 61584 KB Output is correct
53 Correct 192 ms 50856 KB Output is correct
54 Correct 270 ms 65356 KB Output is correct
55 Correct 270 ms 64476 KB Output is correct
56 Correct 222 ms 68492 KB Output is correct
57 Correct 282 ms 65448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 17908 KB Output is correct
2 Correct 13 ms 17868 KB Output is correct
3 Correct 14 ms 17868 KB Output is correct
4 Correct 13 ms 17840 KB Output is correct
5 Correct 14 ms 17956 KB Output is correct
6 Correct 14 ms 18124 KB Output is correct
7 Correct 14 ms 18124 KB Output is correct
8 Correct 14 ms 18204 KB Output is correct
9 Correct 14 ms 18252 KB Output is correct
10 Correct 150 ms 50324 KB Output is correct
11 Correct 178 ms 58096 KB Output is correct
12 Correct 201 ms 57500 KB Output is correct
13 Correct 187 ms 59952 KB Output is correct
14 Correct 138 ms 63924 KB Output is correct
15 Correct 157 ms 50608 KB Output is correct
16 Correct 262 ms 62488 KB Output is correct
17 Correct 248 ms 61328 KB Output is correct
18 Correct 266 ms 63876 KB Output is correct
19 Correct 221 ms 67868 KB Output is correct
20 Correct 194 ms 69776 KB Output is correct
21 Correct 199 ms 72200 KB Output is correct
22 Correct 202 ms 70860 KB Output is correct
23 Correct 202 ms 71944 KB Output is correct
24 Correct 212 ms 71604 KB Output is correct
25 Correct 194 ms 69364 KB Output is correct
26 Correct 207 ms 71056 KB Output is correct
27 Correct 195 ms 69008 KB Output is correct
28 Correct 12 ms 18152 KB Output is correct
29 Correct 13 ms 18124 KB Output is correct
30 Correct 13 ms 18232 KB Output is correct
31 Correct 13 ms 18152 KB Output is correct
32 Correct 14 ms 18160 KB Output is correct
33 Correct 13 ms 18124 KB Output is correct
34 Correct 13 ms 18224 KB Output is correct
35 Correct 13 ms 18252 KB Output is correct
36 Correct 13 ms 18124 KB Output is correct
37 Correct 28 ms 23052 KB Output is correct
38 Correct 189 ms 59608 KB Output is correct
39 Correct 184 ms 59572 KB Output is correct
40 Correct 182 ms 59544 KB Output is correct
41 Correct 181 ms 58932 KB Output is correct
42 Correct 178 ms 58632 KB Output is correct
43 Correct 169 ms 55220 KB Output is correct
44 Correct 186 ms 59820 KB Output is correct
45 Correct 192 ms 59516 KB Output is correct
46 Correct 134 ms 64668 KB Output is correct
47 Correct 187 ms 59912 KB Output is correct
48 Correct 34 ms 23076 KB Output is correct
49 Correct 266 ms 64404 KB Output is correct
50 Correct 270 ms 64476 KB Output is correct
51 Correct 269 ms 64384 KB Output is correct
52 Correct 268 ms 64168 KB Output is correct
53 Correct 255 ms 61584 KB Output is correct
54 Correct 192 ms 50856 KB Output is correct
55 Correct 270 ms 65356 KB Output is correct
56 Correct 270 ms 64476 KB Output is correct
57 Correct 222 ms 68492 KB Output is correct
58 Correct 282 ms 65448 KB Output is correct
59 Correct 94 ms 29064 KB Output is correct
60 Correct 257 ms 63744 KB Output is correct
61 Correct 255 ms 62320 KB Output is correct
62 Correct 277 ms 65288 KB Output is correct
63 Correct 224 ms 69404 KB Output is correct
64 Correct 13 ms 18168 KB Output is correct
65 Correct 13 ms 18124 KB Output is correct
66 Correct 13 ms 18252 KB Output is correct
67 Correct 13 ms 18380 KB Output is correct
68 Correct 13 ms 18124 KB Output is correct
69 Correct 13 ms 18380 KB Output is correct
70 Correct 13 ms 18380 KB Output is correct
71 Correct 14 ms 18508 KB Output is correct
72 Correct 13 ms 18252 KB Output is correct
73 Correct 15 ms 18484 KB Output is correct
74 Correct 213 ms 74580 KB Output is correct
75 Correct 185 ms 59572 KB Output is correct
76 Correct 181 ms 59624 KB Output is correct
77 Correct 192 ms 64704 KB Output is correct
78 Correct 157 ms 66888 KB Output is correct
79 Correct 126 ms 55992 KB Output is correct
80 Correct 182 ms 66692 KB Output is correct
81 Correct 263 ms 79920 KB Output is correct
82 Correct 289 ms 83992 KB Output is correct
83 Correct 299 ms 92540 KB Output is correct
84 Correct 140 ms 64288 KB Output is correct
85 Correct 258 ms 85744 KB Output is correct
86 Correct 96 ms 37868 KB Output is correct
87 Correct 272 ms 64388 KB Output is correct
88 Correct 269 ms 64376 KB Output is correct
89 Correct 260 ms 67400 KB Output is correct
90 Correct 248 ms 73960 KB Output is correct
91 Correct 236 ms 70124 KB Output is correct
92 Correct 262 ms 69708 KB Output is correct
93 Correct 340 ms 83716 KB Output is correct
94 Correct 376 ms 88504 KB Output is correct
95 Correct 382 ms 96528 KB Output is correct
96 Correct 226 ms 68892 KB Output is correct
97 Correct 312 ms 78616 KB Output is correct