답안 #1092901

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1092901 2024-09-25T10:32:45 Z raphaelp 자매 도시 (APIO20_swap) C++14
100 / 100
374 ms 62196 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

class UnionFind
{
private:
    vector<int> p;

public:
    UnionFind(int x)
    {
        p.assign(x, 0);
        for (int i = 0; i < x; i++)
            p[i] = i;
    }
    int find(int x) { return (x == p[x]) ? x : p[x] = find(p[x]); }
    bool merge(int a, int b)
    {
        int x = find(a), y = find(b);
        if (x == y)
            return 0;
        p[x] = y;
        return 1;
    }
};
vector<vector<int>> lca, lca2, lca3;
vector<int> depth;

void dfs(int x, int p, vector<vector<pair<int, int>>> &AR, vector<int> &best, int prof)
{
    depth[x] = prof;
    lca[0][x] = p;
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i].first == p)
            continue;
        lca3[0][AR[x][i].first] = AR[x][i].second;
        dfs(AR[x][i].first, x, AR, best, prof + 1);
        best[x] = min(best[x], max(best[AR[x][i].first], AR[x][i].second));
    }
}
void dfs2(int x, int p, vector<vector<pair<int, int>>> &AR, vector<int> &best)
{
    lca2[0][x] = min(best[x], best[p]);
    for (int i = 0; i < AR[x].size(); i++)
    {
        if (AR[x][i].first == p)
            continue;
        best[AR[x][i].first] = min(best[AR[x][i].first], max(best[x], AR[x][i].second));
        dfs2(AR[x][i].first, x, AR, best);
    }
}
void init(int N, int M, vector<int> U, vector<int> V, vector<int> W)
{
    vector<vector<int>> Tab;
    vector<vector<pair<int, int>>> AR2(N);
    for (int i = 0; i < M; i++)
    {
        Tab.push_back({W[i], U[i], V[i]});
        AR2[U[i]].push_back({W[i], V[i]});
        AR2[V[i]].push_back({W[i], U[i]});
    }
    for (int i = 0; i < N; i++)
        sort(AR2[i].begin(), AR2[i].end());
    sort(Tab.begin(), Tab.end());
    UnionFind UF(N);
    vector<vector<pair<int, int>>> AR(N);
    vector<int> best(N, 1000000010);
    for (int i = 0; i < M; i++)
    {
        if (UF.merge(Tab[i][1], Tab[i][2]))
        {
            AR[Tab[i][1]].push_back({Tab[i][2], Tab[i][0]});
            AR[Tab[i][2]].push_back({Tab[i][1], Tab[i][0]});
        }
        else
        {
            best[Tab[i][1]] = min(best[Tab[i][1]], Tab[i][0]);
            best[Tab[i][2]] = min(best[Tab[i][2]], Tab[i][0]);
        }
    }
    for (int i = 0; i < N; i++)
    {
        if (AR2[i].size() >= 3)
        {
            best[i] = min(best[i], max(AR2[i][0].first, max(AR2[i][1].first, AR2[i][2].first)));
        }
    }
    lca.assign(log2(N) + 1, vector<int>(N, 0));
    lca2.assign(log2(N) + 1, vector<int>(N, 0));
    lca3.assign(log2(N) + 1, vector<int>(N, 0));
    depth.assign(N, 0);
    dfs(0, 0, AR, best, 0);
    dfs2(0, 0, AR, best);
    for (int i = 1; i <= log2(N); i++)
    {
        for (int j = 0; j < N; j++)
        {
            lca[i][j] = lca[i - 1][lca[i - 1][j]];
            lca2[i][j] = min(lca2[i - 1][j], lca2[i - 1][lca[i - 1][j]]);
            lca3[i][j] = max(lca3[i - 1][j], lca3[i - 1][lca[i - 1][j]]);
        }
    }
}

int getMinimumFuelCapacity(int X, int Y)
{
    int a = X, b = Y, N = lca[0].size();
    int best = 1000000010;
    int cost = 0;
    if (depth[a] > depth[b])
        swap(a, b);
    for (int i = log2(N); i >= 0; i--)
    {
        if (depth[lca[i][b]] >= depth[a])
        {
            best = min(best, lca2[i][b]);
            cost = max(cost, lca3[i][b]);
            b = lca[i][b];
        }
    }
    if (a == b)
        return (max(best, cost) == 1000000010) ? -1 : max(best, cost);
    for (int i = log2(N); i >= 0; i--)
    {
        if (lca[i][a] != lca[i][b])
        {
            best = min(best, min(lca2[i][a], lca2[i][b]));
            cost = max(cost, max(lca3[i][a], lca3[i][b]));
            a = lca[i][a];
            b = lca[i][b];
        }
    }
    best = min(best, min(lca2[0][a], lca2[0][b]));
    cost = max(cost, max(lca3[0][a], lca3[0][b]));
    return (max(best, cost) == 1000000010) ? -1 : max(best, cost);
}

/*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;
    for (int i = 0; i < Q; i++)
    {
        int a, b;
        cin >> a >> b;
        cout << getMinimumFuelCapacity(a, b) << '\n';
    }
}*/

Compilation message

swap.cpp: In function 'void dfs(int, int, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&, int)':
swap.cpp:34:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
swap.cpp: In function 'void dfs2(int, int, std::vector<std::vector<std::pair<int, int> > >&, std::vector<int>&)':
swap.cpp:46:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int i = 0; i < AR[x].size(); i++)
      |                     ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 0 ms 352 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 87 ms 37852 KB Output is correct
10 Correct 112 ms 47872 KB Output is correct
11 Correct 117 ms 46336 KB Output is correct
12 Correct 128 ms 49400 KB Output is correct
13 Correct 101 ms 51824 KB Output is correct
14 Correct 101 ms 37404 KB Output is correct
15 Correct 294 ms 51712 KB Output is correct
16 Correct 281 ms 48328 KB Output is correct
17 Correct 280 ms 55556 KB Output is correct
18 Correct 261 ms 53780 KB Output is correct
19 Correct 87 ms 11316 KB Output is correct
20 Correct 277 ms 50688 KB Output is correct
21 Correct 269 ms 48188 KB Output is correct
22 Correct 277 ms 52692 KB Output is correct
23 Correct 273 ms 53356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 139 ms 44808 KB Output is correct
4 Correct 144 ms 47104 KB Output is correct
5 Correct 154 ms 45824 KB Output is correct
6 Correct 139 ms 46844 KB Output is correct
7 Correct 140 ms 46292 KB Output is correct
8 Correct 150 ms 44548 KB Output is correct
9 Correct 140 ms 46172 KB Output is correct
10 Correct 133 ms 44036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 0 ms 352 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 540 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 720 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 704 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 1 ms 700 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 352 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 87 ms 37852 KB Output is correct
11 Correct 112 ms 47872 KB Output is correct
12 Correct 117 ms 46336 KB Output is correct
13 Correct 128 ms 49400 KB Output is correct
14 Correct 101 ms 51824 KB Output is correct
15 Correct 1 ms 860 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 540 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 720 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 704 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 2 ms 860 KB Output is correct
31 Correct 1 ms 700 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 12 ms 5784 KB Output is correct
35 Correct 108 ms 48592 KB Output is correct
36 Correct 110 ms 45572 KB Output is correct
37 Correct 106 ms 43260 KB Output is correct
38 Correct 116 ms 41980 KB Output is correct
39 Correct 105 ms 41384 KB Output is correct
40 Correct 90 ms 38240 KB Output is correct
41 Correct 106 ms 46596 KB Output is correct
42 Correct 106 ms 47684 KB Output is correct
43 Correct 104 ms 49224 KB Output is correct
44 Correct 104 ms 43524 KB Output is correct
45 Correct 124 ms 42232 KB Output is correct
46 Correct 104 ms 44288 KB Output is correct
47 Correct 101 ms 41988 KB Output is correct
48 Correct 124 ms 42988 KB Output is correct
49 Correct 85 ms 20472 KB Output is correct
50 Correct 62 ms 17156 KB Output is correct
51 Correct 98 ms 34556 KB Output is correct
52 Correct 156 ms 51960 KB Output is correct
53 Correct 179 ms 53124 KB Output is correct
54 Correct 189 ms 58612 KB Output is correct
55 Correct 110 ms 50944 KB Output is correct
56 Correct 164 ms 51892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 0 ms 352 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 608 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 87 ms 37852 KB Output is correct
10 Correct 112 ms 47872 KB Output is correct
11 Correct 117 ms 46336 KB Output is correct
12 Correct 128 ms 49400 KB Output is correct
13 Correct 101 ms 51824 KB Output is correct
14 Correct 101 ms 37404 KB Output is correct
15 Correct 294 ms 51712 KB Output is correct
16 Correct 281 ms 48328 KB Output is correct
17 Correct 280 ms 55556 KB Output is correct
18 Correct 261 ms 53780 KB Output is correct
19 Correct 139 ms 44808 KB Output is correct
20 Correct 144 ms 47104 KB Output is correct
21 Correct 154 ms 45824 KB Output is correct
22 Correct 139 ms 46844 KB Output is correct
23 Correct 140 ms 46292 KB Output is correct
24 Correct 150 ms 44548 KB Output is correct
25 Correct 140 ms 46172 KB Output is correct
26 Correct 133 ms 44036 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 540 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 720 KB Output is correct
36 Correct 12 ms 5784 KB Output is correct
37 Correct 108 ms 48592 KB Output is correct
38 Correct 110 ms 45572 KB Output is correct
39 Correct 106 ms 43260 KB Output is correct
40 Correct 116 ms 41980 KB Output is correct
41 Correct 105 ms 41384 KB Output is correct
42 Correct 90 ms 38240 KB Output is correct
43 Correct 106 ms 46596 KB Output is correct
44 Correct 106 ms 47684 KB Output is correct
45 Correct 104 ms 49224 KB Output is correct
46 Correct 104 ms 43524 KB Output is correct
47 Correct 21 ms 5528 KB Output is correct
48 Correct 323 ms 50180 KB Output is correct
49 Correct 305 ms 48272 KB Output is correct
50 Correct 303 ms 47108 KB Output is correct
51 Correct 275 ms 46332 KB Output is correct
52 Correct 254 ms 43408 KB Output is correct
53 Correct 188 ms 32768 KB Output is correct
54 Correct 301 ms 49676 KB Output is correct
55 Correct 281 ms 50916 KB Output is correct
56 Correct 250 ms 54000 KB Output is correct
57 Correct 240 ms 47360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 352 KB Output is correct
4 Correct 0 ms 352 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 608 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 87 ms 37852 KB Output is correct
11 Correct 112 ms 47872 KB Output is correct
12 Correct 117 ms 46336 KB Output is correct
13 Correct 128 ms 49400 KB Output is correct
14 Correct 101 ms 51824 KB Output is correct
15 Correct 101 ms 37404 KB Output is correct
16 Correct 294 ms 51712 KB Output is correct
17 Correct 281 ms 48328 KB Output is correct
18 Correct 280 ms 55556 KB Output is correct
19 Correct 261 ms 53780 KB Output is correct
20 Correct 139 ms 44808 KB Output is correct
21 Correct 144 ms 47104 KB Output is correct
22 Correct 154 ms 45824 KB Output is correct
23 Correct 139 ms 46844 KB Output is correct
24 Correct 140 ms 46292 KB Output is correct
25 Correct 150 ms 44548 KB Output is correct
26 Correct 140 ms 46172 KB Output is correct
27 Correct 133 ms 44036 KB Output is correct
28 Correct 1 ms 860 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 540 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 720 KB Output is correct
37 Correct 12 ms 5784 KB Output is correct
38 Correct 108 ms 48592 KB Output is correct
39 Correct 110 ms 45572 KB Output is correct
40 Correct 106 ms 43260 KB Output is correct
41 Correct 116 ms 41980 KB Output is correct
42 Correct 105 ms 41384 KB Output is correct
43 Correct 90 ms 38240 KB Output is correct
44 Correct 106 ms 46596 KB Output is correct
45 Correct 106 ms 47684 KB Output is correct
46 Correct 104 ms 49224 KB Output is correct
47 Correct 104 ms 43524 KB Output is correct
48 Correct 21 ms 5528 KB Output is correct
49 Correct 323 ms 50180 KB Output is correct
50 Correct 305 ms 48272 KB Output is correct
51 Correct 303 ms 47108 KB Output is correct
52 Correct 275 ms 46332 KB Output is correct
53 Correct 254 ms 43408 KB Output is correct
54 Correct 188 ms 32768 KB Output is correct
55 Correct 301 ms 49676 KB Output is correct
56 Correct 281 ms 50916 KB Output is correct
57 Correct 250 ms 54000 KB Output is correct
58 Correct 240 ms 47360 KB Output is correct
59 Correct 87 ms 11316 KB Output is correct
60 Correct 277 ms 50688 KB Output is correct
61 Correct 269 ms 48188 KB Output is correct
62 Correct 277 ms 52692 KB Output is correct
63 Correct 273 ms 53356 KB Output is correct
64 Correct 1 ms 604 KB Output is correct
65 Correct 1 ms 704 KB Output is correct
66 Correct 1 ms 604 KB Output is correct
67 Correct 1 ms 604 KB Output is correct
68 Correct 1 ms 604 KB Output is correct
69 Correct 1 ms 860 KB Output is correct
70 Correct 2 ms 860 KB Output is correct
71 Correct 1 ms 700 KB Output is correct
72 Correct 1 ms 604 KB Output is correct
73 Correct 1 ms 860 KB Output is correct
74 Correct 124 ms 42232 KB Output is correct
75 Correct 104 ms 44288 KB Output is correct
76 Correct 101 ms 41988 KB Output is correct
77 Correct 124 ms 42988 KB Output is correct
78 Correct 85 ms 20472 KB Output is correct
79 Correct 62 ms 17156 KB Output is correct
80 Correct 98 ms 34556 KB Output is correct
81 Correct 156 ms 51960 KB Output is correct
82 Correct 179 ms 53124 KB Output is correct
83 Correct 189 ms 58612 KB Output is correct
84 Correct 110 ms 50944 KB Output is correct
85 Correct 164 ms 51892 KB Output is correct
86 Correct 60 ms 16136 KB Output is correct
87 Correct 294 ms 47868 KB Output is correct
88 Correct 291 ms 47960 KB Output is correct
89 Correct 210 ms 44392 KB Output is correct
90 Correct 141 ms 24604 KB Output is correct
91 Correct 156 ms 24992 KB Output is correct
92 Correct 205 ms 37892 KB Output is correct
93 Correct 312 ms 56188 KB Output is correct
94 Correct 277 ms 56820 KB Output is correct
95 Correct 374 ms 62196 KB Output is correct
96 Correct 252 ms 51360 KB Output is correct
97 Correct 252 ms 51464 KB Output is correct