답안 #297231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
297231 2020-09-11T11:54:35 Z PeppaPig 자매 도시 (APIO20_swap) C++14
100 / 100
623 ms 61876 KB
#include "swap.h"
#include <bits/stdc++.h>

#define iii tuple<int, int, int>

using namespace std;

const int N = 3e5 + 5;

int dsu[N];

int find(int x) { return dsu[x] = x == dsu[x] ? x : find(dsu[x]); }

int n, m, ptr;
int cost[N], deg[N], has_3[N], has_cycle[N], par[N][20], dep[N];
vector<int> g[N];

void onion(int a, int b) {
    has_3[ptr] = has_3[find(a)] | has_3[find(b)]; 
    has_cycle[ptr] = has_cycle[find(a)] | has_cycle[find(b)]; 

    if(deg[a] >= 3 || deg[b] >= 3) has_3[ptr] = 1;

    g[ptr].emplace_back(find(a));
    if(find(a) == find(b)) has_cycle[ptr] = 1;
    else g[ptr].emplace_back(find(b));

    dsu[find(a)] = dsu[find(b)] = ptr;
}

void dfs(int u, int p) {
    dep[u] = dep[p] + 1, par[u][0] = p;
    for(int i = 1; i <= 19; i++) par[u][i] = par[par[u][i - 1]][i - 1];
    for(int v : g[u]) {
        dfs(v, u);
        has_3[u] |= has_3[v], has_cycle[u] |= has_cycle[v];
    }
}

int lca(int a, int b) {
    if(dep[a] < dep[b]) swap(a, b);
    for(int i = 19; ~i; i--) if(dep[par[a][i]] >= dep[b]) a = par[a][i];
    if(a == b) return a;
    for(int i = 19; ~i; i--) if(par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
    return par[a][0];
}

void init(int _n, int _m, vector<int> U, vector<int> V, vector<int> W) {
    n = ptr = _n, m = _m;
    iota(dsu, dsu + N, 0);

    vector<iii> E;
    for(int i = 0; i < m; i++) E.emplace_back(W[i], ++U[i], ++V[i]);

    sort(E.begin(), E.end());
    for(int i = 0; i < m; i++) {
        int u, v, w; tie(w, u, v) = E[i];
        cost[++ptr] = w, ++deg[u], ++deg[v];
        onion(u, v);
    }
    cost[0] = -1, has_3[0] = has_cycle[0] = 1;
    dfs(ptr, 0);
}

int getMinimumFuelCapacity(int a, int b) {
    int l = lca(++a, ++b);

    if(has_3[l] || has_cycle[l]) return cost[l];
    for(int i = 19; ~i; i--) if(!has_3[par[l][i]] && !has_cycle[par[l][i]])
        l = par[l][i];
    return cost[par[l][0]];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8576 KB Output is correct
2 Correct 6 ms 8576 KB Output is correct
3 Correct 6 ms 8576 KB Output is correct
4 Correct 6 ms 8704 KB Output is correct
5 Correct 8 ms 8832 KB Output is correct
6 Correct 9 ms 8960 KB Output is correct
7 Correct 10 ms 8832 KB Output is correct
8 Correct 7 ms 8832 KB Output is correct
9 Correct 131 ms 29548 KB Output is correct
10 Correct 162 ms 34284 KB Output is correct
11 Correct 176 ms 34028 KB Output is correct
12 Correct 165 ms 35412 KB Output is correct
13 Correct 163 ms 37740 KB Output is correct
14 Correct 168 ms 29936 KB Output is correct
15 Correct 383 ms 38496 KB Output is correct
16 Correct 435 ms 37612 KB Output is correct
17 Correct 380 ms 39208 KB Output is correct
18 Correct 623 ms 41636 KB Output is correct
19 Correct 143 ms 17816 KB Output is correct
20 Correct 397 ms 38592 KB Output is correct
21 Correct 375 ms 37956 KB Output is correct
22 Correct 421 ms 39628 KB Output is correct
23 Correct 603 ms 41968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8576 KB Output is correct
2 Correct 6 ms 8576 KB Output is correct
3 Correct 461 ms 42032 KB Output is correct
4 Correct 447 ms 43500 KB Output is correct
5 Correct 482 ms 42940 KB Output is correct
6 Correct 413 ms 43352 KB Output is correct
7 Correct 468 ms 43240 KB Output is correct
8 Correct 453 ms 42008 KB Output is correct
9 Correct 453 ms 42808 KB Output is correct
10 Correct 449 ms 41748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8576 KB Output is correct
2 Correct 6 ms 8576 KB Output is correct
3 Correct 6 ms 8576 KB Output is correct
4 Correct 6 ms 8576 KB Output is correct
5 Correct 6 ms 8704 KB Output is correct
6 Correct 8 ms 8832 KB Output is correct
7 Correct 9 ms 8960 KB Output is correct
8 Correct 10 ms 8832 KB Output is correct
9 Correct 7 ms 8832 KB Output is correct
10 Correct 7 ms 8864 KB Output is correct
11 Correct 7 ms 8832 KB Output is correct
12 Correct 7 ms 8832 KB Output is correct
13 Correct 7 ms 8832 KB Output is correct
14 Correct 7 ms 8832 KB Output is correct
15 Correct 7 ms 8832 KB Output is correct
16 Correct 7 ms 8832 KB Output is correct
17 Correct 8 ms 8832 KB Output is correct
18 Correct 8 ms 8960 KB Output is correct
19 Correct 7 ms 8832 KB Output is correct
20 Correct 7 ms 8832 KB Output is correct
21 Correct 8 ms 8832 KB Output is correct
22 Correct 8 ms 8960 KB Output is correct
23 Correct 7 ms 8832 KB Output is correct
24 Correct 9 ms 8992 KB Output is correct
25 Correct 8 ms 8960 KB Output is correct
26 Correct 8 ms 9088 KB Output is correct
27 Correct 7 ms 8832 KB Output is correct
28 Correct 8 ms 8992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8576 KB Output is correct
2 Correct 6 ms 8576 KB Output is correct
3 Correct 6 ms 8576 KB Output is correct
4 Correct 6 ms 8576 KB Output is correct
5 Correct 6 ms 8704 KB Output is correct
6 Correct 8 ms 8832 KB Output is correct
7 Correct 9 ms 8960 KB Output is correct
8 Correct 10 ms 8832 KB Output is correct
9 Correct 7 ms 8832 KB Output is correct
10 Correct 131 ms 29548 KB Output is correct
11 Correct 162 ms 34284 KB Output is correct
12 Correct 176 ms 34028 KB Output is correct
13 Correct 165 ms 35412 KB Output is correct
14 Correct 163 ms 37740 KB Output is correct
15 Correct 7 ms 8864 KB Output is correct
16 Correct 7 ms 8832 KB Output is correct
17 Correct 7 ms 8832 KB Output is correct
18 Correct 7 ms 8832 KB Output is correct
19 Correct 7 ms 8832 KB Output is correct
20 Correct 7 ms 8832 KB Output is correct
21 Correct 7 ms 8832 KB Output is correct
22 Correct 8 ms 8832 KB Output is correct
23 Correct 8 ms 8960 KB Output is correct
24 Correct 25 ms 12224 KB Output is correct
25 Correct 185 ms 35052 KB Output is correct
26 Correct 160 ms 35076 KB Output is correct
27 Correct 188 ms 35052 KB Output is correct
28 Correct 160 ms 34752 KB Output is correct
29 Correct 166 ms 34540 KB Output is correct
30 Correct 152 ms 32468 KB Output is correct
31 Correct 172 ms 35312 KB Output is correct
32 Correct 167 ms 35052 KB Output is correct
33 Correct 173 ms 38124 KB Output is correct
34 Correct 183 ms 35436 KB Output is correct
35 Correct 7 ms 8832 KB Output is correct
36 Correct 7 ms 8832 KB Output is correct
37 Correct 8 ms 8832 KB Output is correct
38 Correct 8 ms 8960 KB Output is correct
39 Correct 7 ms 8832 KB Output is correct
40 Correct 9 ms 8992 KB Output is correct
41 Correct 8 ms 8960 KB Output is correct
42 Correct 8 ms 9088 KB Output is correct
43 Correct 7 ms 8832 KB Output is correct
44 Correct 8 ms 8992 KB Output is correct
45 Correct 217 ms 46500 KB Output is correct
46 Correct 162 ms 35084 KB Output is correct
47 Correct 167 ms 35220 KB Output is correct
48 Correct 177 ms 38636 KB Output is correct
49 Correct 161 ms 44156 KB Output is correct
50 Correct 138 ms 36292 KB Output is correct
51 Correct 220 ms 41340 KB Output is correct
52 Correct 238 ms 49384 KB Output is correct
53 Correct 269 ms 52388 KB Output is correct
54 Correct 283 ms 57876 KB Output is correct
55 Correct 145 ms 37868 KB Output is correct
56 Correct 269 ms 53220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8576 KB Output is correct
2 Correct 6 ms 8576 KB Output is correct
3 Correct 6 ms 8576 KB Output is correct
4 Correct 6 ms 8704 KB Output is correct
5 Correct 8 ms 8832 KB Output is correct
6 Correct 9 ms 8960 KB Output is correct
7 Correct 10 ms 8832 KB Output is correct
8 Correct 7 ms 8832 KB Output is correct
9 Correct 131 ms 29548 KB Output is correct
10 Correct 162 ms 34284 KB Output is correct
11 Correct 176 ms 34028 KB Output is correct
12 Correct 165 ms 35412 KB Output is correct
13 Correct 163 ms 37740 KB Output is correct
14 Correct 168 ms 29936 KB Output is correct
15 Correct 383 ms 38496 KB Output is correct
16 Correct 435 ms 37612 KB Output is correct
17 Correct 380 ms 39208 KB Output is correct
18 Correct 623 ms 41636 KB Output is correct
19 Correct 461 ms 42032 KB Output is correct
20 Correct 447 ms 43500 KB Output is correct
21 Correct 482 ms 42940 KB Output is correct
22 Correct 413 ms 43352 KB Output is correct
23 Correct 468 ms 43240 KB Output is correct
24 Correct 453 ms 42008 KB Output is correct
25 Correct 453 ms 42808 KB Output is correct
26 Correct 449 ms 41748 KB Output is correct
27 Correct 7 ms 8864 KB Output is correct
28 Correct 7 ms 8832 KB Output is correct
29 Correct 7 ms 8832 KB Output is correct
30 Correct 7 ms 8832 KB Output is correct
31 Correct 7 ms 8832 KB Output is correct
32 Correct 7 ms 8832 KB Output is correct
33 Correct 7 ms 8832 KB Output is correct
34 Correct 8 ms 8832 KB Output is correct
35 Correct 8 ms 8960 KB Output is correct
36 Correct 25 ms 12224 KB Output is correct
37 Correct 185 ms 35052 KB Output is correct
38 Correct 160 ms 35076 KB Output is correct
39 Correct 188 ms 35052 KB Output is correct
40 Correct 160 ms 34752 KB Output is correct
41 Correct 166 ms 34540 KB Output is correct
42 Correct 152 ms 32468 KB Output is correct
43 Correct 172 ms 35312 KB Output is correct
44 Correct 167 ms 35052 KB Output is correct
45 Correct 173 ms 38124 KB Output is correct
46 Correct 183 ms 35436 KB Output is correct
47 Correct 36 ms 12352 KB Output is correct
48 Correct 360 ms 39000 KB Output is correct
49 Correct 368 ms 38896 KB Output is correct
50 Correct 382 ms 38892 KB Output is correct
51 Correct 368 ms 38768 KB Output is correct
52 Correct 364 ms 37228 KB Output is correct
53 Correct 310 ms 31360 KB Output is correct
54 Correct 380 ms 39664 KB Output is correct
55 Correct 423 ms 38920 KB Output is correct
56 Correct 501 ms 41388 KB Output is correct
57 Correct 380 ms 39840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8576 KB Output is correct
2 Correct 6 ms 8576 KB Output is correct
3 Correct 6 ms 8576 KB Output is correct
4 Correct 6 ms 8576 KB Output is correct
5 Correct 6 ms 8704 KB Output is correct
6 Correct 8 ms 8832 KB Output is correct
7 Correct 9 ms 8960 KB Output is correct
8 Correct 10 ms 8832 KB Output is correct
9 Correct 7 ms 8832 KB Output is correct
10 Correct 131 ms 29548 KB Output is correct
11 Correct 162 ms 34284 KB Output is correct
12 Correct 176 ms 34028 KB Output is correct
13 Correct 165 ms 35412 KB Output is correct
14 Correct 163 ms 37740 KB Output is correct
15 Correct 168 ms 29936 KB Output is correct
16 Correct 383 ms 38496 KB Output is correct
17 Correct 435 ms 37612 KB Output is correct
18 Correct 380 ms 39208 KB Output is correct
19 Correct 623 ms 41636 KB Output is correct
20 Correct 461 ms 42032 KB Output is correct
21 Correct 447 ms 43500 KB Output is correct
22 Correct 482 ms 42940 KB Output is correct
23 Correct 413 ms 43352 KB Output is correct
24 Correct 468 ms 43240 KB Output is correct
25 Correct 453 ms 42008 KB Output is correct
26 Correct 453 ms 42808 KB Output is correct
27 Correct 449 ms 41748 KB Output is correct
28 Correct 7 ms 8864 KB Output is correct
29 Correct 7 ms 8832 KB Output is correct
30 Correct 7 ms 8832 KB Output is correct
31 Correct 7 ms 8832 KB Output is correct
32 Correct 7 ms 8832 KB Output is correct
33 Correct 7 ms 8832 KB Output is correct
34 Correct 7 ms 8832 KB Output is correct
35 Correct 8 ms 8832 KB Output is correct
36 Correct 8 ms 8960 KB Output is correct
37 Correct 25 ms 12224 KB Output is correct
38 Correct 185 ms 35052 KB Output is correct
39 Correct 160 ms 35076 KB Output is correct
40 Correct 188 ms 35052 KB Output is correct
41 Correct 160 ms 34752 KB Output is correct
42 Correct 166 ms 34540 KB Output is correct
43 Correct 152 ms 32468 KB Output is correct
44 Correct 172 ms 35312 KB Output is correct
45 Correct 167 ms 35052 KB Output is correct
46 Correct 173 ms 38124 KB Output is correct
47 Correct 183 ms 35436 KB Output is correct
48 Correct 36 ms 12352 KB Output is correct
49 Correct 360 ms 39000 KB Output is correct
50 Correct 368 ms 38896 KB Output is correct
51 Correct 382 ms 38892 KB Output is correct
52 Correct 368 ms 38768 KB Output is correct
53 Correct 364 ms 37228 KB Output is correct
54 Correct 310 ms 31360 KB Output is correct
55 Correct 380 ms 39664 KB Output is correct
56 Correct 423 ms 38920 KB Output is correct
57 Correct 501 ms 41388 KB Output is correct
58 Correct 380 ms 39840 KB Output is correct
59 Correct 143 ms 17816 KB Output is correct
60 Correct 397 ms 38592 KB Output is correct
61 Correct 375 ms 37956 KB Output is correct
62 Correct 421 ms 39628 KB Output is correct
63 Correct 603 ms 41968 KB Output is correct
64 Correct 7 ms 8832 KB Output is correct
65 Correct 7 ms 8832 KB Output is correct
66 Correct 8 ms 8832 KB Output is correct
67 Correct 8 ms 8960 KB Output is correct
68 Correct 7 ms 8832 KB Output is correct
69 Correct 9 ms 8992 KB Output is correct
70 Correct 8 ms 8960 KB Output is correct
71 Correct 8 ms 9088 KB Output is correct
72 Correct 7 ms 8832 KB Output is correct
73 Correct 8 ms 8992 KB Output is correct
74 Correct 217 ms 46500 KB Output is correct
75 Correct 162 ms 35084 KB Output is correct
76 Correct 167 ms 35220 KB Output is correct
77 Correct 177 ms 38636 KB Output is correct
78 Correct 161 ms 44156 KB Output is correct
79 Correct 138 ms 36292 KB Output is correct
80 Correct 220 ms 41340 KB Output is correct
81 Correct 238 ms 49384 KB Output is correct
82 Correct 269 ms 52388 KB Output is correct
83 Correct 283 ms 57876 KB Output is correct
84 Correct 145 ms 37868 KB Output is correct
85 Correct 269 ms 53220 KB Output is correct
86 Correct 132 ms 22640 KB Output is correct
87 Correct 349 ms 38892 KB Output is correct
88 Correct 367 ms 38892 KB Output is correct
89 Correct 476 ms 41068 KB Output is correct
90 Correct 335 ms 49572 KB Output is correct
91 Correct 344 ms 46512 KB Output is correct
92 Correct 525 ms 43788 KB Output is correct
93 Correct 479 ms 52572 KB Output is correct
94 Correct 562 ms 56144 KB Output is correct
95 Correct 513 ms 61876 KB Output is correct
96 Correct 519 ms 41532 KB Output is correct
97 Correct 549 ms 48612 KB Output is correct