답안 #645234

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
645234 2022-09-26T13:24:02 Z dooompy 자매 도시 (APIO20_swap) C++17
100 / 100
365 ms 52256 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

int par[300005];
int cost[300005];

vector<int> adj[300005];
int deg[300005];

int findset(int i) {
    return (par[i] == i ? i : par[i] = findset(par[i]));
}

void onion(int a, int b, int idx) {
    par[a] = idx, par[b] = idx;
    adj[idx].push_back(a);
    adj[idx].push_back(b);
}

int up[300005][20];
int depth[300005];

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    vector<pair<int, pair<int, int>>> edges;

    fill(cost, cost+300005, INT_MAX);
    fill(&up[0][0], &up[0][0] + sizeof(up) / sizeof(up[0][0]), -1);

    for (int i = 1; i < 300005; i++) par[i] = i;

    for (int i = 0; i < M; i++) {
        edges.push_back({W[i], {U[i], V[i]}});
    }

    sort(edges.begin(), edges.end());

    int idx = N;

    for (auto edge : edges) {
        int a = findset(edge.second.first), b = findset(edge.second.second);

        if (a == b) {
            cost[a] = min(cost[a], edge.first);
        } else {
            onion(a, b, ++idx);

            up[a][0] = idx; up[b][0] = idx;

            if (cost[a] != INT_MAX || cost[b] != INT_MAX || deg[edge.second.first] >= 2 || deg[edge.second.second] >= 2) {
                cost[idx] = edge.first;
            }
        }

        deg[edge.second.first]++;
        deg[edge.second.second]++;
    }

    for (int i = idx; i >= 0; i--) {
        for (auto child : adj[i]) {
            depth[child] = depth[i] + 1;
            cost[child] = min(cost[child], cost[i]);
        }
    }



    for (int i = idx; i >= 0; i--) {
        int j = 0;
        while (up[i][j] != -1) {
            up[i][j+1] = up[up[i][j]][j];
            j++;
        }
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    if (depth[X] < depth[Y]) swap(X, Y);

    for (int j = 19; j >= 0; j--) {
        if (up[X][j] == -1) continue;
        if (depth[up[X][j]] >= depth[Y]) {
            X = up[X][j];
        }
    }

    while (X != Y) {
        int j = 19;
        while (up[X][j] == up[Y][j] && j > 0) j--;
        X = up[X][j];
        Y = up[Y][j];
    }

    return cost[X] == INT_MAX ? -1 : cost[X];
}

//int main() {
//	int N, M;
//	cin >> N >> M;
//	vector<int> U(M), V(M), W(M);
//	for (int i = 0; i < M; i++)cin >> U[i] >> V[i] >> W[i];
//	init(N, M, U, V, W);
//	int Q;
//	cin >> Q;
//	while (Q--) {
//		int a, b;
//		cin >> a >> b;
//		cout << getMinimumFuelCapacity(a, b) << "\n";
//	}
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33108 KB Output is correct
2 Correct 15 ms 33304 KB Output is correct
3 Correct 14 ms 33132 KB Output is correct
4 Correct 16 ms 33220 KB Output is correct
5 Correct 16 ms 33180 KB Output is correct
6 Correct 15 ms 33228 KB Output is correct
7 Correct 15 ms 33236 KB Output is correct
8 Correct 15 ms 33180 KB Output is correct
9 Correct 71 ms 40980 KB Output is correct
10 Correct 87 ms 42628 KB Output is correct
11 Correct 82 ms 42484 KB Output is correct
12 Correct 85 ms 43072 KB Output is correct
13 Correct 100 ms 43600 KB Output is correct
14 Correct 79 ms 41280 KB Output is correct
15 Correct 201 ms 46660 KB Output is correct
16 Correct 206 ms 46308 KB Output is correct
17 Correct 222 ms 47040 KB Output is correct
18 Correct 337 ms 47540 KB Output is correct
19 Correct 91 ms 39696 KB Output is correct
20 Correct 245 ms 46696 KB Output is correct
21 Correct 193 ms 46828 KB Output is correct
22 Correct 235 ms 47388 KB Output is correct
23 Correct 313 ms 47980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33108 KB Output is correct
2 Correct 15 ms 33304 KB Output is correct
3 Correct 291 ms 46224 KB Output is correct
4 Correct 318 ms 46496 KB Output is correct
5 Correct 297 ms 46924 KB Output is correct
6 Correct 292 ms 46396 KB Output is correct
7 Correct 324 ms 46748 KB Output is correct
8 Correct 337 ms 46356 KB Output is correct
9 Correct 293 ms 46440 KB Output is correct
10 Correct 309 ms 46324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33108 KB Output is correct
2 Correct 15 ms 33304 KB Output is correct
3 Correct 14 ms 33132 KB Output is correct
4 Correct 16 ms 33220 KB Output is correct
5 Correct 16 ms 33180 KB Output is correct
6 Correct 15 ms 33228 KB Output is correct
7 Correct 15 ms 33236 KB Output is correct
8 Correct 15 ms 33180 KB Output is correct
9 Correct 13 ms 33084 KB Output is correct
10 Correct 14 ms 33240 KB Output is correct
11 Correct 14 ms 33260 KB Output is correct
12 Correct 16 ms 33236 KB Output is correct
13 Correct 13 ms 33220 KB Output is correct
14 Correct 14 ms 33220 KB Output is correct
15 Correct 14 ms 33236 KB Output is correct
16 Correct 14 ms 33224 KB Output is correct
17 Correct 14 ms 33236 KB Output is correct
18 Correct 16 ms 33224 KB Output is correct
19 Correct 17 ms 33164 KB Output is correct
20 Correct 15 ms 33236 KB Output is correct
21 Correct 16 ms 33236 KB Output is correct
22 Correct 16 ms 33224 KB Output is correct
23 Correct 14 ms 33236 KB Output is correct
24 Correct 15 ms 33264 KB Output is correct
25 Correct 15 ms 33236 KB Output is correct
26 Correct 15 ms 33320 KB Output is correct
27 Correct 14 ms 33236 KB Output is correct
28 Correct 15 ms 33228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33084 KB Output is correct
2 Correct 18 ms 33108 KB Output is correct
3 Correct 15 ms 33304 KB Output is correct
4 Correct 14 ms 33132 KB Output is correct
5 Correct 16 ms 33220 KB Output is correct
6 Correct 16 ms 33180 KB Output is correct
7 Correct 15 ms 33228 KB Output is correct
8 Correct 15 ms 33236 KB Output is correct
9 Correct 15 ms 33180 KB Output is correct
10 Correct 71 ms 40980 KB Output is correct
11 Correct 87 ms 42628 KB Output is correct
12 Correct 82 ms 42484 KB Output is correct
13 Correct 85 ms 43072 KB Output is correct
14 Correct 100 ms 43600 KB Output is correct
15 Correct 14 ms 33240 KB Output is correct
16 Correct 14 ms 33260 KB Output is correct
17 Correct 16 ms 33236 KB Output is correct
18 Correct 13 ms 33220 KB Output is correct
19 Correct 14 ms 33220 KB Output is correct
20 Correct 14 ms 33236 KB Output is correct
21 Correct 14 ms 33224 KB Output is correct
22 Correct 14 ms 33236 KB Output is correct
23 Correct 16 ms 33224 KB Output is correct
24 Correct 17 ms 33164 KB Output is correct
25 Correct 15 ms 33236 KB Output is correct
26 Correct 16 ms 33236 KB Output is correct
27 Correct 16 ms 33224 KB Output is correct
28 Correct 14 ms 33236 KB Output is correct
29 Correct 15 ms 33264 KB Output is correct
30 Correct 15 ms 33236 KB Output is correct
31 Correct 15 ms 33320 KB Output is correct
32 Correct 14 ms 33236 KB Output is correct
33 Correct 15 ms 33228 KB Output is correct
34 Correct 24 ms 34412 KB Output is correct
35 Correct 98 ms 42720 KB Output is correct
36 Correct 82 ms 42768 KB Output is correct
37 Correct 82 ms 42756 KB Output is correct
38 Correct 92 ms 42672 KB Output is correct
39 Correct 83 ms 42536 KB Output is correct
40 Correct 92 ms 41784 KB Output is correct
41 Correct 76 ms 43028 KB Output is correct
42 Correct 82 ms 42816 KB Output is correct
43 Correct 104 ms 42760 KB Output is correct
44 Correct 94 ms 43108 KB Output is correct
45 Correct 110 ms 44732 KB Output is correct
46 Correct 81 ms 42780 KB Output is correct
47 Correct 87 ms 42816 KB Output is correct
48 Correct 119 ms 43192 KB Output is correct
49 Correct 74 ms 42816 KB Output is correct
50 Correct 63 ms 39988 KB Output is correct
51 Correct 96 ms 43072 KB Output is correct
52 Correct 120 ms 46592 KB Output is correct
53 Correct 130 ms 47572 KB Output is correct
54 Correct 125 ms 48468 KB Output is correct
55 Correct 98 ms 42820 KB Output is correct
56 Correct 133 ms 47132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 33108 KB Output is correct
2 Correct 15 ms 33304 KB Output is correct
3 Correct 14 ms 33132 KB Output is correct
4 Correct 16 ms 33220 KB Output is correct
5 Correct 16 ms 33180 KB Output is correct
6 Correct 15 ms 33228 KB Output is correct
7 Correct 15 ms 33236 KB Output is correct
8 Correct 15 ms 33180 KB Output is correct
9 Correct 71 ms 40980 KB Output is correct
10 Correct 87 ms 42628 KB Output is correct
11 Correct 82 ms 42484 KB Output is correct
12 Correct 85 ms 43072 KB Output is correct
13 Correct 100 ms 43600 KB Output is correct
14 Correct 79 ms 41280 KB Output is correct
15 Correct 201 ms 46660 KB Output is correct
16 Correct 206 ms 46308 KB Output is correct
17 Correct 222 ms 47040 KB Output is correct
18 Correct 337 ms 47540 KB Output is correct
19 Correct 291 ms 46224 KB Output is correct
20 Correct 318 ms 46496 KB Output is correct
21 Correct 297 ms 46924 KB Output is correct
22 Correct 292 ms 46396 KB Output is correct
23 Correct 324 ms 46748 KB Output is correct
24 Correct 337 ms 46356 KB Output is correct
25 Correct 293 ms 46440 KB Output is correct
26 Correct 309 ms 46324 KB Output is correct
27 Correct 14 ms 33240 KB Output is correct
28 Correct 14 ms 33260 KB Output is correct
29 Correct 16 ms 33236 KB Output is correct
30 Correct 13 ms 33220 KB Output is correct
31 Correct 14 ms 33220 KB Output is correct
32 Correct 14 ms 33236 KB Output is correct
33 Correct 14 ms 33224 KB Output is correct
34 Correct 14 ms 33236 KB Output is correct
35 Correct 16 ms 33224 KB Output is correct
36 Correct 24 ms 34412 KB Output is correct
37 Correct 98 ms 42720 KB Output is correct
38 Correct 82 ms 42768 KB Output is correct
39 Correct 82 ms 42756 KB Output is correct
40 Correct 92 ms 42672 KB Output is correct
41 Correct 83 ms 42536 KB Output is correct
42 Correct 92 ms 41784 KB Output is correct
43 Correct 76 ms 43028 KB Output is correct
44 Correct 82 ms 42816 KB Output is correct
45 Correct 104 ms 42760 KB Output is correct
46 Correct 94 ms 43108 KB Output is correct
47 Correct 34 ms 34728 KB Output is correct
48 Correct 206 ms 46656 KB Output is correct
49 Correct 199 ms 46640 KB Output is correct
50 Correct 192 ms 46680 KB Output is correct
51 Correct 228 ms 46540 KB Output is correct
52 Correct 182 ms 45964 KB Output is correct
53 Correct 174 ms 44752 KB Output is correct
54 Correct 218 ms 47360 KB Output is correct
55 Correct 218 ms 46640 KB Output is correct
56 Correct 318 ms 46692 KB Output is correct
57 Correct 226 ms 47376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33084 KB Output is correct
2 Correct 18 ms 33108 KB Output is correct
3 Correct 15 ms 33304 KB Output is correct
4 Correct 14 ms 33132 KB Output is correct
5 Correct 16 ms 33220 KB Output is correct
6 Correct 16 ms 33180 KB Output is correct
7 Correct 15 ms 33228 KB Output is correct
8 Correct 15 ms 33236 KB Output is correct
9 Correct 15 ms 33180 KB Output is correct
10 Correct 71 ms 40980 KB Output is correct
11 Correct 87 ms 42628 KB Output is correct
12 Correct 82 ms 42484 KB Output is correct
13 Correct 85 ms 43072 KB Output is correct
14 Correct 100 ms 43600 KB Output is correct
15 Correct 79 ms 41280 KB Output is correct
16 Correct 201 ms 46660 KB Output is correct
17 Correct 206 ms 46308 KB Output is correct
18 Correct 222 ms 47040 KB Output is correct
19 Correct 337 ms 47540 KB Output is correct
20 Correct 291 ms 46224 KB Output is correct
21 Correct 318 ms 46496 KB Output is correct
22 Correct 297 ms 46924 KB Output is correct
23 Correct 292 ms 46396 KB Output is correct
24 Correct 324 ms 46748 KB Output is correct
25 Correct 337 ms 46356 KB Output is correct
26 Correct 293 ms 46440 KB Output is correct
27 Correct 309 ms 46324 KB Output is correct
28 Correct 14 ms 33240 KB Output is correct
29 Correct 14 ms 33260 KB Output is correct
30 Correct 16 ms 33236 KB Output is correct
31 Correct 13 ms 33220 KB Output is correct
32 Correct 14 ms 33220 KB Output is correct
33 Correct 14 ms 33236 KB Output is correct
34 Correct 14 ms 33224 KB Output is correct
35 Correct 14 ms 33236 KB Output is correct
36 Correct 16 ms 33224 KB Output is correct
37 Correct 24 ms 34412 KB Output is correct
38 Correct 98 ms 42720 KB Output is correct
39 Correct 82 ms 42768 KB Output is correct
40 Correct 82 ms 42756 KB Output is correct
41 Correct 92 ms 42672 KB Output is correct
42 Correct 83 ms 42536 KB Output is correct
43 Correct 92 ms 41784 KB Output is correct
44 Correct 76 ms 43028 KB Output is correct
45 Correct 82 ms 42816 KB Output is correct
46 Correct 104 ms 42760 KB Output is correct
47 Correct 94 ms 43108 KB Output is correct
48 Correct 34 ms 34728 KB Output is correct
49 Correct 206 ms 46656 KB Output is correct
50 Correct 199 ms 46640 KB Output is correct
51 Correct 192 ms 46680 KB Output is correct
52 Correct 228 ms 46540 KB Output is correct
53 Correct 182 ms 45964 KB Output is correct
54 Correct 174 ms 44752 KB Output is correct
55 Correct 218 ms 47360 KB Output is correct
56 Correct 218 ms 46640 KB Output is correct
57 Correct 318 ms 46692 KB Output is correct
58 Correct 226 ms 47376 KB Output is correct
59 Correct 91 ms 39696 KB Output is correct
60 Correct 245 ms 46696 KB Output is correct
61 Correct 193 ms 46828 KB Output is correct
62 Correct 235 ms 47388 KB Output is correct
63 Correct 313 ms 47980 KB Output is correct
64 Correct 17 ms 33164 KB Output is correct
65 Correct 15 ms 33236 KB Output is correct
66 Correct 16 ms 33236 KB Output is correct
67 Correct 16 ms 33224 KB Output is correct
68 Correct 14 ms 33236 KB Output is correct
69 Correct 15 ms 33264 KB Output is correct
70 Correct 15 ms 33236 KB Output is correct
71 Correct 15 ms 33320 KB Output is correct
72 Correct 14 ms 33236 KB Output is correct
73 Correct 15 ms 33228 KB Output is correct
74 Correct 110 ms 44732 KB Output is correct
75 Correct 81 ms 42780 KB Output is correct
76 Correct 87 ms 42816 KB Output is correct
77 Correct 119 ms 43192 KB Output is correct
78 Correct 74 ms 42816 KB Output is correct
79 Correct 63 ms 39988 KB Output is correct
80 Correct 96 ms 43072 KB Output is correct
81 Correct 120 ms 46592 KB Output is correct
82 Correct 130 ms 47572 KB Output is correct
83 Correct 125 ms 48468 KB Output is correct
84 Correct 98 ms 42820 KB Output is correct
85 Correct 133 ms 47132 KB Output is correct
86 Correct 75 ms 38264 KB Output is correct
87 Correct 190 ms 46632 KB Output is correct
88 Correct 232 ms 46776 KB Output is correct
89 Correct 250 ms 46724 KB Output is correct
90 Correct 170 ms 46528 KB Output is correct
91 Correct 172 ms 46224 KB Output is correct
92 Correct 237 ms 46652 KB Output is correct
93 Correct 240 ms 50180 KB Output is correct
94 Correct 280 ms 51336 KB Output is correct
95 Correct 270 ms 52256 KB Output is correct
96 Correct 365 ms 46676 KB Output is correct
97 Correct 298 ms 49108 KB Output is correct