답안 #571168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
571168 2022-06-01T11:21:11 Z SlavicG 자매 도시 (APIO20_swap) C++17
100 / 100
794 ms 84376 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()

const int N = 4e5 + 10, K = 25;
vector<int> adj[N], g[N];
int val[N], degree[N], depth[N], jump[N][K], id, n;
bool vis[N], good;


struct DSU {
    vector<int> par;
    DSU() {
        par.assign(N, 0);
        forn(i, N) par[i] = i;
    }
    int get(int a) {
        return par[a] = (par[a] == a ? a : get(par[a]));
    }
    void uni(int a, int b) {
        a = get(a);
        b = get(b);
        par[a] = b;
    }
};
vector<int> component;

void get_component(int u) {
    vis[u] = true;
    component.pb(u);
    for(int v: g[u]) {
        if(!vis[v]) get_component(v);
    }
}

void dfs(int u, int par) {
    jump[u][0] = par;
    for(int v: adj[u]) {
        if(v == par) continue;
        depth[v] = depth[u] + 1;
        dfs(v, u);
    }
}

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

    for(int j = K - 1; j >= 0; --j) {
        if(depth[jump[u][j]] >= depth[v]) {
            u = jump[u][j];
        }
    }
    if(u == v) return u;
    for(int j = K - 1; j >= 0; --j) {
        if(jump[u][j] != jump[v][j]) {
            u = jump[u][j], v = jump[v][j];
        }
    }
    return jump[u][0];
}

void init(int _n, int m, vector<int> U, vector<int> V, vector<int> W) {
    n = _n;
    id = n;

    vector<array<int, 3>> edges;
    DSU d;
    DSU connect;
    for(int i = 0; i < m; ++i) {
        edges.pb({W[i], U[i], V[i]});
    }
    sort(all(edges));
    
    for(auto x: edges) {
        int u = x[1], v = x[2], w = x[0];
        
        g[u].pb(v);
        g[v].pb(u);

        if(d.get(u) == d.get(v)) continue;
        if(d.get(u) == u) swap(u, v);

        if(d.get(u) != u && d.get(v) != v) {
            adj[id].pb(d.get(u));
            adj[id].pb(d.get(v));
            d.uni(u, id);
            d.uni(v, id);
            val[id] = w;
            ++id;
        } else if(d.get(u) != u) {
            component.clear();
            get_component(v);
            for(auto x: component) {
                adj[id].pb(x);
                d.uni(x, id);
            }
            val[id] = w;
            ++id;

            adj[id].pb(d.get(u));
            adj[id].pb(d.get(v));
            d.uni(u, id);
            d.uni(v, id);
            val[id] = w;
            ++id;
        } else {
            ++degree[u];
            ++degree[v];
            if(connect.get(u) == connect.get(v) || degree[u] > 2 || degree[v] > 2) {
                component.clear();
                get_component(u);
                good = true;
                for(auto x: component) {
                    d.uni(x, id);
                    adj[id].pb(x);
                }
                
                val[id] = w;
                ++id;
            } else{
                connect.uni(u, v);
            }

        }
    }
    
    dfs(id - 1, id - 1);
    for(int k = 1; k < K; ++k) {
        for(int i = 0; i < N; ++i) {
            jump[i][k] = id - 1;
        }
    }

    for(int k = 1; k < K; ++k) {
        for(int i = 0; i < N; ++i) {
            jump[i][k] = jump[jump[i][k - 1]][k - 1];
        }
    }
}

int getMinimumFuelCapacity(int u, int v) {
    if(!good) return -1;
    return val[LCA(u, v)];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 61280 KB Output is correct
2 Correct 171 ms 61348 KB Output is correct
3 Correct 169 ms 61436 KB Output is correct
4 Correct 172 ms 61396 KB Output is correct
5 Correct 191 ms 61448 KB Output is correct
6 Correct 217 ms 61444 KB Output is correct
7 Correct 173 ms 61372 KB Output is correct
8 Correct 180 ms 61436 KB Output is correct
9 Correct 220 ms 66912 KB Output is correct
10 Correct 251 ms 68152 KB Output is correct
11 Correct 262 ms 67924 KB Output is correct
12 Correct 259 ms 68432 KB Output is correct
13 Correct 242 ms 68644 KB Output is correct
14 Correct 223 ms 67052 KB Output is correct
15 Correct 305 ms 69768 KB Output is correct
16 Correct 295 ms 69564 KB Output is correct
17 Correct 316 ms 69992 KB Output is correct
18 Correct 278 ms 70168 KB Output is correct
19 Correct 280 ms 64680 KB Output is correct
20 Correct 388 ms 76060 KB Output is correct
21 Correct 441 ms 75588 KB Output is correct
22 Correct 419 ms 76480 KB Output is correct
23 Correct 366 ms 76484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 61280 KB Output is correct
2 Correct 171 ms 61348 KB Output is correct
3 Correct 563 ms 83360 KB Output is correct
4 Correct 570 ms 84376 KB Output is correct
5 Correct 662 ms 83728 KB Output is correct
6 Correct 613 ms 84276 KB Output is correct
7 Correct 599 ms 84032 KB Output is correct
8 Correct 571 ms 83136 KB Output is correct
9 Correct 660 ms 83944 KB Output is correct
10 Correct 615 ms 83016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 61280 KB Output is correct
2 Correct 171 ms 61348 KB Output is correct
3 Correct 169 ms 61436 KB Output is correct
4 Correct 172 ms 61396 KB Output is correct
5 Correct 191 ms 61448 KB Output is correct
6 Correct 217 ms 61444 KB Output is correct
7 Correct 173 ms 61372 KB Output is correct
8 Correct 180 ms 61436 KB Output is correct
9 Correct 185 ms 61380 KB Output is correct
10 Correct 181 ms 61480 KB Output is correct
11 Correct 185 ms 61464 KB Output is correct
12 Correct 180 ms 61456 KB Output is correct
13 Correct 175 ms 61468 KB Output is correct
14 Correct 173 ms 61464 KB Output is correct
15 Correct 170 ms 61472 KB Output is correct
16 Correct 178 ms 61480 KB Output is correct
17 Correct 171 ms 61556 KB Output is correct
18 Correct 175 ms 61480 KB Output is correct
19 Correct 201 ms 61396 KB Output is correct
20 Correct 179 ms 61468 KB Output is correct
21 Correct 201 ms 61516 KB Output is correct
22 Correct 191 ms 61492 KB Output is correct
23 Correct 182 ms 61516 KB Output is correct
24 Correct 190 ms 61504 KB Output is correct
25 Correct 191 ms 61524 KB Output is correct
26 Correct 181 ms 61528 KB Output is correct
27 Correct 182 ms 61596 KB Output is correct
28 Correct 185 ms 61548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 61380 KB Output is correct
2 Correct 197 ms 61280 KB Output is correct
3 Correct 171 ms 61348 KB Output is correct
4 Correct 169 ms 61436 KB Output is correct
5 Correct 172 ms 61396 KB Output is correct
6 Correct 191 ms 61448 KB Output is correct
7 Correct 217 ms 61444 KB Output is correct
8 Correct 173 ms 61372 KB Output is correct
9 Correct 180 ms 61436 KB Output is correct
10 Correct 220 ms 66912 KB Output is correct
11 Correct 251 ms 68152 KB Output is correct
12 Correct 262 ms 67924 KB Output is correct
13 Correct 259 ms 68432 KB Output is correct
14 Correct 242 ms 68644 KB Output is correct
15 Correct 181 ms 61480 KB Output is correct
16 Correct 185 ms 61464 KB Output is correct
17 Correct 180 ms 61456 KB Output is correct
18 Correct 175 ms 61468 KB Output is correct
19 Correct 173 ms 61464 KB Output is correct
20 Correct 170 ms 61472 KB Output is correct
21 Correct 178 ms 61480 KB Output is correct
22 Correct 171 ms 61556 KB Output is correct
23 Correct 175 ms 61480 KB Output is correct
24 Correct 201 ms 61396 KB Output is correct
25 Correct 179 ms 61468 KB Output is correct
26 Correct 201 ms 61516 KB Output is correct
27 Correct 191 ms 61492 KB Output is correct
28 Correct 182 ms 61516 KB Output is correct
29 Correct 190 ms 61504 KB Output is correct
30 Correct 191 ms 61524 KB Output is correct
31 Correct 181 ms 61528 KB Output is correct
32 Correct 182 ms 61596 KB Output is correct
33 Correct 185 ms 61548 KB Output is correct
34 Correct 189 ms 62852 KB Output is correct
35 Correct 268 ms 70080 KB Output is correct
36 Correct 309 ms 69800 KB Output is correct
37 Correct 295 ms 69668 KB Output is correct
38 Correct 260 ms 69684 KB Output is correct
39 Correct 268 ms 70520 KB Output is correct
40 Correct 279 ms 70448 KB Output is correct
41 Correct 296 ms 71456 KB Output is correct
42 Correct 285 ms 72472 KB Output is correct
43 Correct 300 ms 80476 KB Output is correct
44 Correct 343 ms 72180 KB Output is correct
45 Correct 337 ms 74856 KB Output is correct
46 Correct 296 ms 69932 KB Output is correct
47 Correct 292 ms 70052 KB Output is correct
48 Correct 317 ms 74688 KB Output is correct
49 Correct 250 ms 69556 KB Output is correct
50 Correct 232 ms 68096 KB Output is correct
51 Correct 273 ms 72684 KB Output is correct
52 Correct 307 ms 73272 KB Output is correct
53 Correct 392 ms 75640 KB Output is correct
54 Correct 332 ms 76580 KB Output is correct
55 Correct 282 ms 80192 KB Output is correct
56 Correct 388 ms 77500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 197 ms 61280 KB Output is correct
2 Correct 171 ms 61348 KB Output is correct
3 Correct 169 ms 61436 KB Output is correct
4 Correct 172 ms 61396 KB Output is correct
5 Correct 191 ms 61448 KB Output is correct
6 Correct 217 ms 61444 KB Output is correct
7 Correct 173 ms 61372 KB Output is correct
8 Correct 180 ms 61436 KB Output is correct
9 Correct 220 ms 66912 KB Output is correct
10 Correct 251 ms 68152 KB Output is correct
11 Correct 262 ms 67924 KB Output is correct
12 Correct 259 ms 68432 KB Output is correct
13 Correct 242 ms 68644 KB Output is correct
14 Correct 223 ms 67052 KB Output is correct
15 Correct 305 ms 69768 KB Output is correct
16 Correct 295 ms 69564 KB Output is correct
17 Correct 316 ms 69992 KB Output is correct
18 Correct 278 ms 70168 KB Output is correct
19 Correct 563 ms 83360 KB Output is correct
20 Correct 570 ms 84376 KB Output is correct
21 Correct 662 ms 83728 KB Output is correct
22 Correct 613 ms 84276 KB Output is correct
23 Correct 599 ms 84032 KB Output is correct
24 Correct 571 ms 83136 KB Output is correct
25 Correct 660 ms 83944 KB Output is correct
26 Correct 615 ms 83016 KB Output is correct
27 Correct 181 ms 61480 KB Output is correct
28 Correct 185 ms 61464 KB Output is correct
29 Correct 180 ms 61456 KB Output is correct
30 Correct 175 ms 61468 KB Output is correct
31 Correct 173 ms 61464 KB Output is correct
32 Correct 170 ms 61472 KB Output is correct
33 Correct 178 ms 61480 KB Output is correct
34 Correct 171 ms 61556 KB Output is correct
35 Correct 175 ms 61480 KB Output is correct
36 Correct 189 ms 62852 KB Output is correct
37 Correct 268 ms 70080 KB Output is correct
38 Correct 309 ms 69800 KB Output is correct
39 Correct 295 ms 69668 KB Output is correct
40 Correct 260 ms 69684 KB Output is correct
41 Correct 268 ms 70520 KB Output is correct
42 Correct 279 ms 70448 KB Output is correct
43 Correct 296 ms 71456 KB Output is correct
44 Correct 285 ms 72472 KB Output is correct
45 Correct 300 ms 80476 KB Output is correct
46 Correct 343 ms 72180 KB Output is correct
47 Correct 201 ms 62936 KB Output is correct
48 Correct 438 ms 73088 KB Output is correct
49 Correct 399 ms 71212 KB Output is correct
50 Correct 435 ms 71108 KB Output is correct
51 Correct 410 ms 71192 KB Output is correct
52 Correct 439 ms 71416 KB Output is correct
53 Correct 400 ms 70096 KB Output is correct
54 Correct 429 ms 72836 KB Output is correct
55 Correct 476 ms 73496 KB Output is correct
56 Correct 794 ms 81312 KB Output is correct
57 Correct 427 ms 73792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 185 ms 61380 KB Output is correct
2 Correct 197 ms 61280 KB Output is correct
3 Correct 171 ms 61348 KB Output is correct
4 Correct 169 ms 61436 KB Output is correct
5 Correct 172 ms 61396 KB Output is correct
6 Correct 191 ms 61448 KB Output is correct
7 Correct 217 ms 61444 KB Output is correct
8 Correct 173 ms 61372 KB Output is correct
9 Correct 180 ms 61436 KB Output is correct
10 Correct 220 ms 66912 KB Output is correct
11 Correct 251 ms 68152 KB Output is correct
12 Correct 262 ms 67924 KB Output is correct
13 Correct 259 ms 68432 KB Output is correct
14 Correct 242 ms 68644 KB Output is correct
15 Correct 223 ms 67052 KB Output is correct
16 Correct 305 ms 69768 KB Output is correct
17 Correct 295 ms 69564 KB Output is correct
18 Correct 316 ms 69992 KB Output is correct
19 Correct 278 ms 70168 KB Output is correct
20 Correct 563 ms 83360 KB Output is correct
21 Correct 570 ms 84376 KB Output is correct
22 Correct 662 ms 83728 KB Output is correct
23 Correct 613 ms 84276 KB Output is correct
24 Correct 599 ms 84032 KB Output is correct
25 Correct 571 ms 83136 KB Output is correct
26 Correct 660 ms 83944 KB Output is correct
27 Correct 615 ms 83016 KB Output is correct
28 Correct 181 ms 61480 KB Output is correct
29 Correct 185 ms 61464 KB Output is correct
30 Correct 180 ms 61456 KB Output is correct
31 Correct 175 ms 61468 KB Output is correct
32 Correct 173 ms 61464 KB Output is correct
33 Correct 170 ms 61472 KB Output is correct
34 Correct 178 ms 61480 KB Output is correct
35 Correct 171 ms 61556 KB Output is correct
36 Correct 175 ms 61480 KB Output is correct
37 Correct 189 ms 62852 KB Output is correct
38 Correct 268 ms 70080 KB Output is correct
39 Correct 309 ms 69800 KB Output is correct
40 Correct 295 ms 69668 KB Output is correct
41 Correct 260 ms 69684 KB Output is correct
42 Correct 268 ms 70520 KB Output is correct
43 Correct 279 ms 70448 KB Output is correct
44 Correct 296 ms 71456 KB Output is correct
45 Correct 285 ms 72472 KB Output is correct
46 Correct 300 ms 80476 KB Output is correct
47 Correct 343 ms 72180 KB Output is correct
48 Correct 201 ms 62936 KB Output is correct
49 Correct 438 ms 73088 KB Output is correct
50 Correct 399 ms 71212 KB Output is correct
51 Correct 435 ms 71108 KB Output is correct
52 Correct 410 ms 71192 KB Output is correct
53 Correct 439 ms 71416 KB Output is correct
54 Correct 400 ms 70096 KB Output is correct
55 Correct 429 ms 72836 KB Output is correct
56 Correct 476 ms 73496 KB Output is correct
57 Correct 794 ms 81312 KB Output is correct
58 Correct 427 ms 73792 KB Output is correct
59 Correct 280 ms 64680 KB Output is correct
60 Correct 388 ms 76060 KB Output is correct
61 Correct 441 ms 75588 KB Output is correct
62 Correct 419 ms 76480 KB Output is correct
63 Correct 366 ms 76484 KB Output is correct
64 Correct 201 ms 61396 KB Output is correct
65 Correct 179 ms 61468 KB Output is correct
66 Correct 201 ms 61516 KB Output is correct
67 Correct 191 ms 61492 KB Output is correct
68 Correct 182 ms 61516 KB Output is correct
69 Correct 190 ms 61504 KB Output is correct
70 Correct 191 ms 61524 KB Output is correct
71 Correct 181 ms 61528 KB Output is correct
72 Correct 182 ms 61596 KB Output is correct
73 Correct 185 ms 61548 KB Output is correct
74 Correct 337 ms 74856 KB Output is correct
75 Correct 296 ms 69932 KB Output is correct
76 Correct 292 ms 70052 KB Output is correct
77 Correct 317 ms 74688 KB Output is correct
78 Correct 250 ms 69556 KB Output is correct
79 Correct 232 ms 68096 KB Output is correct
80 Correct 273 ms 72684 KB Output is correct
81 Correct 307 ms 73272 KB Output is correct
82 Correct 392 ms 75640 KB Output is correct
83 Correct 332 ms 76580 KB Output is correct
84 Correct 282 ms 80192 KB Output is correct
85 Correct 388 ms 77500 KB Output is correct
86 Correct 266 ms 66652 KB Output is correct
87 Correct 396 ms 71264 KB Output is correct
88 Correct 455 ms 71352 KB Output is correct
89 Correct 551 ms 75872 KB Output is correct
90 Correct 372 ms 71100 KB Output is correct
91 Correct 412 ms 71268 KB Output is correct
92 Correct 462 ms 74060 KB Output is correct
93 Correct 466 ms 74652 KB Output is correct
94 Correct 512 ms 76588 KB Output is correct
95 Correct 435 ms 78156 KB Output is correct
96 Correct 646 ms 81468 KB Output is correct
97 Correct 528 ms 77112 KB Output is correct