답안 #486625

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486625 2021-11-12T07:26:05 Z aryan12 자매 도시 (APIO20_swap) C++17
100 / 100
304 ms 62744 KB
#include "swap.h"
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;
vector<array<int, 3> > edges(MAXN * 2);
int DP[20][MAXN * 3];
int depth[MAXN * 3];
vector<int> g[MAXN * 3];

struct ufds {
    bool isLine;
    pair<int, int> endpoints;
    int parent, value;

    ufds(int x) {
        isLine = true;
        endpoints = {x, x};
        parent = x;
        value = 1e9;
    } 
} *Node[MAXN * 3];

int Find(int x) {
    if(Node[x]->parent == x) {
        return x;
    }
    return Node[x]->parent = Find(Node[x]->parent);
}

void Unite(int a, int b, int new_idx, int cur_val) {
    int original_a = a, original_b = b;
    a = Find(a);
    b = Find(b);
    Node[a]->parent = new_idx;
    Node[b]->parent = new_idx;
    g[new_idx].push_back(a);
    g[new_idx].push_back(b);
    Node[new_idx]->value = cur_val;
    DP[0][a] = new_idx;
    DP[0][b] = new_idx;
    if(a == b || !Node[a]->isLine || !Node[b]->isLine) {
        Node[new_idx]->isLine = false;
        Node[new_idx]->endpoints = {-1, -1};
    }
    else {
        //cout << "a = " << a << ", b = " << b << "\n";
        //cout << "A Line = " << Node[a]->endpoints.first << ", " << Node[a]->endpoints.second << "\n";
        //cout << "B Line = " << Node[b]->endpoints.first << ", " << Node[b]->endpoints.second << "\n";
        if(Node[a]->endpoints.first != original_a) {
            swap(Node[a]->endpoints.first, Node[a]->endpoints.second);
        }
        if(Node[b]->endpoints.first != original_b) {
            swap(Node[b]->endpoints.first, Node[b]->endpoints.second);
        }
        if(original_a == Node[a]->endpoints.first && original_b == Node[b]->endpoints.first) {
            Node[new_idx]->isLine = true;
            Node[new_idx]->endpoints = {Node[a]->endpoints.second, Node[b]->endpoints.second};
        }
        else {
            Node[new_idx]->isLine = false;
            Node[new_idx]->endpoints = {-1, -1};
        }
    }
}

bool cmp(array<int, 3> a, array<int, 3> b) {
    return a[2] < b[2];
}

void dfs(int node, int dep) {
    depth[node] = dep;
    for(int i = 0; i < g[node].size(); i++) {
        dfs(g[node][i], dep + 1);
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    auto begin = std::chrono::high_resolution_clock::now();
    for(int i = 0; i < 20; i++) {
        for(int j = 0; j < MAXN * 3; j++) {
            DP[i][j] = -1;
        }
    }
    for(int i = 0; i < M; i++) {
        edges[i] = {U[i], V[i], W[i]};
    }
    sort(edges.begin(), edges.begin() + M, cmp);
    int cur_new = N;
    for(int i = 0; i < MAXN * 3; i++) {
        Node[i] = new ufds(i);
    }
    for(int i = 0; i < M; i++) {
        //cout << Find(edges[i][0]) << " " << Find(edges[i][1]) << "\n"; 
        int x = Find(edges[i][0]), y = Find(edges[i][1]);
        //cout << "x = " << x << ", Node[x] is a line? " << Node[x]->isLine << "\n";
        if(x != y || Node[x]->isLine == true) {
            //cout << "Uniting edge: (" << edges[i][0] << ", " << edges[i][1] << ", " << edges[i][2] << ")\n";
            Unite(edges[i][0], edges[i][1], cur_new++, edges[i][2]);
        }
    }
    assert(cur_new <= MAXN * 3);
    for(int i = 1; i < 20; i++) {
        for(int j = 0; j < cur_new; j++) {
            if(DP[i - 1][j] != -1) {
                DP[i][j] = DP[i - 1][DP[i - 1][j]];
            }
            else {
                DP[i][j] = -1;
            }
        }
    }
    dfs(cur_new - 1, 0);
    auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << elapsed.count() * 1e-9 << "\n";
    if(elapsed.count() * 1e-9 > 2.0) {
        assert(false);
    }
}

int lca(int X, int Y) {
    if(depth[X] < depth[Y]) {
        swap(X, Y);
    }
    int diff = depth[X] - depth[Y];
    for(int i = 19; i >= 0; i--) {
        if(diff >= (1 << i)) {
            diff -= (1 << i);
            X = DP[i][X];
        }
    }
    assert(depth[X] == depth[Y]);
    for(int i = 19; i >= 0; i--) {
        if(DP[i][X] != -1) {
            if(DP[i][X] != DP[i][Y] || Node[DP[i][X]]->isLine) {
                X = DP[i][X];
                Y = DP[i][Y];
            }
        }
        if(DP[i][X] != -1 && (DP[i][X] != DP[i][Y] && Node[DP[i][X]]->isLine)) {
            X = DP[i][X];
            Y = DP[i][Y];
        }
    }
    if(DP[0][X] == -1) {
        return -1;
    }
    X = DP[0][X];
    Y = DP[0][Y];
    if(X != Y || Node[X]->isLine) {
        return -1;
    }
    return Node[X]->value;
}

int getMinimumFuelCapacity(int X, int Y) {
    return lca(X, Y);
}

Compilation message

swap.cpp: In function 'void dfs(int, int)':
swap.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for(int i = 0; i < g[node].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 44876 KB Output is correct
2 Correct 23 ms 44876 KB Output is correct
3 Correct 24 ms 44892 KB Output is correct
4 Correct 23 ms 44892 KB Output is correct
5 Correct 30 ms 44980 KB Output is correct
6 Correct 23 ms 44988 KB Output is correct
7 Correct 26 ms 44948 KB Output is correct
8 Correct 23 ms 44960 KB Output is correct
9 Correct 84 ms 49732 KB Output is correct
10 Correct 110 ms 50932 KB Output is correct
11 Correct 95 ms 50744 KB Output is correct
12 Correct 102 ms 51396 KB Output is correct
13 Correct 84 ms 53444 KB Output is correct
14 Correct 84 ms 49920 KB Output is correct
15 Correct 185 ms 52688 KB Output is correct
16 Correct 183 ms 52836 KB Output is correct
17 Correct 191 ms 52904 KB Output is correct
18 Correct 260 ms 55216 KB Output is correct
19 Correct 91 ms 49288 KB Output is correct
20 Correct 196 ms 55636 KB Output is correct
21 Correct 195 ms 55724 KB Output is correct
22 Correct 222 ms 56192 KB Output is correct
23 Correct 290 ms 58388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 44876 KB Output is correct
2 Correct 23 ms 44876 KB Output is correct
3 Correct 304 ms 58212 KB Output is correct
4 Correct 290 ms 62504 KB Output is correct
5 Correct 300 ms 62676 KB Output is correct
6 Correct 291 ms 62420 KB Output is correct
7 Correct 304 ms 62664 KB Output is correct
8 Correct 290 ms 62112 KB Output is correct
9 Correct 292 ms 62364 KB Output is correct
10 Correct 297 ms 62076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 44876 KB Output is correct
2 Correct 23 ms 44876 KB Output is correct
3 Correct 24 ms 44892 KB Output is correct
4 Correct 23 ms 44892 KB Output is correct
5 Correct 30 ms 44980 KB Output is correct
6 Correct 23 ms 44988 KB Output is correct
7 Correct 26 ms 44948 KB Output is correct
8 Correct 23 ms 44960 KB Output is correct
9 Correct 23 ms 44832 KB Output is correct
10 Correct 24 ms 44920 KB Output is correct
11 Correct 24 ms 44976 KB Output is correct
12 Correct 24 ms 44860 KB Output is correct
13 Correct 24 ms 44868 KB Output is correct
14 Correct 28 ms 44988 KB Output is correct
15 Correct 24 ms 45004 KB Output is correct
16 Correct 23 ms 44996 KB Output is correct
17 Correct 24 ms 45012 KB Output is correct
18 Correct 24 ms 44928 KB Output is correct
19 Correct 24 ms 44920 KB Output is correct
20 Correct 24 ms 44928 KB Output is correct
21 Correct 24 ms 45016 KB Output is correct
22 Correct 24 ms 45004 KB Output is correct
23 Correct 23 ms 44900 KB Output is correct
24 Correct 24 ms 44928 KB Output is correct
25 Correct 25 ms 45004 KB Output is correct
26 Correct 24 ms 44940 KB Output is correct
27 Correct 24 ms 45024 KB Output is correct
28 Correct 24 ms 45004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 44832 KB Output is correct
2 Correct 23 ms 44876 KB Output is correct
3 Correct 23 ms 44876 KB Output is correct
4 Correct 24 ms 44892 KB Output is correct
5 Correct 23 ms 44892 KB Output is correct
6 Correct 30 ms 44980 KB Output is correct
7 Correct 23 ms 44988 KB Output is correct
8 Correct 26 ms 44948 KB Output is correct
9 Correct 23 ms 44960 KB Output is correct
10 Correct 84 ms 49732 KB Output is correct
11 Correct 110 ms 50932 KB Output is correct
12 Correct 95 ms 50744 KB Output is correct
13 Correct 102 ms 51396 KB Output is correct
14 Correct 84 ms 53444 KB Output is correct
15 Correct 24 ms 44920 KB Output is correct
16 Correct 24 ms 44976 KB Output is correct
17 Correct 24 ms 44860 KB Output is correct
18 Correct 24 ms 44868 KB Output is correct
19 Correct 28 ms 44988 KB Output is correct
20 Correct 24 ms 45004 KB Output is correct
21 Correct 23 ms 44996 KB Output is correct
22 Correct 24 ms 45012 KB Output is correct
23 Correct 24 ms 44928 KB Output is correct
24 Correct 24 ms 44920 KB Output is correct
25 Correct 24 ms 44928 KB Output is correct
26 Correct 24 ms 45016 KB Output is correct
27 Correct 24 ms 45004 KB Output is correct
28 Correct 23 ms 44900 KB Output is correct
29 Correct 24 ms 44928 KB Output is correct
30 Correct 25 ms 45004 KB Output is correct
31 Correct 24 ms 44940 KB Output is correct
32 Correct 24 ms 45024 KB Output is correct
33 Correct 24 ms 45004 KB Output is correct
34 Correct 31 ms 45996 KB Output is correct
35 Correct 101 ms 52832 KB Output is correct
36 Correct 104 ms 52900 KB Output is correct
37 Correct 108 ms 53072 KB Output is correct
38 Correct 97 ms 52804 KB Output is correct
39 Correct 101 ms 52804 KB Output is correct
40 Correct 95 ms 52096 KB Output is correct
41 Correct 100 ms 52840 KB Output is correct
42 Correct 102 ms 53048 KB Output is correct
43 Correct 83 ms 56004 KB Output is correct
44 Correct 106 ms 52916 KB Output is correct
45 Correct 123 ms 54340 KB Output is correct
46 Correct 97 ms 52932 KB Output is correct
47 Correct 105 ms 53060 KB Output is correct
48 Correct 108 ms 54212 KB Output is correct
49 Correct 78 ms 50480 KB Output is correct
50 Correct 68 ms 49860 KB Output is correct
51 Correct 104 ms 53108 KB Output is correct
52 Correct 145 ms 54348 KB Output is correct
53 Correct 165 ms 56388 KB Output is correct
54 Correct 153 ms 55236 KB Output is correct
55 Correct 82 ms 55620 KB Output is correct
56 Correct 161 ms 56040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 44876 KB Output is correct
2 Correct 23 ms 44876 KB Output is correct
3 Correct 24 ms 44892 KB Output is correct
4 Correct 23 ms 44892 KB Output is correct
5 Correct 30 ms 44980 KB Output is correct
6 Correct 23 ms 44988 KB Output is correct
7 Correct 26 ms 44948 KB Output is correct
8 Correct 23 ms 44960 KB Output is correct
9 Correct 84 ms 49732 KB Output is correct
10 Correct 110 ms 50932 KB Output is correct
11 Correct 95 ms 50744 KB Output is correct
12 Correct 102 ms 51396 KB Output is correct
13 Correct 84 ms 53444 KB Output is correct
14 Correct 84 ms 49920 KB Output is correct
15 Correct 185 ms 52688 KB Output is correct
16 Correct 183 ms 52836 KB Output is correct
17 Correct 191 ms 52904 KB Output is correct
18 Correct 260 ms 55216 KB Output is correct
19 Correct 304 ms 58212 KB Output is correct
20 Correct 290 ms 62504 KB Output is correct
21 Correct 300 ms 62676 KB Output is correct
22 Correct 291 ms 62420 KB Output is correct
23 Correct 304 ms 62664 KB Output is correct
24 Correct 290 ms 62112 KB Output is correct
25 Correct 292 ms 62364 KB Output is correct
26 Correct 297 ms 62076 KB Output is correct
27 Correct 24 ms 44920 KB Output is correct
28 Correct 24 ms 44976 KB Output is correct
29 Correct 24 ms 44860 KB Output is correct
30 Correct 24 ms 44868 KB Output is correct
31 Correct 28 ms 44988 KB Output is correct
32 Correct 24 ms 45004 KB Output is correct
33 Correct 23 ms 44996 KB Output is correct
34 Correct 24 ms 45012 KB Output is correct
35 Correct 24 ms 44928 KB Output is correct
36 Correct 31 ms 45996 KB Output is correct
37 Correct 101 ms 52832 KB Output is correct
38 Correct 104 ms 52900 KB Output is correct
39 Correct 108 ms 53072 KB Output is correct
40 Correct 97 ms 52804 KB Output is correct
41 Correct 101 ms 52804 KB Output is correct
42 Correct 95 ms 52096 KB Output is correct
43 Correct 100 ms 52840 KB Output is correct
44 Correct 102 ms 53048 KB Output is correct
45 Correct 83 ms 56004 KB Output is correct
46 Correct 106 ms 52916 KB Output is correct
47 Correct 38 ms 46436 KB Output is correct
48 Correct 200 ms 57884 KB Output is correct
49 Correct 193 ms 57768 KB Output is correct
50 Correct 184 ms 57896 KB Output is correct
51 Correct 184 ms 57832 KB Output is correct
52 Correct 172 ms 57308 KB Output is correct
53 Correct 155 ms 56340 KB Output is correct
54 Correct 192 ms 58732 KB Output is correct
55 Correct 205 ms 57824 KB Output is correct
56 Correct 294 ms 60264 KB Output is correct
57 Correct 195 ms 58748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 44832 KB Output is correct
2 Correct 23 ms 44876 KB Output is correct
3 Correct 23 ms 44876 KB Output is correct
4 Correct 24 ms 44892 KB Output is correct
5 Correct 23 ms 44892 KB Output is correct
6 Correct 30 ms 44980 KB Output is correct
7 Correct 23 ms 44988 KB Output is correct
8 Correct 26 ms 44948 KB Output is correct
9 Correct 23 ms 44960 KB Output is correct
10 Correct 84 ms 49732 KB Output is correct
11 Correct 110 ms 50932 KB Output is correct
12 Correct 95 ms 50744 KB Output is correct
13 Correct 102 ms 51396 KB Output is correct
14 Correct 84 ms 53444 KB Output is correct
15 Correct 84 ms 49920 KB Output is correct
16 Correct 185 ms 52688 KB Output is correct
17 Correct 183 ms 52836 KB Output is correct
18 Correct 191 ms 52904 KB Output is correct
19 Correct 260 ms 55216 KB Output is correct
20 Correct 304 ms 58212 KB Output is correct
21 Correct 290 ms 62504 KB Output is correct
22 Correct 300 ms 62676 KB Output is correct
23 Correct 291 ms 62420 KB Output is correct
24 Correct 304 ms 62664 KB Output is correct
25 Correct 290 ms 62112 KB Output is correct
26 Correct 292 ms 62364 KB Output is correct
27 Correct 297 ms 62076 KB Output is correct
28 Correct 24 ms 44920 KB Output is correct
29 Correct 24 ms 44976 KB Output is correct
30 Correct 24 ms 44860 KB Output is correct
31 Correct 24 ms 44868 KB Output is correct
32 Correct 28 ms 44988 KB Output is correct
33 Correct 24 ms 45004 KB Output is correct
34 Correct 23 ms 44996 KB Output is correct
35 Correct 24 ms 45012 KB Output is correct
36 Correct 24 ms 44928 KB Output is correct
37 Correct 31 ms 45996 KB Output is correct
38 Correct 101 ms 52832 KB Output is correct
39 Correct 104 ms 52900 KB Output is correct
40 Correct 108 ms 53072 KB Output is correct
41 Correct 97 ms 52804 KB Output is correct
42 Correct 101 ms 52804 KB Output is correct
43 Correct 95 ms 52096 KB Output is correct
44 Correct 100 ms 52840 KB Output is correct
45 Correct 102 ms 53048 KB Output is correct
46 Correct 83 ms 56004 KB Output is correct
47 Correct 106 ms 52916 KB Output is correct
48 Correct 38 ms 46436 KB Output is correct
49 Correct 200 ms 57884 KB Output is correct
50 Correct 193 ms 57768 KB Output is correct
51 Correct 184 ms 57896 KB Output is correct
52 Correct 184 ms 57832 KB Output is correct
53 Correct 172 ms 57308 KB Output is correct
54 Correct 155 ms 56340 KB Output is correct
55 Correct 192 ms 58732 KB Output is correct
56 Correct 205 ms 57824 KB Output is correct
57 Correct 294 ms 60264 KB Output is correct
58 Correct 195 ms 58748 KB Output is correct
59 Correct 91 ms 49288 KB Output is correct
60 Correct 196 ms 55636 KB Output is correct
61 Correct 195 ms 55724 KB Output is correct
62 Correct 222 ms 56192 KB Output is correct
63 Correct 290 ms 58388 KB Output is correct
64 Correct 24 ms 44920 KB Output is correct
65 Correct 24 ms 44928 KB Output is correct
66 Correct 24 ms 45016 KB Output is correct
67 Correct 24 ms 45004 KB Output is correct
68 Correct 23 ms 44900 KB Output is correct
69 Correct 24 ms 44928 KB Output is correct
70 Correct 25 ms 45004 KB Output is correct
71 Correct 24 ms 44940 KB Output is correct
72 Correct 24 ms 45024 KB Output is correct
73 Correct 24 ms 45004 KB Output is correct
74 Correct 123 ms 54340 KB Output is correct
75 Correct 97 ms 52932 KB Output is correct
76 Correct 105 ms 53060 KB Output is correct
77 Correct 108 ms 54212 KB Output is correct
78 Correct 78 ms 50480 KB Output is correct
79 Correct 68 ms 49860 KB Output is correct
80 Correct 104 ms 53108 KB Output is correct
81 Correct 145 ms 54348 KB Output is correct
82 Correct 165 ms 56388 KB Output is correct
83 Correct 153 ms 55236 KB Output is correct
84 Correct 82 ms 55620 KB Output is correct
85 Correct 161 ms 56040 KB Output is correct
86 Correct 81 ms 49744 KB Output is correct
87 Correct 190 ms 57840 KB Output is correct
88 Correct 189 ms 57808 KB Output is correct
89 Correct 266 ms 59008 KB Output is correct
90 Correct 152 ms 55740 KB Output is correct
91 Correct 160 ms 56284 KB Output is correct
92 Correct 220 ms 58948 KB Output is correct
93 Correct 223 ms 60632 KB Output is correct
94 Correct 300 ms 62744 KB Output is correct
95 Correct 253 ms 61320 KB Output is correct
96 Correct 299 ms 60480 KB Output is correct
97 Correct 269 ms 61108 KB Output is correct