답안 #365005

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
365005 2021-02-10T17:52:42 Z tatyam 자매 도시 (APIO20_swap) C++17
100 / 100
456 ms 23660 KB
#include <bits/stdc++.h>
using namespace std;

struct UnionFind{
    vector<vector<pair<int, int>>> data;
    vector<int> flag_time, degree;
    UnionFind(int n = 0): data(n, {{-1, -1}}), flag_time(n, INT_MAX), degree(n){}
    int merged_time(int x) const {
        const auto [time, root] = data[x].back();
        if(root < 0) return INT_MAX;
        return time;
    }
    int root(int t, int x) const { // after t
        if(t < merged_time(x)) return x;
        return root(t, data[x].back().second);
    }
    bool unite(int t, int x, int y){
        bool flag = ++degree[x] >= 3 || ++degree[y] >= 3;
        x = root(t, x);
        y = root(t, y);
        if(x == y){
            if(flag_time[x] == INT_MAX) flag_time[x] = t;
            return 0;
        }
        const int size_x = data[x].back().second, size_y = data[y].back().second;
        if(size_x > size_y) swap(x, y);
        if(flag_time[x] == INT_MAX && (flag || flag_time[y] != INT_MAX)) flag_time[x] = t;
        data[x].emplace_back(t, size_x + size_y);
        data[y].emplace_back(t, x);
        return 1;
    }
} uf;
vector<array<int, 3>> edge;

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W){
    uf = UnionFind(N);
    edge.resize(M);
    for(int i = 0; i < M; i++) edge[i] = {U[i], V[i], W[i]};
    sort(edge.begin(), edge.end(), [&](const auto& a, const auto& b){ return a[2] < b[2]; });
    for(int i = 0; i < M; i++){
        const auto& [U, V, W] = edge[i];
        uf.unite(i + 1, U, V);
    }
}

int getMinimumFuelCapacity(int X, int Y){
    auto check = [&](int t){
        const int x = uf.root(t, X), y = uf.root(t, Y);
        return x == y && uf.flag_time[x] <= t;
    };
    int ng = 0, ok = edge.size();
    if(!check(ok)) return -1;
    while(ok - ng > 1){
        const int cen = (ok + ng) / 2;
        (check(cen) ? ok : ng) = cen;
    }
    return edge[ok - 1][2];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 73 ms 11244 KB Output is correct
10 Correct 90 ms 13656 KB Output is correct
11 Correct 90 ms 13420 KB Output is correct
12 Correct 99 ms 14316 KB Output is correct
13 Correct 75 ms 13268 KB Output is correct
14 Correct 84 ms 11520 KB Output is correct
15 Correct 253 ms 17912 KB Output is correct
16 Correct 205 ms 17564 KB Output is correct
17 Correct 216 ms 18388 KB Output is correct
18 Correct 156 ms 17352 KB Output is correct
19 Correct 168 ms 7660 KB Output is correct
20 Correct 370 ms 19052 KB Output is correct
21 Correct 366 ms 18856 KB Output is correct
22 Correct 397 ms 19732 KB Output is correct
23 Correct 201 ms 18760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 183 ms 17284 KB Output is correct
4 Correct 224 ms 17680 KB Output is correct
5 Correct 202 ms 17916 KB Output is correct
6 Correct 196 ms 17612 KB Output is correct
7 Correct 209 ms 18144 KB Output is correct
8 Correct 192 ms 17296 KB Output is correct
9 Correct 190 ms 17632 KB Output is correct
10 Correct 196 ms 17196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 364 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 2 ms 492 KB Output is correct
25 Correct 2 ms 492 KB Output is correct
26 Correct 2 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 2 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 73 ms 11244 KB Output is correct
11 Correct 90 ms 13656 KB Output is correct
12 Correct 90 ms 13420 KB Output is correct
13 Correct 99 ms 14316 KB Output is correct
14 Correct 75 ms 13268 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 364 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 2 ms 492 KB Output is correct
30 Correct 2 ms 492 KB Output is correct
31 Correct 2 ms 492 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 2 ms 492 KB Output is correct
34 Correct 9 ms 2156 KB Output is correct
35 Correct 92 ms 13932 KB Output is correct
36 Correct 84 ms 13932 KB Output is correct
37 Correct 82 ms 13932 KB Output is correct
38 Correct 80 ms 13676 KB Output is correct
39 Correct 79 ms 13548 KB Output is correct
40 Correct 70 ms 12396 KB Output is correct
41 Correct 89 ms 14208 KB Output is correct
42 Correct 90 ms 13932 KB Output is correct
43 Correct 70 ms 13544 KB Output is correct
44 Correct 83 ms 13932 KB Output is correct
45 Correct 101 ms 14828 KB Output is correct
46 Correct 85 ms 13932 KB Output is correct
47 Correct 84 ms 13804 KB Output is correct
48 Correct 82 ms 14056 KB Output is correct
49 Correct 72 ms 8812 KB Output is correct
50 Correct 61 ms 7532 KB Output is correct
51 Correct 91 ms 12780 KB Output is correct
52 Correct 146 ms 17780 KB Output is correct
53 Correct 141 ms 18540 KB Output is correct
54 Correct 167 ms 19736 KB Output is correct
55 Correct 71 ms 13024 KB Output is correct
56 Correct 125 ms 17900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 73 ms 11244 KB Output is correct
10 Correct 90 ms 13656 KB Output is correct
11 Correct 90 ms 13420 KB Output is correct
12 Correct 99 ms 14316 KB Output is correct
13 Correct 75 ms 13268 KB Output is correct
14 Correct 84 ms 11520 KB Output is correct
15 Correct 253 ms 17912 KB Output is correct
16 Correct 205 ms 17564 KB Output is correct
17 Correct 216 ms 18388 KB Output is correct
18 Correct 156 ms 17352 KB Output is correct
19 Correct 183 ms 17284 KB Output is correct
20 Correct 224 ms 17680 KB Output is correct
21 Correct 202 ms 17916 KB Output is correct
22 Correct 196 ms 17612 KB Output is correct
23 Correct 209 ms 18144 KB Output is correct
24 Correct 192 ms 17296 KB Output is correct
25 Correct 190 ms 17632 KB Output is correct
26 Correct 196 ms 17196 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 9 ms 2156 KB Output is correct
37 Correct 92 ms 13932 KB Output is correct
38 Correct 84 ms 13932 KB Output is correct
39 Correct 82 ms 13932 KB Output is correct
40 Correct 80 ms 13676 KB Output is correct
41 Correct 79 ms 13548 KB Output is correct
42 Correct 70 ms 12396 KB Output is correct
43 Correct 89 ms 14208 KB Output is correct
44 Correct 90 ms 13932 KB Output is correct
45 Correct 70 ms 13544 KB Output is correct
46 Correct 83 ms 13932 KB Output is correct
47 Correct 25 ms 2540 KB Output is correct
48 Correct 377 ms 19156 KB Output is correct
49 Correct 346 ms 18836 KB Output is correct
50 Correct 350 ms 18772 KB Output is correct
51 Correct 318 ms 18912 KB Output is correct
52 Correct 311 ms 17948 KB Output is correct
53 Correct 271 ms 15328 KB Output is correct
54 Correct 341 ms 19796 KB Output is correct
55 Correct 350 ms 18772 KB Output is correct
56 Correct 204 ms 17864 KB Output is correct
57 Correct 308 ms 19540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 73 ms 11244 KB Output is correct
11 Correct 90 ms 13656 KB Output is correct
12 Correct 90 ms 13420 KB Output is correct
13 Correct 99 ms 14316 KB Output is correct
14 Correct 75 ms 13268 KB Output is correct
15 Correct 84 ms 11520 KB Output is correct
16 Correct 253 ms 17912 KB Output is correct
17 Correct 205 ms 17564 KB Output is correct
18 Correct 216 ms 18388 KB Output is correct
19 Correct 156 ms 17352 KB Output is correct
20 Correct 183 ms 17284 KB Output is correct
21 Correct 224 ms 17680 KB Output is correct
22 Correct 202 ms 17916 KB Output is correct
23 Correct 196 ms 17612 KB Output is correct
24 Correct 209 ms 18144 KB Output is correct
25 Correct 192 ms 17296 KB Output is correct
26 Correct 190 ms 17632 KB Output is correct
27 Correct 196 ms 17196 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 1 ms 492 KB Output is correct
35 Correct 1 ms 492 KB Output is correct
36 Correct 1 ms 492 KB Output is correct
37 Correct 9 ms 2156 KB Output is correct
38 Correct 92 ms 13932 KB Output is correct
39 Correct 84 ms 13932 KB Output is correct
40 Correct 82 ms 13932 KB Output is correct
41 Correct 80 ms 13676 KB Output is correct
42 Correct 79 ms 13548 KB Output is correct
43 Correct 70 ms 12396 KB Output is correct
44 Correct 89 ms 14208 KB Output is correct
45 Correct 90 ms 13932 KB Output is correct
46 Correct 70 ms 13544 KB Output is correct
47 Correct 83 ms 13932 KB Output is correct
48 Correct 25 ms 2540 KB Output is correct
49 Correct 377 ms 19156 KB Output is correct
50 Correct 346 ms 18836 KB Output is correct
51 Correct 350 ms 18772 KB Output is correct
52 Correct 318 ms 18912 KB Output is correct
53 Correct 311 ms 17948 KB Output is correct
54 Correct 271 ms 15328 KB Output is correct
55 Correct 341 ms 19796 KB Output is correct
56 Correct 350 ms 18772 KB Output is correct
57 Correct 204 ms 17864 KB Output is correct
58 Correct 308 ms 19540 KB Output is correct
59 Correct 168 ms 7660 KB Output is correct
60 Correct 370 ms 19052 KB Output is correct
61 Correct 366 ms 18856 KB Output is correct
62 Correct 397 ms 19732 KB Output is correct
63 Correct 201 ms 18760 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 1 ms 492 KB Output is correct
66 Correct 1 ms 492 KB Output is correct
67 Correct 1 ms 364 KB Output is correct
68 Correct 1 ms 364 KB Output is correct
69 Correct 2 ms 492 KB Output is correct
70 Correct 2 ms 492 KB Output is correct
71 Correct 2 ms 492 KB Output is correct
72 Correct 1 ms 492 KB Output is correct
73 Correct 2 ms 492 KB Output is correct
74 Correct 101 ms 14828 KB Output is correct
75 Correct 85 ms 13932 KB Output is correct
76 Correct 84 ms 13804 KB Output is correct
77 Correct 82 ms 14056 KB Output is correct
78 Correct 72 ms 8812 KB Output is correct
79 Correct 61 ms 7532 KB Output is correct
80 Correct 91 ms 12780 KB Output is correct
81 Correct 146 ms 17780 KB Output is correct
82 Correct 141 ms 18540 KB Output is correct
83 Correct 167 ms 19736 KB Output is correct
84 Correct 71 ms 13024 KB Output is correct
85 Correct 125 ms 17900 KB Output is correct
86 Correct 73 ms 6816 KB Output is correct
87 Correct 349 ms 19028 KB Output is correct
88 Correct 375 ms 18876 KB Output is correct
89 Correct 249 ms 18056 KB Output is correct
90 Correct 251 ms 13256 KB Output is correct
91 Correct 229 ms 13716 KB Output is correct
92 Correct 252 ms 17612 KB Output is correct
93 Correct 422 ms 22440 KB Output is correct
94 Correct 383 ms 23376 KB Output is correct
95 Correct 456 ms 23660 KB Output is correct
96 Correct 206 ms 18120 KB Output is correct
97 Correct 291 ms 21036 KB Output is correct