답안 #1104992

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104992 2024-10-25T04:05:33 Z SoMotThanhXuan 자매 도시 (APIO20_swap) C++17
100 / 100
311 ms 98740 KB
#include <bits/stdc++.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, q;
    cin >> mn >> nm >> q;

    vector<int> a, b, c;

    for (int i = 1; i <= nm; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        --u;
        --v;
        a.push_back(u);
        b.push_back(v);
        c.push_back(w);
    }

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


    while (q--) {
        int x, y;
        cin >> x >> y;
        --x;
        --y;
        cout << getMinimumFuelCapacity(x, y) << '\n';
    }

    return 0;
}
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 66128 KB Output is correct
2 Correct 7 ms 66072 KB Output is correct
3 Correct 8 ms 66128 KB Output is correct
4 Correct 9 ms 66128 KB Output is correct
5 Correct 8 ms 66128 KB Output is correct
6 Correct 8 ms 66128 KB Output is correct
7 Correct 8 ms 66128 KB Output is correct
8 Correct 7 ms 66128 KB Output is correct
9 Correct 53 ms 75592 KB Output is correct
10 Correct 65 ms 77128 KB Output is correct
11 Correct 71 ms 77048 KB Output is correct
12 Correct 64 ms 77640 KB Output is correct
13 Correct 66 ms 81480 KB Output is correct
14 Correct 57 ms 75848 KB Output is correct
15 Correct 108 ms 81364 KB Output is correct
16 Correct 97 ms 81212 KB Output is correct
17 Correct 107 ms 81744 KB Output is correct
18 Correct 98 ms 85424 KB Output is correct
19 Correct 105 ms 74836 KB Output is correct
20 Correct 185 ms 82376 KB Output is correct
21 Correct 196 ms 82436 KB Output is correct
22 Correct 195 ms 83120 KB Output is correct
23 Correct 307 ms 86960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 66128 KB Output is correct
2 Correct 7 ms 66072 KB Output is correct
3 Correct 248 ms 89208 KB Output is correct
4 Correct 248 ms 90008 KB Output is correct
5 Correct 265 ms 89924 KB Output is correct
6 Correct 271 ms 89776 KB Output is correct
7 Correct 244 ms 90052 KB Output is correct
8 Correct 251 ms 89128 KB Output is correct
9 Correct 243 ms 89808 KB Output is correct
10 Correct 263 ms 89088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 66128 KB Output is correct
2 Correct 7 ms 66072 KB Output is correct
3 Correct 8 ms 66128 KB Output is correct
4 Correct 9 ms 66128 KB Output is correct
5 Correct 8 ms 66128 KB Output is correct
6 Correct 8 ms 66128 KB Output is correct
7 Correct 8 ms 66128 KB Output is correct
8 Correct 7 ms 66128 KB Output is correct
9 Correct 8 ms 66128 KB Output is correct
10 Correct 9 ms 66128 KB Output is correct
11 Correct 8 ms 66128 KB Output is correct
12 Correct 8 ms 66200 KB Output is correct
13 Correct 8 ms 66128 KB Output is correct
14 Correct 7 ms 65984 KB Output is correct
15 Correct 8 ms 66128 KB Output is correct
16 Correct 8 ms 66128 KB Output is correct
17 Correct 8 ms 66300 KB Output is correct
18 Correct 8 ms 66128 KB Output is correct
19 Correct 8 ms 66232 KB Output is correct
20 Correct 9 ms 66140 KB Output is correct
21 Correct 8 ms 66128 KB Output is correct
22 Correct 8 ms 66384 KB Output is correct
23 Correct 7 ms 66128 KB Output is correct
24 Correct 8 ms 66128 KB Output is correct
25 Correct 8 ms 66128 KB Output is correct
26 Correct 8 ms 66236 KB Output is correct
27 Correct 7 ms 66128 KB Output is correct
28 Correct 9 ms 66276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 66128 KB Output is correct
2 Correct 8 ms 66128 KB Output is correct
3 Correct 7 ms 66072 KB Output is correct
4 Correct 8 ms 66128 KB Output is correct
5 Correct 9 ms 66128 KB Output is correct
6 Correct 8 ms 66128 KB Output is correct
7 Correct 8 ms 66128 KB Output is correct
8 Correct 8 ms 66128 KB Output is correct
9 Correct 7 ms 66128 KB Output is correct
10 Correct 53 ms 75592 KB Output is correct
11 Correct 65 ms 77128 KB Output is correct
12 Correct 71 ms 77048 KB Output is correct
13 Correct 64 ms 77640 KB Output is correct
14 Correct 66 ms 81480 KB Output is correct
15 Correct 9 ms 66128 KB Output is correct
16 Correct 8 ms 66128 KB Output is correct
17 Correct 8 ms 66200 KB Output is correct
18 Correct 8 ms 66128 KB Output is correct
19 Correct 7 ms 65984 KB Output is correct
20 Correct 8 ms 66128 KB Output is correct
21 Correct 8 ms 66128 KB Output is correct
22 Correct 8 ms 66300 KB Output is correct
23 Correct 8 ms 66128 KB Output is correct
24 Correct 8 ms 66232 KB Output is correct
25 Correct 9 ms 66140 KB Output is correct
26 Correct 8 ms 66128 KB Output is correct
27 Correct 8 ms 66384 KB Output is correct
28 Correct 7 ms 66128 KB Output is correct
29 Correct 8 ms 66128 KB Output is correct
30 Correct 8 ms 66128 KB Output is correct
31 Correct 8 ms 66236 KB Output is correct
32 Correct 7 ms 66128 KB Output is correct
33 Correct 9 ms 66276 KB Output is correct
34 Correct 13 ms 67408 KB Output is correct
35 Correct 61 ms 77344 KB Output is correct
36 Correct 63 ms 77392 KB Output is correct
37 Correct 61 ms 77264 KB Output is correct
38 Correct 62 ms 77128 KB Output is correct
39 Correct 58 ms 77128 KB Output is correct
40 Correct 66 ms 76360 KB Output is correct
41 Correct 61 ms 77640 KB Output is correct
42 Correct 62 ms 77128 KB Output is correct
43 Correct 57 ms 82584 KB Output is correct
44 Correct 66 ms 77640 KB Output is correct
45 Correct 84 ms 88648 KB Output is correct
46 Correct 60 ms 77388 KB Output is correct
47 Correct 62 ms 77640 KB Output is correct
48 Correct 78 ms 81364 KB Output is correct
49 Correct 71 ms 93256 KB Output is correct
50 Correct 64 ms 87368 KB Output is correct
51 Correct 90 ms 86344 KB Output is correct
52 Correct 97 ms 87628 KB Output is correct
53 Correct 113 ms 88904 KB Output is correct
54 Correct 117 ms 93768 KB Output is correct
55 Correct 60 ms 81992 KB Output is correct
56 Correct 116 ms 91800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 66128 KB Output is correct
2 Correct 7 ms 66072 KB Output is correct
3 Correct 8 ms 66128 KB Output is correct
4 Correct 9 ms 66128 KB Output is correct
5 Correct 8 ms 66128 KB Output is correct
6 Correct 8 ms 66128 KB Output is correct
7 Correct 8 ms 66128 KB Output is correct
8 Correct 7 ms 66128 KB Output is correct
9 Correct 53 ms 75592 KB Output is correct
10 Correct 65 ms 77128 KB Output is correct
11 Correct 71 ms 77048 KB Output is correct
12 Correct 64 ms 77640 KB Output is correct
13 Correct 66 ms 81480 KB Output is correct
14 Correct 57 ms 75848 KB Output is correct
15 Correct 108 ms 81364 KB Output is correct
16 Correct 97 ms 81212 KB Output is correct
17 Correct 107 ms 81744 KB Output is correct
18 Correct 98 ms 85424 KB Output is correct
19 Correct 248 ms 89208 KB Output is correct
20 Correct 248 ms 90008 KB Output is correct
21 Correct 265 ms 89924 KB Output is correct
22 Correct 271 ms 89776 KB Output is correct
23 Correct 244 ms 90052 KB Output is correct
24 Correct 251 ms 89128 KB Output is correct
25 Correct 243 ms 89808 KB Output is correct
26 Correct 263 ms 89088 KB Output is correct
27 Correct 9 ms 66128 KB Output is correct
28 Correct 8 ms 66128 KB Output is correct
29 Correct 8 ms 66200 KB Output is correct
30 Correct 8 ms 66128 KB Output is correct
31 Correct 7 ms 65984 KB Output is correct
32 Correct 8 ms 66128 KB Output is correct
33 Correct 8 ms 66128 KB Output is correct
34 Correct 8 ms 66300 KB Output is correct
35 Correct 8 ms 66128 KB Output is correct
36 Correct 13 ms 67408 KB Output is correct
37 Correct 61 ms 77344 KB Output is correct
38 Correct 63 ms 77392 KB Output is correct
39 Correct 61 ms 77264 KB Output is correct
40 Correct 62 ms 77128 KB Output is correct
41 Correct 58 ms 77128 KB Output is correct
42 Correct 66 ms 76360 KB Output is correct
43 Correct 61 ms 77640 KB Output is correct
44 Correct 62 ms 77128 KB Output is correct
45 Correct 57 ms 82584 KB Output is correct
46 Correct 66 ms 77640 KB Output is correct
47 Correct 29 ms 67664 KB Output is correct
48 Correct 181 ms 82092 KB Output is correct
49 Correct 174 ms 81068 KB Output is correct
50 Correct 172 ms 82268 KB Output is correct
51 Correct 179 ms 82104 KB Output is correct
52 Correct 173 ms 81652 KB Output is correct
53 Correct 159 ms 80444 KB Output is correct
54 Correct 199 ms 83036 KB Output is correct
55 Correct 190 ms 82096 KB Output is correct
56 Correct 287 ms 86448 KB Output is correct
57 Correct 213 ms 83164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 66128 KB Output is correct
2 Correct 8 ms 66128 KB Output is correct
3 Correct 7 ms 66072 KB Output is correct
4 Correct 8 ms 66128 KB Output is correct
5 Correct 9 ms 66128 KB Output is correct
6 Correct 8 ms 66128 KB Output is correct
7 Correct 8 ms 66128 KB Output is correct
8 Correct 8 ms 66128 KB Output is correct
9 Correct 7 ms 66128 KB Output is correct
10 Correct 53 ms 75592 KB Output is correct
11 Correct 65 ms 77128 KB Output is correct
12 Correct 71 ms 77048 KB Output is correct
13 Correct 64 ms 77640 KB Output is correct
14 Correct 66 ms 81480 KB Output is correct
15 Correct 57 ms 75848 KB Output is correct
16 Correct 108 ms 81364 KB Output is correct
17 Correct 97 ms 81212 KB Output is correct
18 Correct 107 ms 81744 KB Output is correct
19 Correct 98 ms 85424 KB Output is correct
20 Correct 248 ms 89208 KB Output is correct
21 Correct 248 ms 90008 KB Output is correct
22 Correct 265 ms 89924 KB Output is correct
23 Correct 271 ms 89776 KB Output is correct
24 Correct 244 ms 90052 KB Output is correct
25 Correct 251 ms 89128 KB Output is correct
26 Correct 243 ms 89808 KB Output is correct
27 Correct 263 ms 89088 KB Output is correct
28 Correct 9 ms 66128 KB Output is correct
29 Correct 8 ms 66128 KB Output is correct
30 Correct 8 ms 66200 KB Output is correct
31 Correct 8 ms 66128 KB Output is correct
32 Correct 7 ms 65984 KB Output is correct
33 Correct 8 ms 66128 KB Output is correct
34 Correct 8 ms 66128 KB Output is correct
35 Correct 8 ms 66300 KB Output is correct
36 Correct 8 ms 66128 KB Output is correct
37 Correct 13 ms 67408 KB Output is correct
38 Correct 61 ms 77344 KB Output is correct
39 Correct 63 ms 77392 KB Output is correct
40 Correct 61 ms 77264 KB Output is correct
41 Correct 62 ms 77128 KB Output is correct
42 Correct 58 ms 77128 KB Output is correct
43 Correct 66 ms 76360 KB Output is correct
44 Correct 61 ms 77640 KB Output is correct
45 Correct 62 ms 77128 KB Output is correct
46 Correct 57 ms 82584 KB Output is correct
47 Correct 66 ms 77640 KB Output is correct
48 Correct 29 ms 67664 KB Output is correct
49 Correct 181 ms 82092 KB Output is correct
50 Correct 174 ms 81068 KB Output is correct
51 Correct 172 ms 82268 KB Output is correct
52 Correct 179 ms 82104 KB Output is correct
53 Correct 173 ms 81652 KB Output is correct
54 Correct 159 ms 80444 KB Output is correct
55 Correct 199 ms 83036 KB Output is correct
56 Correct 190 ms 82096 KB Output is correct
57 Correct 287 ms 86448 KB Output is correct
58 Correct 213 ms 83164 KB Output is correct
59 Correct 105 ms 74836 KB Output is correct
60 Correct 185 ms 82376 KB Output is correct
61 Correct 196 ms 82436 KB Output is correct
62 Correct 195 ms 83120 KB Output is correct
63 Correct 307 ms 86960 KB Output is correct
64 Correct 8 ms 66232 KB Output is correct
65 Correct 9 ms 66140 KB Output is correct
66 Correct 8 ms 66128 KB Output is correct
67 Correct 8 ms 66384 KB Output is correct
68 Correct 7 ms 66128 KB Output is correct
69 Correct 8 ms 66128 KB Output is correct
70 Correct 8 ms 66128 KB Output is correct
71 Correct 8 ms 66236 KB Output is correct
72 Correct 7 ms 66128 KB Output is correct
73 Correct 9 ms 66276 KB Output is correct
74 Correct 84 ms 88648 KB Output is correct
75 Correct 60 ms 77388 KB Output is correct
76 Correct 62 ms 77640 KB Output is correct
77 Correct 78 ms 81364 KB Output is correct
78 Correct 71 ms 93256 KB Output is correct
79 Correct 64 ms 87368 KB Output is correct
80 Correct 90 ms 86344 KB Output is correct
81 Correct 97 ms 87628 KB Output is correct
82 Correct 113 ms 88904 KB Output is correct
83 Correct 117 ms 93768 KB Output is correct
84 Correct 60 ms 81992 KB Output is correct
85 Correct 116 ms 91800 KB Output is correct
86 Correct 72 ms 76028 KB Output is correct
87 Correct 174 ms 82352 KB Output is correct
88 Correct 172 ms 82228 KB Output is correct
89 Correct 256 ms 85604 KB Output is correct
90 Correct 186 ms 98740 KB Output is correct
91 Correct 224 ms 96108 KB Output is correct
92 Correct 267 ms 83880 KB Output is correct
93 Correct 238 ms 91916 KB Output is correct
94 Correct 311 ms 93528 KB Output is correct
95 Correct 250 ms 97608 KB Output is correct
96 Correct 303 ms 86700 KB Output is correct
97 Correct 298 ms 90720 KB Output is correct