답안 #972937

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
972937 2024-05-01T10:32:21 Z DAleksa 자매 도시 (APIO20_swap) C++17
100 / 100
470 ms 357604 KB
#include "swap.h"
#include <bits/stdc++.h>

using namespace std;

int tsz;

struct DSU {
    int n;
    vector<int> parent, sz;
    vector<bool> chain;
    vector<vector<int>> comp;
    vector<int> id;
    DSU(int _n) {
        n = _n;
        parent.resize(n);
        sz.resize(n);
        comp.resize(n);
        chain.resize(n);
        id.resize(n);
        for(int i = 0; i < n; i++) {
            parent[i] = i;
            sz[i] = 1;
            chain[i] = true;
            comp[i].push_back(i);
            id[i] = i;
        }
    }
    int find_parent(int u) {
        if(parent[u] == u) return u;
        return parent[u] = find_parent(parent[u]);
    }
    bool check(int u, int v) {
        u = find_parent(u);
        v = find_parent(v);
        return u == v;
    }
    void join(int u, int v) {
        u = find_parent(u);
        v = find_parent(v);
        if(sz[u] > sz[v]) swap(u, v);
        for(int i : comp[u]) comp[v].push_back(i);
        comp[u].clear();
        parent[u] = v;
        sz[v] += sz[u];
        id[v] = tsz;
    }
    void update_chain_state(int u) {
        u = find_parent(u);
        chain[u] = false;
    }
    int get_size(int u) {
        u = find_parent(u);
        return sz[u];
    }
};

const int N = 1e6 + 10, LOG = 20;
bool leaf[N];
DSU dsu(N), tree(2 * N);
int order[2 * N];
vector<int> g[2 * N];
int up[2 * N][LOG + 1];
int root;
int timer;
int tin[2 * N], tout[2 * N];
int ans[2 * N];
bool mark[2 * N];

void dfs(int u) {
    mark[u] = true;
    tin[u] = ++timer;
    for(int v : g[u]) {
        dfs(v);
        up[v][0] = u;
    }
    tout[u] = ++timer;
}

bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
int LCA(int u, int v) {
    if(is_ancestor(u, v)) return u;
    if(is_ancestor(v, u)) return v;
    for(int j = LOG; j >= 0; j--) {
//        cout << up[u][j] << "\n";
        if(up[u][j] == -1) continue;
        if(is_ancestor(up[u][j], v)) continue;
        u = up[u][j];
//        cout << u << " " << j << "\n";
    }
    return up[u][0];
}

void init(int n, int m, vector<int> U, vector<int> V, vector<int> W) {
    for(int i = 0; i < N; i++) {
        leaf[i] = true;
        for(int j = 0; j <= LOG; j++) up[i][j] = -1;
    }
    tsz = n;
    for(int i = 0; i < m; i++) order[i] = i;
    sort(order, order + m, [&](int i, int j) { return W[i] < W[j]; });
    for(int i = 0; i < m; i++) {
        int u = U[order[i]], v = V[order[i]];
        if(dsu.check(u, v)) {
            int par = dsu.find_parent(u);
            if(dsu.chain[par]) {
                for(int j : dsu.comp[par]) {
                    g[tsz].push_back(j);
                    tree.join(tsz, j);
                }
                ans[tsz] = W[order[i]];
                dsu.id[par] = tsz;
                tsz++;
                dsu.update_chain_state(par);
            }
        } else {
            if(!dsu.chain[dsu.find_parent(u)] || !dsu.chain[dsu.find_parent(v)]) {
                if(dsu.chain[dsu.find_parent(u)]) {
                    for(int j : dsu.comp[dsu.find_parent(u)]) {
                        g[tsz].push_back(j);
                        tree.join(tsz, j);
                    }
                } else {
                    g[tsz].push_back(dsu.id[dsu.find_parent(u)]);
                    tree.join(tsz, dsu.id[dsu.find_parent(u)]);
                }
                if(dsu.chain[dsu.find_parent(v)]) {
                    for(int j : dsu.comp[dsu.find_parent(v)]) {
                        g[tsz].push_back(j);
                        tree.join(tsz, j);
                    }
                } else {
                    g[tsz].push_back(dsu.id[dsu.find_parent(v)]);
                    tree.join(tsz, dsu.id[dsu.find_parent(v)]);
                }
                dsu.join(u, v);
                int par = dsu.find_parent(u);
                dsu.update_chain_state(par);
                ans[tsz] = W[order[i]];
                tsz++;
            } else {
                if(leaf[u] && leaf[v]) {
                    if(dsu.get_size(u) > 1) leaf[u] = false;
                    if(dsu.get_size(v) > 1) leaf[v] = false;
                    dsu.join(u, v);
                } else {
                    for(int j : dsu.comp[dsu.find_parent(u)]) {
                        g[tsz].push_back(j);
                        tree.join(tsz, j);
                    }
                    for(int j : dsu.comp[dsu.find_parent(v)]) {
                        g[tsz].push_back(j);
                        tree.join(tsz, j);
                    }
                    dsu.join(u, v);
                    ans[tsz] = W[order[i]];
                    tsz++;
                    dsu.update_chain_state(u);
                }
            }
        }
    }
//    for(int i = 0; i < tsz; i++) {
//        cout << i << ": ";
//        for(int j : g[i]) cout << j << " ";
//        cout << "\n";
//    }
    root = tsz - 1;
    for(int i = root; i >= 0; i--) if(!mark[i]) dfs(i);
//    for(int i = 0; i <= root; i++) {
//        for(int j = 0; j <= 5; j++) cout << up[i][j] << " ";
//        cout << "\n";
//    }
    for(int j = 1; j <= LOG; j++) {
        for(int i = 0; i <= root; i++) {
            if(up[i][j - 1] == -1) up[i][j] = -1;
            else up[i][j] = up[up[i][j - 1]][j - 1];
        }
    }
//    cout << "TU: " << LCA(4, 5) << "\n";
}

int getMinimumFuelCapacity(int X, int Y) {
//    return -1;
    if(!tree.check(X, Y)) return -1;
    return ans[LCA(X, Y)];
}

/*

5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1


3 2
0 1 5
0 2 5
1
1 2


6 7
0 1 1
1 2 2
0 2 3
3 4 4
4 5 5
3 5 6
2 3 7
15
0 1
0 2
0 3
0 4
0 5
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 308 ms 334600 KB Output is correct
2 Correct 195 ms 334676 KB Output is correct
3 Correct 198 ms 334664 KB Output is correct
4 Correct 195 ms 334736 KB Output is correct
5 Correct 199 ms 334644 KB Output is correct
6 Correct 210 ms 335052 KB Output is correct
7 Correct 197 ms 334840 KB Output is correct
8 Correct 193 ms 334696 KB Output is correct
9 Correct 263 ms 341660 KB Output is correct
10 Correct 247 ms 343244 KB Output is correct
11 Correct 249 ms 342852 KB Output is correct
12 Correct 263 ms 343496 KB Output is correct
13 Correct 264 ms 340776 KB Output is correct
14 Correct 243 ms 342092 KB Output is correct
15 Correct 298 ms 347580 KB Output is correct
16 Correct 288 ms 347336 KB Output is correct
17 Correct 300 ms 347760 KB Output is correct
18 Correct 284 ms 345008 KB Output is correct
19 Correct 249 ms 341224 KB Output is correct
20 Correct 311 ms 349392 KB Output is correct
21 Correct 325 ms 349976 KB Output is correct
22 Correct 347 ms 350124 KB Output is correct
23 Correct 303 ms 347520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 308 ms 334600 KB Output is correct
2 Correct 195 ms 334676 KB Output is correct
3 Correct 389 ms 355940 KB Output is correct
4 Correct 401 ms 356612 KB Output is correct
5 Correct 470 ms 357604 KB Output is correct
6 Correct 388 ms 356260 KB Output is correct
7 Correct 416 ms 356400 KB Output is correct
8 Correct 389 ms 355776 KB Output is correct
9 Correct 402 ms 356472 KB Output is correct
10 Correct 436 ms 355580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 308 ms 334600 KB Output is correct
2 Correct 195 ms 334676 KB Output is correct
3 Correct 198 ms 334664 KB Output is correct
4 Correct 195 ms 334736 KB Output is correct
5 Correct 199 ms 334644 KB Output is correct
6 Correct 210 ms 335052 KB Output is correct
7 Correct 197 ms 334840 KB Output is correct
8 Correct 193 ms 334696 KB Output is correct
9 Correct 193 ms 334576 KB Output is correct
10 Correct 198 ms 334676 KB Output is correct
11 Correct 200 ms 334676 KB Output is correct
12 Correct 198 ms 334672 KB Output is correct
13 Correct 212 ms 334856 KB Output is correct
14 Correct 196 ms 334680 KB Output is correct
15 Correct 202 ms 334672 KB Output is correct
16 Correct 198 ms 334680 KB Output is correct
17 Correct 201 ms 334724 KB Output is correct
18 Correct 200 ms 334776 KB Output is correct
19 Correct 196 ms 334676 KB Output is correct
20 Correct 198 ms 334752 KB Output is correct
21 Correct 197 ms 334936 KB Output is correct
22 Correct 199 ms 334672 KB Output is correct
23 Correct 201 ms 334680 KB Output is correct
24 Correct 196 ms 334780 KB Output is correct
25 Correct 199 ms 334928 KB Output is correct
26 Correct 201 ms 334884 KB Output is correct
27 Correct 207 ms 334928 KB Output is correct
28 Correct 205 ms 334940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 193 ms 334576 KB Output is correct
2 Correct 308 ms 334600 KB Output is correct
3 Correct 195 ms 334676 KB Output is correct
4 Correct 198 ms 334664 KB Output is correct
5 Correct 195 ms 334736 KB Output is correct
6 Correct 199 ms 334644 KB Output is correct
7 Correct 210 ms 335052 KB Output is correct
8 Correct 197 ms 334840 KB Output is correct
9 Correct 193 ms 334696 KB Output is correct
10 Correct 263 ms 341660 KB Output is correct
11 Correct 247 ms 343244 KB Output is correct
12 Correct 249 ms 342852 KB Output is correct
13 Correct 263 ms 343496 KB Output is correct
14 Correct 264 ms 340776 KB Output is correct
15 Correct 198 ms 334676 KB Output is correct
16 Correct 200 ms 334676 KB Output is correct
17 Correct 198 ms 334672 KB Output is correct
18 Correct 212 ms 334856 KB Output is correct
19 Correct 196 ms 334680 KB Output is correct
20 Correct 202 ms 334672 KB Output is correct
21 Correct 198 ms 334680 KB Output is correct
22 Correct 201 ms 334724 KB Output is correct
23 Correct 200 ms 334776 KB Output is correct
24 Correct 196 ms 334676 KB Output is correct
25 Correct 198 ms 334752 KB Output is correct
26 Correct 197 ms 334936 KB Output is correct
27 Correct 199 ms 334672 KB Output is correct
28 Correct 201 ms 334680 KB Output is correct
29 Correct 196 ms 334780 KB Output is correct
30 Correct 199 ms 334928 KB Output is correct
31 Correct 201 ms 334884 KB Output is correct
32 Correct 207 ms 334928 KB Output is correct
33 Correct 205 ms 334940 KB Output is correct
34 Correct 211 ms 336404 KB Output is correct
35 Correct 282 ms 346308 KB Output is correct
36 Correct 277 ms 344560 KB Output is correct
37 Correct 273 ms 344836 KB Output is correct
38 Correct 283 ms 344476 KB Output is correct
39 Correct 296 ms 345400 KB Output is correct
40 Correct 290 ms 345296 KB Output is correct
41 Correct 269 ms 344568 KB Output is correct
42 Correct 282 ms 344200 KB Output is correct
43 Correct 339 ms 349764 KB Output is correct
44 Correct 311 ms 347196 KB Output is correct
45 Correct 312 ms 346892 KB Output is correct
46 Correct 289 ms 344396 KB Output is correct
47 Correct 273 ms 343632 KB Output is correct
48 Correct 320 ms 345736 KB Output is correct
49 Correct 264 ms 341348 KB Output is correct
50 Correct 246 ms 340428 KB Output is correct
51 Correct 306 ms 344976 KB Output is correct
52 Correct 306 ms 347076 KB Output is correct
53 Correct 354 ms 349968 KB Output is correct
54 Correct 308 ms 348756 KB Output is correct
55 Correct 317 ms 349364 KB Output is correct
56 Correct 338 ms 349620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 308 ms 334600 KB Output is correct
2 Correct 195 ms 334676 KB Output is correct
3 Correct 198 ms 334664 KB Output is correct
4 Correct 195 ms 334736 KB Output is correct
5 Correct 199 ms 334644 KB Output is correct
6 Correct 210 ms 335052 KB Output is correct
7 Correct 197 ms 334840 KB Output is correct
8 Correct 193 ms 334696 KB Output is correct
9 Correct 263 ms 341660 KB Output is correct
10 Correct 247 ms 343244 KB Output is correct
11 Correct 249 ms 342852 KB Output is correct
12 Correct 263 ms 343496 KB Output is correct
13 Correct 264 ms 340776 KB Output is correct
14 Correct 243 ms 342092 KB Output is correct
15 Correct 298 ms 347580 KB Output is correct
16 Correct 288 ms 347336 KB Output is correct
17 Correct 300 ms 347760 KB Output is correct
18 Correct 284 ms 345008 KB Output is correct
19 Correct 389 ms 355940 KB Output is correct
20 Correct 401 ms 356612 KB Output is correct
21 Correct 470 ms 357604 KB Output is correct
22 Correct 388 ms 356260 KB Output is correct
23 Correct 416 ms 356400 KB Output is correct
24 Correct 389 ms 355776 KB Output is correct
25 Correct 402 ms 356472 KB Output is correct
26 Correct 436 ms 355580 KB Output is correct
27 Correct 198 ms 334676 KB Output is correct
28 Correct 200 ms 334676 KB Output is correct
29 Correct 198 ms 334672 KB Output is correct
30 Correct 212 ms 334856 KB Output is correct
31 Correct 196 ms 334680 KB Output is correct
32 Correct 202 ms 334672 KB Output is correct
33 Correct 198 ms 334680 KB Output is correct
34 Correct 201 ms 334724 KB Output is correct
35 Correct 200 ms 334776 KB Output is correct
36 Correct 211 ms 336404 KB Output is correct
37 Correct 282 ms 346308 KB Output is correct
38 Correct 277 ms 344560 KB Output is correct
39 Correct 273 ms 344836 KB Output is correct
40 Correct 283 ms 344476 KB Output is correct
41 Correct 296 ms 345400 KB Output is correct
42 Correct 290 ms 345296 KB Output is correct
43 Correct 269 ms 344568 KB Output is correct
44 Correct 282 ms 344200 KB Output is correct
45 Correct 339 ms 349764 KB Output is correct
46 Correct 311 ms 347196 KB Output is correct
47 Correct 212 ms 336684 KB Output is correct
48 Correct 318 ms 349444 KB Output is correct
49 Correct 328 ms 349224 KB Output is correct
50 Correct 324 ms 349600 KB Output is correct
51 Correct 331 ms 349880 KB Output is correct
52 Correct 336 ms 349772 KB Output is correct
53 Correct 327 ms 348732 KB Output is correct
54 Correct 316 ms 350192 KB Output is correct
55 Correct 317 ms 349160 KB Output is correct
56 Correct 419 ms 353756 KB Output is correct
57 Correct 378 ms 352276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 193 ms 334576 KB Output is correct
2 Correct 308 ms 334600 KB Output is correct
3 Correct 195 ms 334676 KB Output is correct
4 Correct 198 ms 334664 KB Output is correct
5 Correct 195 ms 334736 KB Output is correct
6 Correct 199 ms 334644 KB Output is correct
7 Correct 210 ms 335052 KB Output is correct
8 Correct 197 ms 334840 KB Output is correct
9 Correct 193 ms 334696 KB Output is correct
10 Correct 263 ms 341660 KB Output is correct
11 Correct 247 ms 343244 KB Output is correct
12 Correct 249 ms 342852 KB Output is correct
13 Correct 263 ms 343496 KB Output is correct
14 Correct 264 ms 340776 KB Output is correct
15 Correct 243 ms 342092 KB Output is correct
16 Correct 298 ms 347580 KB Output is correct
17 Correct 288 ms 347336 KB Output is correct
18 Correct 300 ms 347760 KB Output is correct
19 Correct 284 ms 345008 KB Output is correct
20 Correct 389 ms 355940 KB Output is correct
21 Correct 401 ms 356612 KB Output is correct
22 Correct 470 ms 357604 KB Output is correct
23 Correct 388 ms 356260 KB Output is correct
24 Correct 416 ms 356400 KB Output is correct
25 Correct 389 ms 355776 KB Output is correct
26 Correct 402 ms 356472 KB Output is correct
27 Correct 436 ms 355580 KB Output is correct
28 Correct 198 ms 334676 KB Output is correct
29 Correct 200 ms 334676 KB Output is correct
30 Correct 198 ms 334672 KB Output is correct
31 Correct 212 ms 334856 KB Output is correct
32 Correct 196 ms 334680 KB Output is correct
33 Correct 202 ms 334672 KB Output is correct
34 Correct 198 ms 334680 KB Output is correct
35 Correct 201 ms 334724 KB Output is correct
36 Correct 200 ms 334776 KB Output is correct
37 Correct 211 ms 336404 KB Output is correct
38 Correct 282 ms 346308 KB Output is correct
39 Correct 277 ms 344560 KB Output is correct
40 Correct 273 ms 344836 KB Output is correct
41 Correct 283 ms 344476 KB Output is correct
42 Correct 296 ms 345400 KB Output is correct
43 Correct 290 ms 345296 KB Output is correct
44 Correct 269 ms 344568 KB Output is correct
45 Correct 282 ms 344200 KB Output is correct
46 Correct 339 ms 349764 KB Output is correct
47 Correct 311 ms 347196 KB Output is correct
48 Correct 212 ms 336684 KB Output is correct
49 Correct 318 ms 349444 KB Output is correct
50 Correct 328 ms 349224 KB Output is correct
51 Correct 324 ms 349600 KB Output is correct
52 Correct 331 ms 349880 KB Output is correct
53 Correct 336 ms 349772 KB Output is correct
54 Correct 327 ms 348732 KB Output is correct
55 Correct 316 ms 350192 KB Output is correct
56 Correct 317 ms 349160 KB Output is correct
57 Correct 419 ms 353756 KB Output is correct
58 Correct 378 ms 352276 KB Output is correct
59 Correct 249 ms 341224 KB Output is correct
60 Correct 311 ms 349392 KB Output is correct
61 Correct 325 ms 349976 KB Output is correct
62 Correct 347 ms 350124 KB Output is correct
63 Correct 303 ms 347520 KB Output is correct
64 Correct 196 ms 334676 KB Output is correct
65 Correct 198 ms 334752 KB Output is correct
66 Correct 197 ms 334936 KB Output is correct
67 Correct 199 ms 334672 KB Output is correct
68 Correct 201 ms 334680 KB Output is correct
69 Correct 196 ms 334780 KB Output is correct
70 Correct 199 ms 334928 KB Output is correct
71 Correct 201 ms 334884 KB Output is correct
72 Correct 207 ms 334928 KB Output is correct
73 Correct 205 ms 334940 KB Output is correct
74 Correct 312 ms 346892 KB Output is correct
75 Correct 289 ms 344396 KB Output is correct
76 Correct 273 ms 343632 KB Output is correct
77 Correct 320 ms 345736 KB Output is correct
78 Correct 264 ms 341348 KB Output is correct
79 Correct 246 ms 340428 KB Output is correct
80 Correct 306 ms 344976 KB Output is correct
81 Correct 306 ms 347076 KB Output is correct
82 Correct 354 ms 349968 KB Output is correct
83 Correct 308 ms 348756 KB Output is correct
84 Correct 317 ms 349364 KB Output is correct
85 Correct 338 ms 349620 KB Output is correct
86 Correct 247 ms 340228 KB Output is correct
87 Correct 317 ms 349100 KB Output is correct
88 Correct 340 ms 349232 KB Output is correct
89 Correct 390 ms 350304 KB Output is correct
90 Correct 308 ms 345516 KB Output is correct
91 Correct 318 ms 346432 KB Output is correct
92 Correct 361 ms 350124 KB Output is correct
93 Correct 349 ms 351364 KB Output is correct
94 Correct 430 ms 354084 KB Output is correct
95 Correct 349 ms 352968 KB Output is correct
96 Correct 421 ms 354256 KB Output is correct
97 Correct 403 ms 352860 KB Output is correct