답안 #402067

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
402067 2021-05-11T09:23:13 Z KoD 자매 도시 (APIO20_swap) C++17
100 / 100
330 ms 16220 KB
#include <bits/stdc++.h>
#include "swap.h"

template <class T>
using Vec = std::vector<T>;

constexpr int INF = 1000000000 + 5;

struct DSU {
    int cur;
    Vec<int> date, par;
    DSU() = default;
    DSU(const int n): cur(0), date(n, INF), par(n, -1) { }

    int find(int u, const int t) const {
        while (date[u] < t) u = par[u];
        return u;
    }

    void merge(int u, int v) {
        u = find(u, cur);
        v = find(v, cur);
        cur += 1;
        if (u == v) return;
        if (par[u] > par[v]) {
            std::swap(u, v);
        }
        par[u] += par[v];
        par[v] = u;
        date[v] = cur - 1;
    }

    int when(const int u, const int v) const {
        if (u == v) return 0;
        if (find(u, cur) != find(v, cur)) return INF;
        int ok = cur, ng = 0;
        while (ok - ng > 1) {
            const int md = (ok + ng) / 2;
            (find(u, md) == find(v, md) ? ok : ng) = md;
        }
        return ok;
    }
};

DSU dsu;
Vec<int> par, cost, date;

void init(int N, int M, Vec<int> U, Vec<int> V, Vec<int> W) {
    Vec<int> order(M);
    std::iota(order.begin(), order.end(), 0);
    std::sort(order.begin(), order.end(), [&](const int i, const int j) {
        return W[i] < W[j];
    });
    par = Vec<int>(N, -1);
    date = Vec<int>(N, INF);
    Vec<int> deg(N, 0);
    dsu = DSU(N);
    cost.reserve(M);
    const auto find = [&](int u) {
        while (par[u] >= 0) u = par[u];
        return u;
    };
    for (const auto i: order) {
        deg[U[i]] += 1;
        deg[V[i]] += 1;
        const bool f = deg[U[i]] > 2 or deg[V[i]] > 2;
        int u = find(U[i]);
        int v = find(V[i]);
        if (u == v) {
            if (date[u] == INF) {
                date[u] = W[i];
            }
        }
        else {
            if (par[u] > par[v]) {
                std::swap(u, v);
            }
            par[u] += par[v];
            par[v] = u;
            if (date[u] == INF and (date[v] != INF or f)) {
                date[u] = W[i];
            }
            if (date[v] == INF and (date[u] != INF or f)) {
                date[v] = W[i];
            }
        }
        dsu.merge(U[i], V[i]);
        cost.push_back(W[i]);
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    int ret = cost[dsu.when(X, Y) - 1];
    while (par[X] >= 0 and date[X] == INF) {
        X = par[X];
    }
    while (par[Y] >= 0 and date[Y] == INF) {
        Y = par[Y];
    }
    ret = std::max(ret, std::min(date[X], date[Y]));
    return ret == INF ? -1: ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 43 ms 4288 KB Output is correct
10 Correct 55 ms 5104 KB Output is correct
11 Correct 52 ms 5060 KB Output is correct
12 Correct 56 ms 5316 KB Output is correct
13 Correct 54 ms 5328 KB Output is correct
14 Correct 60 ms 4416 KB Output is correct
15 Correct 222 ms 6684 KB Output is correct
16 Correct 221 ms 6520 KB Output is correct
17 Correct 227 ms 6852 KB Output is correct
18 Correct 151 ms 6836 KB Output is correct
19 Correct 122 ms 4548 KB Output is correct
20 Correct 218 ms 7332 KB Output is correct
21 Correct 239 ms 7504 KB Output is correct
22 Correct 238 ms 7696 KB Output is correct
23 Correct 152 ms 7688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 156 ms 7308 KB Output is correct
4 Correct 144 ms 7380 KB Output is correct
5 Correct 149 ms 11560 KB Output is correct
6 Correct 142 ms 11124 KB Output is correct
7 Correct 148 ms 11528 KB Output is correct
8 Correct 143 ms 11268 KB Output is correct
9 Correct 145 ms 11292 KB Output is correct
10 Correct 147 ms 11176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 43 ms 4288 KB Output is correct
11 Correct 55 ms 5104 KB Output is correct
12 Correct 52 ms 5060 KB Output is correct
13 Correct 56 ms 5316 KB Output is correct
14 Correct 54 ms 5328 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 336 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 8 ms 1204 KB Output is correct
35 Correct 57 ms 7172 KB Output is correct
36 Correct 55 ms 7200 KB Output is correct
37 Correct 55 ms 7128 KB Output is correct
38 Correct 54 ms 7148 KB Output is correct
39 Correct 53 ms 6980 KB Output is correct
40 Correct 50 ms 6452 KB Output is correct
41 Correct 60 ms 7460 KB Output is correct
42 Correct 54 ms 7088 KB Output is correct
43 Correct 55 ms 7080 KB Output is correct
44 Correct 58 ms 7456 KB Output is correct
45 Correct 87 ms 9420 KB Output is correct
46 Correct 56 ms 7108 KB Output is correct
47 Correct 58 ms 7212 KB Output is correct
48 Correct 89 ms 7664 KB Output is correct
49 Correct 86 ms 8008 KB Output is correct
50 Correct 67 ms 6428 KB Output is correct
51 Correct 75 ms 8260 KB Output is correct
52 Correct 95 ms 10624 KB Output is correct
53 Correct 102 ms 11584 KB Output is correct
54 Correct 125 ms 12348 KB Output is correct
55 Correct 72 ms 7108 KB Output is correct
56 Correct 111 ms 11380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 43 ms 4288 KB Output is correct
10 Correct 55 ms 5104 KB Output is correct
11 Correct 52 ms 5060 KB Output is correct
12 Correct 56 ms 5316 KB Output is correct
13 Correct 54 ms 5328 KB Output is correct
14 Correct 60 ms 4416 KB Output is correct
15 Correct 222 ms 6684 KB Output is correct
16 Correct 221 ms 6520 KB Output is correct
17 Correct 227 ms 6852 KB Output is correct
18 Correct 151 ms 6836 KB Output is correct
19 Correct 156 ms 7308 KB Output is correct
20 Correct 144 ms 7380 KB Output is correct
21 Correct 149 ms 11560 KB Output is correct
22 Correct 142 ms 11124 KB Output is correct
23 Correct 148 ms 11528 KB Output is correct
24 Correct 143 ms 11268 KB Output is correct
25 Correct 145 ms 11292 KB Output is correct
26 Correct 147 ms 11176 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 8 ms 1204 KB Output is correct
37 Correct 57 ms 7172 KB Output is correct
38 Correct 55 ms 7200 KB Output is correct
39 Correct 55 ms 7128 KB Output is correct
40 Correct 54 ms 7148 KB Output is correct
41 Correct 53 ms 6980 KB Output is correct
42 Correct 50 ms 6452 KB Output is correct
43 Correct 60 ms 7460 KB Output is correct
44 Correct 54 ms 7088 KB Output is correct
45 Correct 55 ms 7080 KB Output is correct
46 Correct 58 ms 7456 KB Output is correct
47 Correct 20 ms 1612 KB Output is correct
48 Correct 239 ms 11268 KB Output is correct
49 Correct 228 ms 11308 KB Output is correct
50 Correct 227 ms 11216 KB Output is correct
51 Correct 221 ms 11148 KB Output is correct
52 Correct 224 ms 10996 KB Output is correct
53 Correct 187 ms 10388 KB Output is correct
54 Correct 248 ms 12160 KB Output is correct
55 Correct 245 ms 11272 KB Output is correct
56 Correct 147 ms 11292 KB Output is correct
57 Correct 219 ms 12184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 43 ms 4288 KB Output is correct
11 Correct 55 ms 5104 KB Output is correct
12 Correct 52 ms 5060 KB Output is correct
13 Correct 56 ms 5316 KB Output is correct
14 Correct 54 ms 5328 KB Output is correct
15 Correct 60 ms 4416 KB Output is correct
16 Correct 222 ms 6684 KB Output is correct
17 Correct 221 ms 6520 KB Output is correct
18 Correct 227 ms 6852 KB Output is correct
19 Correct 151 ms 6836 KB Output is correct
20 Correct 156 ms 7308 KB Output is correct
21 Correct 144 ms 7380 KB Output is correct
22 Correct 149 ms 11560 KB Output is correct
23 Correct 142 ms 11124 KB Output is correct
24 Correct 148 ms 11528 KB Output is correct
25 Correct 143 ms 11268 KB Output is correct
26 Correct 145 ms 11292 KB Output is correct
27 Correct 147 ms 11176 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 8 ms 1204 KB Output is correct
38 Correct 57 ms 7172 KB Output is correct
39 Correct 55 ms 7200 KB Output is correct
40 Correct 55 ms 7128 KB Output is correct
41 Correct 54 ms 7148 KB Output is correct
42 Correct 53 ms 6980 KB Output is correct
43 Correct 50 ms 6452 KB Output is correct
44 Correct 60 ms 7460 KB Output is correct
45 Correct 54 ms 7088 KB Output is correct
46 Correct 55 ms 7080 KB Output is correct
47 Correct 58 ms 7456 KB Output is correct
48 Correct 20 ms 1612 KB Output is correct
49 Correct 239 ms 11268 KB Output is correct
50 Correct 228 ms 11308 KB Output is correct
51 Correct 227 ms 11216 KB Output is correct
52 Correct 221 ms 11148 KB Output is correct
53 Correct 224 ms 10996 KB Output is correct
54 Correct 187 ms 10388 KB Output is correct
55 Correct 248 ms 12160 KB Output is correct
56 Correct 245 ms 11272 KB Output is correct
57 Correct 147 ms 11292 KB Output is correct
58 Correct 219 ms 12184 KB Output is correct
59 Correct 122 ms 4548 KB Output is correct
60 Correct 218 ms 7332 KB Output is correct
61 Correct 239 ms 7504 KB Output is correct
62 Correct 238 ms 7696 KB Output is correct
63 Correct 152 ms 7688 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 1 ms 332 KB Output is correct
66 Correct 1 ms 332 KB Output is correct
67 Correct 2 ms 332 KB Output is correct
68 Correct 1 ms 348 KB Output is correct
69 Correct 2 ms 336 KB Output is correct
70 Correct 2 ms 332 KB Output is correct
71 Correct 2 ms 332 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 2 ms 332 KB Output is correct
74 Correct 87 ms 9420 KB Output is correct
75 Correct 56 ms 7108 KB Output is correct
76 Correct 58 ms 7212 KB Output is correct
77 Correct 89 ms 7664 KB Output is correct
78 Correct 86 ms 8008 KB Output is correct
79 Correct 67 ms 6428 KB Output is correct
80 Correct 75 ms 8260 KB Output is correct
81 Correct 95 ms 10624 KB Output is correct
82 Correct 102 ms 11584 KB Output is correct
83 Correct 125 ms 12348 KB Output is correct
84 Correct 72 ms 7108 KB Output is correct
85 Correct 111 ms 11380 KB Output is correct
86 Correct 73 ms 4432 KB Output is correct
87 Correct 240 ms 11340 KB Output is correct
88 Correct 229 ms 11292 KB Output is correct
89 Correct 192 ms 11272 KB Output is correct
90 Correct 197 ms 11844 KB Output is correct
91 Correct 200 ms 11836 KB Output is correct
92 Correct 213 ms 12344 KB Output is correct
93 Correct 297 ms 14368 KB Output is correct
94 Correct 249 ms 15280 KB Output is correct
95 Correct 330 ms 16220 KB Output is correct
96 Correct 159 ms 11420 KB Output is correct
97 Correct 223 ms 13476 KB Output is correct