답안 #1065279

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1065279 2024-08-19T05:49:38 Z ShadowShark 자매 도시 (APIO20_swap) C++17
100 / 100
353 ms 91732 KB
#include <bits/stdc++.h>
#include "swap.h"
using namespace std;

const int maxN = 3e5 + 5;
int pre[maxN], sz[maxN], curr[maxN], val[maxN], par[20][maxN], depth[maxN], mx[20][maxN], deg[maxN], node;
bool take[maxN], good[maxN], is_true[20][maxN], is_there_any_answer = false;
vector<int> adj[maxN];

int find_set(int u) {
    return (u == pre[u]) ? u : pre[u] = find_set(pre[u]);
}

void union_set(int u, int v, int w) {
    int a = find_set(u), b = find_set(v);
    ++node;

    if (a == b) {
        adj[node].push_back(curr[a]);
        curr[a] = node;
        good[a] = true;
        take[node] = true;
        val[node] = w;
        return ;
    }

    if (sz[a] < sz[b]) swap(a, b);

    pre[b] = a;
    sz[b] += sz[a];
    adj[node].push_back(curr[a]);
    adj[node].push_back(curr[b]);
    curr[a] = node;

    if (good[a] || good[b] || ++deg[u] >= 3 || ++deg[v] >= 3) good[a] = true;
    take[node] = good[a];
    val[node] = w;
}

void dfs(int u, int pre) {
    is_there_any_answer |= take[u];

    for (auto v: adj[u]) {
        if (v == pre) continue;
        par[0][v] = u;
        mx[0][v] = val[u];
        is_true[0][v] = take[u];
        depth[v] = depth[u] + 1;
        dfs(v, u);
    }
}

struct Ed {
    int u, v, w;

    bool operator < (const Ed& rhs) const {
        return w < rhs.w;
    }
} edge[maxN];

int get_lca(int u, int v) {
    if (depth[u] < depth[v]) swap(u, v);

    for (int k = 18; k >= 0; k--) {
        if (depth[par[k][u]] >= depth[v])
            u = par[k][u];
    }

    if (u == v) return u;

    for (int k = 18; k >= 0; k--) {
        if (par[k][u] != par[k][v]) {
            u = par[k][u];
            v = par[k][v];
        }
    }

    return par[0][u];
}

void init(int n, int m, vector<int> U, vector<int> V, vector<int> W) {
    for (int i = 1; i <= n + m; i++) {
        pre[i] = i;
        sz[i] = 1;
        if (i <= n) curr[i] = i;
    }

    for (int i = 0; i < m; i++) {
        U[i]++; V[i]++;
        edge[i + 1] = {U[i], V[i], W[i]};
    }

    sort(edge + 1, edge + m + 1);

    node = n;

    for (int i = 1; i <= m; i++) {
        auto [u, v, w] = edge[i];
        union_set(u, v, w);
    }

    depth[n + m] = 1;
    dfs(n + m, n + m);

    //for (int i = 1; i <= n + m; i++) {
        //cout << "Debug " << val[i] << ' '<< take[i] << '\n';
    //}

    for (int k = 1; k <= 18; k++)
        for (int i = 1; i <= n + m; i++) {
            par[k][i] = par[k - 1][par[k - 1][i]];
            mx[k][i] = max(mx[k - 1][i], mx[k - 1][par[k - 1][i]]);
            is_true[k][i] = (is_true[k - 1][i] || is_true[k - 1][par[k - 1][i]]) ? true : false;
        }
}

int getMinimumFuelCapacity(int x, int y) {
    if (!is_there_any_answer) return -1;

    x++; y++;

    int lca = get_lca(x, y), ans = 0; bool valid = false;

    for (int k = 18; k >= 0; k--)
        if (depth[par[k][x]] >= depth[lca]) {
            ans = max(ans, mx[k][x]);
            valid |= is_true[k][x];
            x = par[k][x];
        }

    for (int k = 18; k >= 0; k--)
        if (depth[par[k][y]] >= depth[lca]) {
            ans = max(ans, mx[k][y]);
            valid |= is_true[k][y];
            y = par[k][y];
        }

    if (valid) return ans;

    for (int k = 18; k >= 0; k--)
        if (!is_true[k][lca]) {
            ans = max(ans, mx[k][lca]);
            lca = par[k][lca];
        }

    ans = max(ans, mx[0][lca]);
    return ans;
}

/*
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int mn, nm;
    cin >> mn >> nm;

    vector<int> a, b, c;

    for (int i = 1; i <= nm; i++) {
        int u, v, w;
        cin >> u >> v >> w;

        a.push_back(u);
        b.push_back(v);
        c.push_back(w);
    }

    init(mn, nm, a, b, c);

    int q;
    cin >> q;

    while (q--) {
        int x, y;
        cin >> x >> y;

        cout << getMinimumFuelCapacity(x, y) << '\n';
    }

    return 0;
}*/
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7648 KB Output is correct
2 Correct 3 ms 7768 KB Output is correct
3 Correct 3 ms 7772 KB Output is correct
4 Correct 5 ms 8028 KB Output is correct
5 Correct 4 ms 8280 KB Output is correct
6 Correct 4 ms 8028 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 56 ms 43688 KB Output is correct
10 Correct 65 ms 51792 KB Output is correct
11 Correct 65 ms 51028 KB Output is correct
12 Correct 82 ms 53584 KB Output is correct
13 Correct 70 ms 57436 KB Output is correct
14 Correct 55 ms 43824 KB Output is correct
15 Correct 99 ms 56176 KB Output is correct
16 Correct 93 ms 54772 KB Output is correct
17 Correct 101 ms 57592 KB Output is correct
18 Correct 94 ms 61620 KB Output is correct
19 Correct 95 ms 20060 KB Output is correct
20 Correct 201 ms 57264 KB Output is correct
21 Correct 200 ms 56016 KB Output is correct
22 Correct 236 ms 59088 KB Output is correct
23 Correct 353 ms 62900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7648 KB Output is correct
2 Correct 3 ms 7768 KB Output is correct
3 Correct 314 ms 63212 KB Output is correct
4 Correct 284 ms 65472 KB Output is correct
5 Correct 295 ms 64020 KB Output is correct
6 Correct 272 ms 65240 KB Output is correct
7 Correct 296 ms 64724 KB Output is correct
8 Correct 307 ms 62716 KB Output is correct
9 Correct 278 ms 64208 KB Output is correct
10 Correct 301 ms 62032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7648 KB Output is correct
2 Correct 3 ms 7768 KB Output is correct
3 Correct 3 ms 7772 KB Output is correct
4 Correct 5 ms 8028 KB Output is correct
5 Correct 4 ms 8280 KB Output is correct
6 Correct 4 ms 8028 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 3 ms 8008 KB Output is correct
10 Correct 4 ms 8284 KB Output is correct
11 Correct 4 ms 8284 KB Output is correct
12 Correct 3 ms 8284 KB Output is correct
13 Correct 3 ms 8028 KB Output is correct
14 Correct 3 ms 8028 KB Output is correct
15 Correct 4 ms 8272 KB Output is correct
16 Correct 4 ms 8284 KB Output is correct
17 Correct 4 ms 8536 KB Output is correct
18 Correct 4 ms 8284 KB Output is correct
19 Correct 4 ms 8160 KB Output is correct
20 Correct 4 ms 8284 KB Output is correct
21 Correct 3 ms 8284 KB Output is correct
22 Correct 4 ms 8284 KB Output is correct
23 Correct 3 ms 8000 KB Output is correct
24 Correct 4 ms 8540 KB Output is correct
25 Correct 4 ms 8524 KB Output is correct
26 Correct 4 ms 8540 KB Output is correct
27 Correct 4 ms 8284 KB Output is correct
28 Correct 4 ms 8424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8008 KB Output is correct
2 Correct 4 ms 7648 KB Output is correct
3 Correct 3 ms 7768 KB Output is correct
4 Correct 3 ms 7772 KB Output is correct
5 Correct 5 ms 8028 KB Output is correct
6 Correct 4 ms 8280 KB Output is correct
7 Correct 4 ms 8028 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Correct 3 ms 8284 KB Output is correct
10 Correct 56 ms 43688 KB Output is correct
11 Correct 65 ms 51792 KB Output is correct
12 Correct 65 ms 51028 KB Output is correct
13 Correct 82 ms 53584 KB Output is correct
14 Correct 70 ms 57436 KB Output is correct
15 Correct 4 ms 8284 KB Output is correct
16 Correct 4 ms 8284 KB Output is correct
17 Correct 3 ms 8284 KB Output is correct
18 Correct 3 ms 8028 KB Output is correct
19 Correct 3 ms 8028 KB Output is correct
20 Correct 4 ms 8272 KB Output is correct
21 Correct 4 ms 8284 KB Output is correct
22 Correct 4 ms 8536 KB Output is correct
23 Correct 4 ms 8284 KB Output is correct
24 Correct 4 ms 8160 KB Output is correct
25 Correct 4 ms 8284 KB Output is correct
26 Correct 3 ms 8284 KB Output is correct
27 Correct 4 ms 8284 KB Output is correct
28 Correct 3 ms 8000 KB Output is correct
29 Correct 4 ms 8540 KB Output is correct
30 Correct 4 ms 8524 KB Output is correct
31 Correct 4 ms 8540 KB Output is correct
32 Correct 4 ms 8284 KB Output is correct
33 Correct 4 ms 8424 KB Output is correct
34 Correct 11 ms 13916 KB Output is correct
35 Correct 68 ms 53328 KB Output is correct
36 Correct 67 ms 53232 KB Output is correct
37 Correct 69 ms 53332 KB Output is correct
38 Correct 66 ms 52564 KB Output is correct
39 Correct 67 ms 52540 KB Output is correct
40 Correct 62 ms 48628 KB Output is correct
41 Correct 70 ms 53588 KB Output is correct
42 Correct 65 ms 53204 KB Output is correct
43 Correct 77 ms 58460 KB Output is correct
44 Correct 86 ms 53580 KB Output is correct
45 Correct 116 ms 69456 KB Output is correct
46 Correct 70 ms 53236 KB Output is correct
47 Correct 68 ms 53328 KB Output is correct
48 Correct 81 ms 58700 KB Output is correct
49 Correct 71 ms 62520 KB Output is correct
50 Correct 78 ms 50768 KB Output is correct
51 Correct 82 ms 61012 KB Output is correct
52 Correct 103 ms 75028 KB Output is correct
53 Correct 110 ms 79372 KB Output is correct
54 Correct 113 ms 87888 KB Output is correct
55 Correct 62 ms 58064 KB Output is correct
56 Correct 110 ms 80980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7648 KB Output is correct
2 Correct 3 ms 7768 KB Output is correct
3 Correct 3 ms 7772 KB Output is correct
4 Correct 5 ms 8028 KB Output is correct
5 Correct 4 ms 8280 KB Output is correct
6 Correct 4 ms 8028 KB Output is correct
7 Correct 4 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 56 ms 43688 KB Output is correct
10 Correct 65 ms 51792 KB Output is correct
11 Correct 65 ms 51028 KB Output is correct
12 Correct 82 ms 53584 KB Output is correct
13 Correct 70 ms 57436 KB Output is correct
14 Correct 55 ms 43824 KB Output is correct
15 Correct 99 ms 56176 KB Output is correct
16 Correct 93 ms 54772 KB Output is correct
17 Correct 101 ms 57592 KB Output is correct
18 Correct 94 ms 61620 KB Output is correct
19 Correct 314 ms 63212 KB Output is correct
20 Correct 284 ms 65472 KB Output is correct
21 Correct 295 ms 64020 KB Output is correct
22 Correct 272 ms 65240 KB Output is correct
23 Correct 296 ms 64724 KB Output is correct
24 Correct 307 ms 62716 KB Output is correct
25 Correct 278 ms 64208 KB Output is correct
26 Correct 301 ms 62032 KB Output is correct
27 Correct 4 ms 8284 KB Output is correct
28 Correct 4 ms 8284 KB Output is correct
29 Correct 3 ms 8284 KB Output is correct
30 Correct 3 ms 8028 KB Output is correct
31 Correct 3 ms 8028 KB Output is correct
32 Correct 4 ms 8272 KB Output is correct
33 Correct 4 ms 8284 KB Output is correct
34 Correct 4 ms 8536 KB Output is correct
35 Correct 4 ms 8284 KB Output is correct
36 Correct 11 ms 13916 KB Output is correct
37 Correct 68 ms 53328 KB Output is correct
38 Correct 67 ms 53232 KB Output is correct
39 Correct 69 ms 53332 KB Output is correct
40 Correct 66 ms 52564 KB Output is correct
41 Correct 67 ms 52540 KB Output is correct
42 Correct 62 ms 48628 KB Output is correct
43 Correct 70 ms 53588 KB Output is correct
44 Correct 65 ms 53204 KB Output is correct
45 Correct 77 ms 58460 KB Output is correct
46 Correct 86 ms 53580 KB Output is correct
47 Correct 22 ms 13912 KB Output is correct
48 Correct 220 ms 58044 KB Output is correct
49 Correct 216 ms 58040 KB Output is correct
50 Correct 196 ms 58040 KB Output is correct
51 Correct 193 ms 57940 KB Output is correct
52 Correct 185 ms 55128 KB Output is correct
53 Correct 168 ms 43872 KB Output is correct
54 Correct 216 ms 58916 KB Output is correct
55 Correct 201 ms 58044 KB Output is correct
56 Correct 305 ms 62136 KB Output is correct
57 Correct 209 ms 59064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8008 KB Output is correct
2 Correct 4 ms 7648 KB Output is correct
3 Correct 3 ms 7768 KB Output is correct
4 Correct 3 ms 7772 KB Output is correct
5 Correct 5 ms 8028 KB Output is correct
6 Correct 4 ms 8280 KB Output is correct
7 Correct 4 ms 8028 KB Output is correct
8 Correct 4 ms 8284 KB Output is correct
9 Correct 3 ms 8284 KB Output is correct
10 Correct 56 ms 43688 KB Output is correct
11 Correct 65 ms 51792 KB Output is correct
12 Correct 65 ms 51028 KB Output is correct
13 Correct 82 ms 53584 KB Output is correct
14 Correct 70 ms 57436 KB Output is correct
15 Correct 55 ms 43824 KB Output is correct
16 Correct 99 ms 56176 KB Output is correct
17 Correct 93 ms 54772 KB Output is correct
18 Correct 101 ms 57592 KB Output is correct
19 Correct 94 ms 61620 KB Output is correct
20 Correct 314 ms 63212 KB Output is correct
21 Correct 284 ms 65472 KB Output is correct
22 Correct 295 ms 64020 KB Output is correct
23 Correct 272 ms 65240 KB Output is correct
24 Correct 296 ms 64724 KB Output is correct
25 Correct 307 ms 62716 KB Output is correct
26 Correct 278 ms 64208 KB Output is correct
27 Correct 301 ms 62032 KB Output is correct
28 Correct 4 ms 8284 KB Output is correct
29 Correct 4 ms 8284 KB Output is correct
30 Correct 3 ms 8284 KB Output is correct
31 Correct 3 ms 8028 KB Output is correct
32 Correct 3 ms 8028 KB Output is correct
33 Correct 4 ms 8272 KB Output is correct
34 Correct 4 ms 8284 KB Output is correct
35 Correct 4 ms 8536 KB Output is correct
36 Correct 4 ms 8284 KB Output is correct
37 Correct 11 ms 13916 KB Output is correct
38 Correct 68 ms 53328 KB Output is correct
39 Correct 67 ms 53232 KB Output is correct
40 Correct 69 ms 53332 KB Output is correct
41 Correct 66 ms 52564 KB Output is correct
42 Correct 67 ms 52540 KB Output is correct
43 Correct 62 ms 48628 KB Output is correct
44 Correct 70 ms 53588 KB Output is correct
45 Correct 65 ms 53204 KB Output is correct
46 Correct 77 ms 58460 KB Output is correct
47 Correct 86 ms 53580 KB Output is correct
48 Correct 22 ms 13912 KB Output is correct
49 Correct 220 ms 58044 KB Output is correct
50 Correct 216 ms 58040 KB Output is correct
51 Correct 196 ms 58040 KB Output is correct
52 Correct 193 ms 57940 KB Output is correct
53 Correct 185 ms 55128 KB Output is correct
54 Correct 168 ms 43872 KB Output is correct
55 Correct 216 ms 58916 KB Output is correct
56 Correct 201 ms 58044 KB Output is correct
57 Correct 305 ms 62136 KB Output is correct
58 Correct 209 ms 59064 KB Output is correct
59 Correct 95 ms 20060 KB Output is correct
60 Correct 201 ms 57264 KB Output is correct
61 Correct 200 ms 56016 KB Output is correct
62 Correct 236 ms 59088 KB Output is correct
63 Correct 353 ms 62900 KB Output is correct
64 Correct 4 ms 8160 KB Output is correct
65 Correct 4 ms 8284 KB Output is correct
66 Correct 3 ms 8284 KB Output is correct
67 Correct 4 ms 8284 KB Output is correct
68 Correct 3 ms 8000 KB Output is correct
69 Correct 4 ms 8540 KB Output is correct
70 Correct 4 ms 8524 KB Output is correct
71 Correct 4 ms 8540 KB Output is correct
72 Correct 4 ms 8284 KB Output is correct
73 Correct 4 ms 8424 KB Output is correct
74 Correct 116 ms 69456 KB Output is correct
75 Correct 70 ms 53236 KB Output is correct
76 Correct 68 ms 53328 KB Output is correct
77 Correct 81 ms 58700 KB Output is correct
78 Correct 71 ms 62520 KB Output is correct
79 Correct 78 ms 50768 KB Output is correct
80 Correct 82 ms 61012 KB Output is correct
81 Correct 103 ms 75028 KB Output is correct
82 Correct 110 ms 79372 KB Output is correct
83 Correct 113 ms 87888 KB Output is correct
84 Correct 62 ms 58064 KB Output is correct
85 Correct 110 ms 80980 KB Output is correct
86 Correct 70 ms 30468 KB Output is correct
87 Correct 182 ms 58300 KB Output is correct
88 Correct 185 ms 58192 KB Output is correct
89 Correct 272 ms 61216 KB Output is correct
90 Correct 185 ms 69548 KB Output is correct
91 Correct 213 ms 65396 KB Output is correct
92 Correct 289 ms 64188 KB Output is correct
93 Correct 233 ms 78928 KB Output is correct
94 Correct 316 ms 83756 KB Output is correct
95 Correct 266 ms 91732 KB Output is correct
96 Correct 310 ms 62676 KB Output is correct
97 Correct 347 ms 73232 KB Output is correct