답안 #467356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
467356 2021-08-22T17:54:25 Z SirCovidThe19th 자매 도시 (APIO20_swap) C++17
100 / 100
446 ms 66628 KB
#include <bits/stdc++.h>
using namespace std; 

const int mx = 3e5 + 5;

int node, par[mx], up[mx][19], vals[mx], deg[mx], dep[mx]; 
bool ok[mx][19], cyc[mx], deg3[mx]; vector<int> adj[mx];

int get(int i){
    return i == par[i] ? i : par[i] = get(par[i]);
}
void merge(int A, int B){
    int a = get(A), b = get(B); deg[A]++; deg[B]++;
    deg3[node] = deg[A] == 3 or deg[B] == 3 or deg3[a] or deg3[b];
    cyc[node] = a == b or cyc[a] or cyc[b];
    adj[node].push_back(a); if (a != b) adj[node].push_back(b);
    par[a] = par[b] = node++;
}
void dfs(int i){
    for (int l = 1; l < 19; l++){
        up[i][l] = up[up[i][l - 1]][l - 1];
        ok[i][l] = ok[up[i][l - 1]][l - 1] or ok[i][l - 1];
    }
    for (auto x : adj[i]){
        up[x][0] = i; ok[x][0] = cyc[i] or deg3[i];
        dep[x] = dep[i] + 1; dfs(x);
    }
}
void init(int n, int m, vector<int> u, vector<int> v, vector<int> w){
    int idx[m]; node = n + 1; iota(idx, idx + m, 0); iota(par, par + n + m + 1, 0); 
    sort(idx, idx + m, [&](int a, int b){ return w[a] < w[b]; });
    for (int i : idx){
        vals[node] = w[i]; 
        merge(u[i] + 1, v[i] + 1);
    }
    for (int i = n + m; i > 0; i--)
        if (!up[i][0]) dfs(i);
}
int getMinimumFuelCapacity(int x, int y){
    x++; y++; if (dep[x] < dep[y]) swap(x, y);
    for (int i = 18; i >= 0; i--)
        if (((dep[x] - dep[y]) >> i) & 1) x = up[x][i];
    auto good = [&](){ return (cyc[x] or deg3[x]) and (cyc[y] or deg3[y]); };
    for (int i = 18; i >= 0; i--)
        if (up[x][i] != up[y][i] or (!ok[x][i] and !ok[y][i] and !good())) 
            x = up[x][i], y = up[y][i];
    int L = (x != y or !good()) ? up[x][0] : x;
    return (!L) ? -1 : vals[L];
}



# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7500 KB Output is correct
5 Correct 5 ms 7628 KB Output is correct
6 Correct 5 ms 7628 KB Output is correct
7 Correct 7 ms 7628 KB Output is correct
8 Correct 5 ms 7628 KB Output is correct
9 Correct 115 ms 28496 KB Output is correct
10 Correct 166 ms 33220 KB Output is correct
11 Correct 144 ms 32832 KB Output is correct
12 Correct 155 ms 34384 KB Output is correct
13 Correct 146 ms 38220 KB Output is correct
14 Correct 126 ms 28616 KB Output is correct
15 Correct 295 ms 35276 KB Output is correct
16 Correct 305 ms 34524 KB Output is correct
17 Correct 283 ms 36088 KB Output is correct
18 Correct 379 ms 40064 KB Output is correct
19 Correct 124 ms 15064 KB Output is correct
20 Correct 310 ms 36508 KB Output is correct
21 Correct 288 ms 35716 KB Output is correct
22 Correct 321 ms 37668 KB Output is correct
23 Correct 403 ms 41408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 367 ms 43356 KB Output is correct
4 Correct 362 ms 48852 KB Output is correct
5 Correct 380 ms 47964 KB Output is correct
6 Correct 346 ms 48548 KB Output is correct
7 Correct 370 ms 48456 KB Output is correct
8 Correct 321 ms 46916 KB Output is correct
9 Correct 314 ms 48100 KB Output is correct
10 Correct 350 ms 46608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7500 KB Output is correct
5 Correct 5 ms 7628 KB Output is correct
6 Correct 5 ms 7628 KB Output is correct
7 Correct 7 ms 7628 KB Output is correct
8 Correct 5 ms 7628 KB Output is correct
9 Correct 5 ms 7396 KB Output is correct
10 Correct 6 ms 7628 KB Output is correct
11 Correct 5 ms 7620 KB Output is correct
12 Correct 6 ms 7628 KB Output is correct
13 Correct 7 ms 7500 KB Output is correct
14 Correct 5 ms 7628 KB Output is correct
15 Correct 5 ms 7628 KB Output is correct
16 Correct 5 ms 7620 KB Output is correct
17 Correct 5 ms 7620 KB Output is correct
18 Correct 5 ms 7628 KB Output is correct
19 Correct 5 ms 7616 KB Output is correct
20 Correct 6 ms 7620 KB Output is correct
21 Correct 5 ms 7628 KB Output is correct
22 Correct 6 ms 7748 KB Output is correct
23 Correct 5 ms 7628 KB Output is correct
24 Correct 7 ms 7884 KB Output is correct
25 Correct 6 ms 7884 KB Output is correct
26 Correct 6 ms 7884 KB Output is correct
27 Correct 5 ms 7616 KB Output is correct
28 Correct 6 ms 7916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7396 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 5 ms 7500 KB Output is correct
6 Correct 5 ms 7628 KB Output is correct
7 Correct 5 ms 7628 KB Output is correct
8 Correct 7 ms 7628 KB Output is correct
9 Correct 5 ms 7628 KB Output is correct
10 Correct 115 ms 28496 KB Output is correct
11 Correct 166 ms 33220 KB Output is correct
12 Correct 144 ms 32832 KB Output is correct
13 Correct 155 ms 34384 KB Output is correct
14 Correct 146 ms 38220 KB Output is correct
15 Correct 6 ms 7628 KB Output is correct
16 Correct 5 ms 7620 KB Output is correct
17 Correct 6 ms 7628 KB Output is correct
18 Correct 7 ms 7500 KB Output is correct
19 Correct 5 ms 7628 KB Output is correct
20 Correct 5 ms 7628 KB Output is correct
21 Correct 5 ms 7620 KB Output is correct
22 Correct 5 ms 7620 KB Output is correct
23 Correct 5 ms 7628 KB Output is correct
24 Correct 5 ms 7616 KB Output is correct
25 Correct 6 ms 7620 KB Output is correct
26 Correct 5 ms 7628 KB Output is correct
27 Correct 6 ms 7748 KB Output is correct
28 Correct 5 ms 7628 KB Output is correct
29 Correct 7 ms 7884 KB Output is correct
30 Correct 6 ms 7884 KB Output is correct
31 Correct 6 ms 7884 KB Output is correct
32 Correct 5 ms 7616 KB Output is correct
33 Correct 6 ms 7916 KB Output is correct
34 Correct 19 ms 11340 KB Output is correct
35 Correct 150 ms 36148 KB Output is correct
36 Correct 152 ms 36156 KB Output is correct
37 Correct 146 ms 36244 KB Output is correct
38 Correct 147 ms 35712 KB Output is correct
39 Correct 145 ms 35628 KB Output is correct
40 Correct 136 ms 33352 KB Output is correct
41 Correct 151 ms 36416 KB Output is correct
42 Correct 152 ms 36164 KB Output is correct
43 Correct 142 ms 41272 KB Output is correct
44 Correct 152 ms 36540 KB Output is correct
45 Correct 220 ms 50168 KB Output is correct
46 Correct 147 ms 36160 KB Output is correct
47 Correct 160 ms 36276 KB Output is correct
48 Correct 198 ms 40900 KB Output is correct
49 Correct 141 ms 48612 KB Output is correct
50 Correct 115 ms 39352 KB Output is correct
51 Correct 169 ms 44556 KB Output is correct
52 Correct 213 ms 52676 KB Output is correct
53 Correct 246 ms 55684 KB Output is correct
54 Correct 255 ms 62700 KB Output is correct
55 Correct 149 ms 40896 KB Output is correct
56 Correct 248 ms 57796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7372 KB Output is correct
2 Correct 4 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 5 ms 7500 KB Output is correct
5 Correct 5 ms 7628 KB Output is correct
6 Correct 5 ms 7628 KB Output is correct
7 Correct 7 ms 7628 KB Output is correct
8 Correct 5 ms 7628 KB Output is correct
9 Correct 115 ms 28496 KB Output is correct
10 Correct 166 ms 33220 KB Output is correct
11 Correct 144 ms 32832 KB Output is correct
12 Correct 155 ms 34384 KB Output is correct
13 Correct 146 ms 38220 KB Output is correct
14 Correct 126 ms 28616 KB Output is correct
15 Correct 295 ms 35276 KB Output is correct
16 Correct 305 ms 34524 KB Output is correct
17 Correct 283 ms 36088 KB Output is correct
18 Correct 379 ms 40064 KB Output is correct
19 Correct 367 ms 43356 KB Output is correct
20 Correct 362 ms 48852 KB Output is correct
21 Correct 380 ms 47964 KB Output is correct
22 Correct 346 ms 48548 KB Output is correct
23 Correct 370 ms 48456 KB Output is correct
24 Correct 321 ms 46916 KB Output is correct
25 Correct 314 ms 48100 KB Output is correct
26 Correct 350 ms 46608 KB Output is correct
27 Correct 6 ms 7628 KB Output is correct
28 Correct 5 ms 7620 KB Output is correct
29 Correct 6 ms 7628 KB Output is correct
30 Correct 7 ms 7500 KB Output is correct
31 Correct 5 ms 7628 KB Output is correct
32 Correct 5 ms 7628 KB Output is correct
33 Correct 5 ms 7620 KB Output is correct
34 Correct 5 ms 7620 KB Output is correct
35 Correct 5 ms 7628 KB Output is correct
36 Correct 19 ms 11340 KB Output is correct
37 Correct 150 ms 36148 KB Output is correct
38 Correct 152 ms 36156 KB Output is correct
39 Correct 146 ms 36244 KB Output is correct
40 Correct 147 ms 35712 KB Output is correct
41 Correct 145 ms 35628 KB Output is correct
42 Correct 136 ms 33352 KB Output is correct
43 Correct 151 ms 36416 KB Output is correct
44 Correct 152 ms 36164 KB Output is correct
45 Correct 142 ms 41272 KB Output is correct
46 Correct 152 ms 36540 KB Output is correct
47 Correct 30 ms 11444 KB Output is correct
48 Correct 298 ms 41000 KB Output is correct
49 Correct 290 ms 40996 KB Output is correct
50 Correct 278 ms 41128 KB Output is correct
51 Correct 292 ms 40872 KB Output is correct
52 Correct 300 ms 39208 KB Output is correct
53 Correct 231 ms 32588 KB Output is correct
54 Correct 294 ms 41908 KB Output is correct
55 Correct 293 ms 41128 KB Output is correct
56 Correct 446 ms 45160 KB Output is correct
57 Correct 293 ms 42152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7396 KB Output is correct
2 Correct 5 ms 7372 KB Output is correct
3 Correct 4 ms 7372 KB Output is correct
4 Correct 4 ms 7372 KB Output is correct
5 Correct 5 ms 7500 KB Output is correct
6 Correct 5 ms 7628 KB Output is correct
7 Correct 5 ms 7628 KB Output is correct
8 Correct 7 ms 7628 KB Output is correct
9 Correct 5 ms 7628 KB Output is correct
10 Correct 115 ms 28496 KB Output is correct
11 Correct 166 ms 33220 KB Output is correct
12 Correct 144 ms 32832 KB Output is correct
13 Correct 155 ms 34384 KB Output is correct
14 Correct 146 ms 38220 KB Output is correct
15 Correct 126 ms 28616 KB Output is correct
16 Correct 295 ms 35276 KB Output is correct
17 Correct 305 ms 34524 KB Output is correct
18 Correct 283 ms 36088 KB Output is correct
19 Correct 379 ms 40064 KB Output is correct
20 Correct 367 ms 43356 KB Output is correct
21 Correct 362 ms 48852 KB Output is correct
22 Correct 380 ms 47964 KB Output is correct
23 Correct 346 ms 48548 KB Output is correct
24 Correct 370 ms 48456 KB Output is correct
25 Correct 321 ms 46916 KB Output is correct
26 Correct 314 ms 48100 KB Output is correct
27 Correct 350 ms 46608 KB Output is correct
28 Correct 6 ms 7628 KB Output is correct
29 Correct 5 ms 7620 KB Output is correct
30 Correct 6 ms 7628 KB Output is correct
31 Correct 7 ms 7500 KB Output is correct
32 Correct 5 ms 7628 KB Output is correct
33 Correct 5 ms 7628 KB Output is correct
34 Correct 5 ms 7620 KB Output is correct
35 Correct 5 ms 7620 KB Output is correct
36 Correct 5 ms 7628 KB Output is correct
37 Correct 19 ms 11340 KB Output is correct
38 Correct 150 ms 36148 KB Output is correct
39 Correct 152 ms 36156 KB Output is correct
40 Correct 146 ms 36244 KB Output is correct
41 Correct 147 ms 35712 KB Output is correct
42 Correct 145 ms 35628 KB Output is correct
43 Correct 136 ms 33352 KB Output is correct
44 Correct 151 ms 36416 KB Output is correct
45 Correct 152 ms 36164 KB Output is correct
46 Correct 142 ms 41272 KB Output is correct
47 Correct 152 ms 36540 KB Output is correct
48 Correct 30 ms 11444 KB Output is correct
49 Correct 298 ms 41000 KB Output is correct
50 Correct 290 ms 40996 KB Output is correct
51 Correct 278 ms 41128 KB Output is correct
52 Correct 292 ms 40872 KB Output is correct
53 Correct 300 ms 39208 KB Output is correct
54 Correct 231 ms 32588 KB Output is correct
55 Correct 294 ms 41908 KB Output is correct
56 Correct 293 ms 41128 KB Output is correct
57 Correct 446 ms 45160 KB Output is correct
58 Correct 293 ms 42152 KB Output is correct
59 Correct 124 ms 15064 KB Output is correct
60 Correct 310 ms 36508 KB Output is correct
61 Correct 288 ms 35716 KB Output is correct
62 Correct 321 ms 37668 KB Output is correct
63 Correct 403 ms 41408 KB Output is correct
64 Correct 5 ms 7616 KB Output is correct
65 Correct 6 ms 7620 KB Output is correct
66 Correct 5 ms 7628 KB Output is correct
67 Correct 6 ms 7748 KB Output is correct
68 Correct 5 ms 7628 KB Output is correct
69 Correct 7 ms 7884 KB Output is correct
70 Correct 6 ms 7884 KB Output is correct
71 Correct 6 ms 7884 KB Output is correct
72 Correct 5 ms 7616 KB Output is correct
73 Correct 6 ms 7916 KB Output is correct
74 Correct 220 ms 50168 KB Output is correct
75 Correct 147 ms 36160 KB Output is correct
76 Correct 160 ms 36276 KB Output is correct
77 Correct 198 ms 40900 KB Output is correct
78 Correct 141 ms 48612 KB Output is correct
79 Correct 115 ms 39352 KB Output is correct
80 Correct 169 ms 44556 KB Output is correct
81 Correct 213 ms 52676 KB Output is correct
82 Correct 246 ms 55684 KB Output is correct
83 Correct 255 ms 62700 KB Output is correct
84 Correct 149 ms 40896 KB Output is correct
85 Correct 248 ms 57796 KB Output is correct
86 Correct 100 ms 23120 KB Output is correct
87 Correct 270 ms 41076 KB Output is correct
88 Correct 264 ms 41132 KB Output is correct
89 Correct 333 ms 44324 KB Output is correct
90 Correct 246 ms 54944 KB Output is correct
91 Correct 263 ms 51496 KB Output is correct
92 Correct 367 ms 48144 KB Output is correct
93 Correct 349 ms 56704 KB Output is correct
94 Correct 418 ms 60196 KB Output is correct
95 Correct 389 ms 66628 KB Output is correct
96 Correct 364 ms 45452 KB Output is correct
97 Correct 398 ms 52996 KB Output is correct