답안 #734705

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
734705 2023-05-02T23:12:53 Z DrearyJoke 자매 도시 (APIO20_swap) C++17
100 / 100
398 ms 58756 KB
#include "swap.h"

#include<bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define END "\n"
#define rall(v) (v).rbegin(), (v).rend()
#define all(v) (v).begin(), (v).end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

vector<int> parent, sz;
void make_set(int n) {
    parent.resize(n);
    iota(all(parent), 0);
    sz.assign(n, 1);
}

int find_parent(int a) {
    if (a == parent[a]) return a;
    else return parent[a] = find_parent(parent[a]);
}

vector<vector<int>> adj;
vector<int> good, weight;

struct LCA {
    int n, d;
    vector<vector<int>> parent;
    vector<int> depth;
    LCA () {}
    LCA (const vector<vector<int>>& adj, int r) {
        n = adj.size();
        assert(r < adj.size());
        d = 0;
        while ((1 << d) <= n) d++;
        depth.resize(n);
        parent.resize(d, vector<int>(n));
        parent[0][r] = r;
        dfs(adj, r, -1);
        pre_calc();
    }
    void dfs(const vector<vector<int>>& adj, int v, int p) {
        // cout << "VISITING: " << v << " " << p << endl;
        for (auto u: adj[v]) {
            if (u != p) 
                parent[0][u] = v, depth[u] = depth[v] + 1, dfs(adj, u, v);
        }
    }
    void pre_calc() {
        for (int i = 1; i < d; ++i) {
            for (int j = 0; j < n; ++j) {
                parent[i][j] = parent[i - 1][parent[i - 1][j]];
            }
        }
    }
    int walk(int v, int x) {
        for (int i = d - 1; i >= 0; --i) {
            if (x & (1 << i)) v = parent[i][v];
        }
        return v;
    }
    int lca(int u, int v) {
        if (depth[u] < depth[v]) swap(u, v);
        int x = depth[u] - depth[v];
        u = walk(u, x);
        if (u == v) return u;
        for (int i = d - 1; i >= 0; --i) {
            if (parent[i][u] != parent[i][v]) u = parent[i][u], v = parent[i][v];
        }
        return parent[0][u];
    }
};

LCA lca;
int root;

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    make_set(N + M);
    adj.clear();
    adj.resize(N + M);
    vector<int> idxes(M);
    iota(all(idxes), 0);
    sort(all(idxes), [&] (int a, int b) {
        return W[a] < W[b];
    });
    int counter = N;
    good.resize(N + M), weight.resize(N + M);
    vector<int> degree(N);
    for (int i = 0; i < M; ++i) {
        int idx = idxes[i];
        int a = find_parent(U[idx]), b = find_parent(V[idx]), w = W[idx];
        ++degree[U[idx]];
        ++degree[V[idx]];
        int v = counter++;
        // cout << "ADDING EDGE: " << v << " " << a << " " << b << endl;
        adj[v].push_back(a);
        if (a != b) adj[v].push_back(b);
        if (a == b || good[a] || good[b] || degree[U[idx]] > 2 || degree[V[idx]] > 2) {
            good[v] = 1;
        }
        weight[v] = w;
        parent[a] = v;
        parent[b] = v;
    }
    assert(counter == N + M);
    lca = LCA(adj, counter - 1);
    // for (int i = 0; i < N + M; ++i) cout << i << ": " << lca.parent[0][i] << endl;
    root = counter - 1;
}

int getMinimumFuelCapacity(int X, int Y) {
    int V = lca.lca(X, Y);
    // cout << X << " " << Y << ": " << V << endl;
    if (good[V]) return weight[V];
    for (int i = lca.d - 1; i >= 0; --i) {
        if (!good[lca.parent[i][V]]) V = lca.parent[i][V]; 
    } 
    // cout << "FINAL: " << X << " " << Y << ": " << V << endl;
    if (V == root) return -1;
    return weight[lca.parent[0][V]];
    return 0;
}

Compilation message

In file included from /usr/include/c++/10/ext/pb_ds/detail/pat_trie_/pat_trie_.hpp:45,
                 from /usr/include/c++/10/ext/pb_ds/detail/container_base_dispatch.hpp:90,
                 from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:48,
                 from swap.cpp:19:
swap.cpp: In constructor 'LCA::LCA(const std::vector<std::vector<int> >&, int)':
swap.cpp:48:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |         assert(r < adj.size());
      |                ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 568 KB Output is correct
9 Correct 67 ms 25404 KB Output is correct
10 Correct 93 ms 30836 KB Output is correct
11 Correct 92 ms 30448 KB Output is correct
12 Correct 91 ms 32180 KB Output is correct
13 Correct 77 ms 33064 KB Output is correct
14 Correct 93 ms 25524 KB Output is correct
15 Correct 259 ms 34948 KB Output is correct
16 Correct 225 ms 34140 KB Output is correct
17 Correct 264 ms 36004 KB Output is correct
18 Correct 276 ms 36872 KB Output is correct
19 Correct 77 ms 10032 KB Output is correct
20 Correct 256 ms 34992 KB Output is correct
21 Correct 226 ms 34064 KB Output is correct
22 Correct 242 ms 36064 KB Output is correct
23 Correct 276 ms 37716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 225 ms 36912 KB Output is correct
4 Correct 256 ms 38516 KB Output is correct
5 Correct 251 ms 37764 KB Output is correct
6 Correct 260 ms 38280 KB Output is correct
7 Correct 250 ms 38104 KB Output is correct
8 Correct 302 ms 36688 KB Output is correct
9 Correct 248 ms 37840 KB Output is correct
10 Correct 283 ms 36440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 568 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 440 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 432 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 440 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 2 ms 596 KB Output is correct
23 Correct 1 ms 440 KB Output is correct
24 Correct 2 ms 720 KB Output is correct
25 Correct 2 ms 696 KB Output is correct
26 Correct 2 ms 696 KB Output is correct
27 Correct 2 ms 468 KB Output is correct
28 Correct 2 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 568 KB Output is correct
10 Correct 67 ms 25404 KB Output is correct
11 Correct 93 ms 30836 KB Output is correct
12 Correct 92 ms 30448 KB Output is correct
13 Correct 91 ms 32180 KB Output is correct
14 Correct 77 ms 33064 KB Output is correct
15 Correct 1 ms 440 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 432 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 440 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 1 ms 440 KB Output is correct
29 Correct 2 ms 720 KB Output is correct
30 Correct 2 ms 696 KB Output is correct
31 Correct 2 ms 696 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 724 KB Output is correct
34 Correct 13 ms 4308 KB Output is correct
35 Correct 85 ms 31928 KB Output is correct
36 Correct 83 ms 31868 KB Output is correct
37 Correct 85 ms 31812 KB Output is correct
38 Correct 109 ms 31408 KB Output is correct
39 Correct 78 ms 31268 KB Output is correct
40 Correct 72 ms 28616 KB Output is correct
41 Correct 88 ms 32172 KB Output is correct
42 Correct 85 ms 31820 KB Output is correct
43 Correct 69 ms 33212 KB Output is correct
44 Correct 84 ms 32228 KB Output is correct
45 Correct 116 ms 41052 KB Output is correct
46 Correct 83 ms 31928 KB Output is correct
47 Correct 77 ms 31732 KB Output is correct
48 Correct 86 ms 34172 KB Output is correct
49 Correct 96 ms 35560 KB Output is correct
50 Correct 83 ms 27552 KB Output is correct
51 Correct 116 ms 35356 KB Output is correct
52 Correct 131 ms 45308 KB Output is correct
53 Correct 161 ms 49680 KB Output is correct
54 Correct 183 ms 54836 KB Output is correct
55 Correct 84 ms 33052 KB Output is correct
56 Correct 149 ms 49632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 300 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 568 KB Output is correct
9 Correct 67 ms 25404 KB Output is correct
10 Correct 93 ms 30836 KB Output is correct
11 Correct 92 ms 30448 KB Output is correct
12 Correct 91 ms 32180 KB Output is correct
13 Correct 77 ms 33064 KB Output is correct
14 Correct 93 ms 25524 KB Output is correct
15 Correct 259 ms 34948 KB Output is correct
16 Correct 225 ms 34140 KB Output is correct
17 Correct 264 ms 36004 KB Output is correct
18 Correct 276 ms 36872 KB Output is correct
19 Correct 225 ms 36912 KB Output is correct
20 Correct 256 ms 38516 KB Output is correct
21 Correct 251 ms 37764 KB Output is correct
22 Correct 260 ms 38280 KB Output is correct
23 Correct 250 ms 38104 KB Output is correct
24 Correct 302 ms 36688 KB Output is correct
25 Correct 248 ms 37840 KB Output is correct
26 Correct 283 ms 36440 KB Output is correct
27 Correct 1 ms 440 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 432 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 13 ms 4308 KB Output is correct
37 Correct 85 ms 31928 KB Output is correct
38 Correct 83 ms 31868 KB Output is correct
39 Correct 85 ms 31812 KB Output is correct
40 Correct 109 ms 31408 KB Output is correct
41 Correct 78 ms 31268 KB Output is correct
42 Correct 72 ms 28616 KB Output is correct
43 Correct 88 ms 32172 KB Output is correct
44 Correct 85 ms 31820 KB Output is correct
45 Correct 69 ms 33212 KB Output is correct
46 Correct 84 ms 32228 KB Output is correct
47 Correct 18 ms 4388 KB Output is correct
48 Correct 225 ms 35904 KB Output is correct
49 Correct 187 ms 35804 KB Output is correct
50 Correct 238 ms 35636 KB Output is correct
51 Correct 187 ms 35404 KB Output is correct
52 Correct 190 ms 33680 KB Output is correct
53 Correct 153 ms 26292 KB Output is correct
54 Correct 246 ms 36052 KB Output is correct
55 Correct 206 ms 35752 KB Output is correct
56 Correct 299 ms 36864 KB Output is correct
57 Correct 190 ms 36216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 568 KB Output is correct
10 Correct 67 ms 25404 KB Output is correct
11 Correct 93 ms 30836 KB Output is correct
12 Correct 92 ms 30448 KB Output is correct
13 Correct 91 ms 32180 KB Output is correct
14 Correct 77 ms 33064 KB Output is correct
15 Correct 93 ms 25524 KB Output is correct
16 Correct 259 ms 34948 KB Output is correct
17 Correct 225 ms 34140 KB Output is correct
18 Correct 264 ms 36004 KB Output is correct
19 Correct 276 ms 36872 KB Output is correct
20 Correct 225 ms 36912 KB Output is correct
21 Correct 256 ms 38516 KB Output is correct
22 Correct 251 ms 37764 KB Output is correct
23 Correct 260 ms 38280 KB Output is correct
24 Correct 250 ms 38104 KB Output is correct
25 Correct 302 ms 36688 KB Output is correct
26 Correct 248 ms 37840 KB Output is correct
27 Correct 283 ms 36440 KB Output is correct
28 Correct 1 ms 440 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 432 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 13 ms 4308 KB Output is correct
38 Correct 85 ms 31928 KB Output is correct
39 Correct 83 ms 31868 KB Output is correct
40 Correct 85 ms 31812 KB Output is correct
41 Correct 109 ms 31408 KB Output is correct
42 Correct 78 ms 31268 KB Output is correct
43 Correct 72 ms 28616 KB Output is correct
44 Correct 88 ms 32172 KB Output is correct
45 Correct 85 ms 31820 KB Output is correct
46 Correct 69 ms 33212 KB Output is correct
47 Correct 84 ms 32228 KB Output is correct
48 Correct 18 ms 4388 KB Output is correct
49 Correct 225 ms 35904 KB Output is correct
50 Correct 187 ms 35804 KB Output is correct
51 Correct 238 ms 35636 KB Output is correct
52 Correct 187 ms 35404 KB Output is correct
53 Correct 190 ms 33680 KB Output is correct
54 Correct 153 ms 26292 KB Output is correct
55 Correct 246 ms 36052 KB Output is correct
56 Correct 206 ms 35752 KB Output is correct
57 Correct 299 ms 36864 KB Output is correct
58 Correct 190 ms 36216 KB Output is correct
59 Correct 77 ms 10032 KB Output is correct
60 Correct 256 ms 34992 KB Output is correct
61 Correct 226 ms 34064 KB Output is correct
62 Correct 242 ms 36064 KB Output is correct
63 Correct 276 ms 37716 KB Output is correct
64 Correct 1 ms 440 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 2 ms 596 KB Output is correct
68 Correct 1 ms 440 KB Output is correct
69 Correct 2 ms 720 KB Output is correct
70 Correct 2 ms 696 KB Output is correct
71 Correct 2 ms 696 KB Output is correct
72 Correct 2 ms 468 KB Output is correct
73 Correct 2 ms 724 KB Output is correct
74 Correct 116 ms 41052 KB Output is correct
75 Correct 83 ms 31928 KB Output is correct
76 Correct 77 ms 31732 KB Output is correct
77 Correct 86 ms 34172 KB Output is correct
78 Correct 96 ms 35560 KB Output is correct
79 Correct 83 ms 27552 KB Output is correct
80 Correct 116 ms 35356 KB Output is correct
81 Correct 131 ms 45308 KB Output is correct
82 Correct 161 ms 49680 KB Output is correct
83 Correct 183 ms 54836 KB Output is correct
84 Correct 84 ms 33052 KB Output is correct
85 Correct 149 ms 49632 KB Output is correct
86 Correct 85 ms 15164 KB Output is correct
87 Correct 190 ms 35756 KB Output is correct
88 Correct 171 ms 35776 KB Output is correct
89 Correct 218 ms 36496 KB Output is correct
90 Correct 190 ms 41040 KB Output is correct
91 Correct 199 ms 38588 KB Output is correct
92 Correct 235 ms 38512 KB Output is correct
93 Correct 259 ms 48540 KB Output is correct
94 Correct 398 ms 53252 KB Output is correct
95 Correct 317 ms 58756 KB Output is correct
96 Correct 259 ms 37152 KB Output is correct
97 Correct 310 ms 44396 KB Output is correct