Submission #486510

# Submission time Handle Problem Language Result Execution time Memory
486510 2021-11-11T21:27:35 Z aryan12 Swapping Cities (APIO20_swap) C++17
43 / 100
2000 ms 48268 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];

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

    ufds(int x) {
        isLine = true;
        endpoints = {x, x};
        parent = x;
        value = 1e9;
        depth = 0;
    } 
} *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;
    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 init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    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;
            }
        }
    }
}

bool Check(int val, int X, int Y) {
    for(int i = 19; i >= 0; i--) {
        if(DP[i][X] != -1 && Node[DP[i][X]]->value <= val) {
            X = DP[i][X];
        }
        if(DP[i][Y] != -1 && Node[DP[i][Y]]->value <= val) {
            Y = DP[i][Y];
        }
    }
    return (X == Y && Node[X]->isLine == false);
}

int getMinimumFuelCapacity(int X, int Y) {
    int ans = 1e9 + 2;
    int l = 0, r = 1e9;
    while(l <= r) {
        int mid = (l + r) / 2;
        if(Check(mid, X, Y)) {
            r = mid - 1;
            ans = mid;
        }
        else {
            l = mid + 1;
        }
    }
    if(ans == 1e9 + 2) {
        ans = -1;
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37788 KB Output is correct
2 Correct 18 ms 37776 KB Output is correct
3 Correct 18 ms 37836 KB Output is correct
4 Correct 18 ms 37756 KB Output is correct
5 Correct 19 ms 37828 KB Output is correct
6 Correct 20 ms 37840 KB Output is correct
7 Correct 19 ms 37836 KB Output is correct
8 Correct 19 ms 37844 KB Output is correct
9 Correct 60 ms 41324 KB Output is correct
10 Correct 74 ms 42052 KB Output is correct
11 Correct 71 ms 41992 KB Output is correct
12 Correct 84 ms 42240 KB Output is correct
13 Correct 68 ms 42820 KB Output is correct
14 Correct 117 ms 41708 KB Output is correct
15 Correct 680 ms 46284 KB Output is correct
16 Correct 662 ms 46196 KB Output is correct
17 Correct 682 ms 46316 KB Output is correct
18 Correct 1263 ms 46848 KB Output is correct
19 Correct 395 ms 43632 KB Output is correct
20 Correct 653 ms 47204 KB Output is correct
21 Correct 706 ms 47608 KB Output is correct
22 Correct 708 ms 47732 KB Output is correct
23 Correct 1241 ms 48268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37788 KB Output is correct
2 Correct 18 ms 37776 KB Output is correct
3 Execution timed out 2078 ms 41588 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37788 KB Output is correct
2 Correct 18 ms 37776 KB Output is correct
3 Correct 18 ms 37836 KB Output is correct
4 Correct 18 ms 37756 KB Output is correct
5 Correct 19 ms 37828 KB Output is correct
6 Correct 20 ms 37840 KB Output is correct
7 Correct 19 ms 37836 KB Output is correct
8 Correct 19 ms 37844 KB Output is correct
9 Correct 19 ms 37880 KB Output is correct
10 Correct 19 ms 37888 KB Output is correct
11 Correct 18 ms 37796 KB Output is correct
12 Correct 19 ms 37892 KB Output is correct
13 Correct 20 ms 37868 KB Output is correct
14 Correct 20 ms 37872 KB Output is correct
15 Correct 19 ms 37836 KB Output is correct
16 Correct 19 ms 37852 KB Output is correct
17 Correct 19 ms 37836 KB Output is correct
18 Correct 22 ms 37836 KB Output is correct
19 Correct 19 ms 37840 KB Output is correct
20 Correct 19 ms 37828 KB Output is correct
21 Correct 19 ms 37892 KB Output is correct
22 Correct 19 ms 37936 KB Output is correct
23 Correct 19 ms 37864 KB Output is correct
24 Correct 19 ms 37848 KB Output is correct
25 Correct 19 ms 37892 KB Output is correct
26 Correct 19 ms 37836 KB Output is correct
27 Correct 20 ms 37852 KB Output is correct
28 Correct 19 ms 37912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37880 KB Output is correct
2 Correct 19 ms 37788 KB Output is correct
3 Correct 18 ms 37776 KB Output is correct
4 Correct 18 ms 37836 KB Output is correct
5 Correct 18 ms 37756 KB Output is correct
6 Correct 19 ms 37828 KB Output is correct
7 Correct 20 ms 37840 KB Output is correct
8 Correct 19 ms 37836 KB Output is correct
9 Correct 19 ms 37844 KB Output is correct
10 Correct 60 ms 41324 KB Output is correct
11 Correct 74 ms 42052 KB Output is correct
12 Correct 71 ms 41992 KB Output is correct
13 Correct 84 ms 42240 KB Output is correct
14 Correct 68 ms 42820 KB Output is correct
15 Correct 19 ms 37888 KB Output is correct
16 Correct 18 ms 37796 KB Output is correct
17 Correct 19 ms 37892 KB Output is correct
18 Correct 20 ms 37868 KB Output is correct
19 Correct 20 ms 37872 KB Output is correct
20 Correct 19 ms 37836 KB Output is correct
21 Correct 19 ms 37852 KB Output is correct
22 Correct 19 ms 37836 KB Output is correct
23 Correct 22 ms 37836 KB Output is correct
24 Correct 19 ms 37840 KB Output is correct
25 Correct 19 ms 37828 KB Output is correct
26 Correct 19 ms 37892 KB Output is correct
27 Correct 19 ms 37936 KB Output is correct
28 Correct 19 ms 37864 KB Output is correct
29 Correct 19 ms 37848 KB Output is correct
30 Correct 19 ms 37892 KB Output is correct
31 Correct 19 ms 37836 KB Output is correct
32 Correct 20 ms 37852 KB Output is correct
33 Correct 19 ms 37912 KB Output is correct
34 Correct 25 ms 38420 KB Output is correct
35 Correct 73 ms 41940 KB Output is correct
36 Correct 75 ms 41944 KB Output is correct
37 Correct 73 ms 41956 KB Output is correct
38 Correct 72 ms 41988 KB Output is correct
39 Correct 74 ms 41920 KB Output is correct
40 Correct 69 ms 41520 KB Output is correct
41 Correct 75 ms 42224 KB Output is correct
42 Correct 74 ms 41952 KB Output is correct
43 Correct 66 ms 41924 KB Output is correct
44 Correct 78 ms 42216 KB Output is correct
45 Correct 104 ms 44356 KB Output is correct
46 Correct 71 ms 41924 KB Output is correct
47 Correct 73 ms 41948 KB Output is correct
48 Correct 87 ms 42476 KB Output is correct
49 Correct 75 ms 44264 KB Output is correct
50 Correct 67 ms 42964 KB Output is correct
51 Correct 91 ms 43560 KB Output is correct
52 Correct 112 ms 44996 KB Output is correct
53 Correct 136 ms 45900 KB Output is correct
54 Correct 134 ms 46504 KB Output is correct
55 Correct 68 ms 41924 KB Output is correct
56 Correct 131 ms 45532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37788 KB Output is correct
2 Correct 18 ms 37776 KB Output is correct
3 Correct 18 ms 37836 KB Output is correct
4 Correct 18 ms 37756 KB Output is correct
5 Correct 19 ms 37828 KB Output is correct
6 Correct 20 ms 37840 KB Output is correct
7 Correct 19 ms 37836 KB Output is correct
8 Correct 19 ms 37844 KB Output is correct
9 Correct 60 ms 41324 KB Output is correct
10 Correct 74 ms 42052 KB Output is correct
11 Correct 71 ms 41992 KB Output is correct
12 Correct 84 ms 42240 KB Output is correct
13 Correct 68 ms 42820 KB Output is correct
14 Correct 117 ms 41708 KB Output is correct
15 Correct 680 ms 46284 KB Output is correct
16 Correct 662 ms 46196 KB Output is correct
17 Correct 682 ms 46316 KB Output is correct
18 Correct 1263 ms 46848 KB Output is correct
19 Execution timed out 2078 ms 41588 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 19 ms 37880 KB Output is correct
2 Correct 19 ms 37788 KB Output is correct
3 Correct 18 ms 37776 KB Output is correct
4 Correct 18 ms 37836 KB Output is correct
5 Correct 18 ms 37756 KB Output is correct
6 Correct 19 ms 37828 KB Output is correct
7 Correct 20 ms 37840 KB Output is correct
8 Correct 19 ms 37836 KB Output is correct
9 Correct 19 ms 37844 KB Output is correct
10 Correct 60 ms 41324 KB Output is correct
11 Correct 74 ms 42052 KB Output is correct
12 Correct 71 ms 41992 KB Output is correct
13 Correct 84 ms 42240 KB Output is correct
14 Correct 68 ms 42820 KB Output is correct
15 Correct 117 ms 41708 KB Output is correct
16 Correct 680 ms 46284 KB Output is correct
17 Correct 662 ms 46196 KB Output is correct
18 Correct 682 ms 46316 KB Output is correct
19 Correct 1263 ms 46848 KB Output is correct
20 Execution timed out 2078 ms 41588 KB Time limit exceeded
21 Halted 0 ms 0 KB -