Submission #1042103

# Submission time Handle Problem Language Result Execution time Memory
1042103 2024-08-02T14:37:29 Z RecursiveCo Swapping Cities (APIO20_swap) C++17
100 / 100
206 ms 44724 KB
// CF template, version 3.0

#include <bits/stdc++.h>

using namespace std;

#define improvePerformance ios_base::sync_with_stdio(false); cin.tie(0)
#define getTest int t; cin >> t
#define eachTest for (int _var=0;_var<t;_var++)
#define get(name) int (name); cin >> (name)
#define out(o) cout << (o)
#define getList(cnt, name) vector<int> (name); for (int _=0;_<(cnt);_++) { get(a); (name).push_back(a); }
#define sortl(name) sort((name).begin(), (name).end())
#define rev(name) reverse((name).begin(), (name).end())
#define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
#define decision(b) if (b){out("YES");}else{out("NO");}

//#define int long long int

template <typename T, typename I>
struct segtree {
    int n;
    vector<T> tree;
    vector<I> initial;
    T id;

    segtree(int i_n, vector<I> i_initial, T i_id): n(i_n), initial(i_initial), id(i_id) {
        tree.resize(4 * n);
    }

    T conquer(T left, T right) {
        // write your conquer function
    }

    T value(I inp) {
        // write your value function
    }

    void build(int v, int l, int r) {
        if (l == r) tree[v] = value(initial[l]);
        else {
            int middle = (l + r) / 2;
            build(2 * v, l, middle);
            build(2 * v + 1, middle + 1, r);
            tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
        }
    }

    void upd(int v, int l, int r, int i, I x) {
        if (l == r) tree[v] = value(x);
        else {
            int middle = (l + r) / 2;
            if (middle >= i) upd(2 * v, l, middle, i, x);
            else upd(2 * v + 1, middle + 1, r, i, x);
            tree[v] = conquer(tree[2 * v], tree[2 * v + 1]);
        }
    }

    T query(int v, int l, int r, int ql, int qr) {
        if (ql <= l && r <= qr) return tree[v];
        else if (r < ql || qr < l) return id;
        int middle = (l + r) / 2;
        T left = query(2 * v, l, middle, ql, qr);
        T right = query(2 * v + 1, middle + 1, r, ql, qr);
        return conquer(left, right);
    }
};

// vector<int>

vector<int> parent;
vector<vector<int>> comps;
vector<int> deg;
vector<int> maxdeg;
vector<int> edges;
vector<bool> marked;
vector<int> nonline;
vector<vector<array<int, 4>>> merges;

int find(int v) {
    if (v == parent[v]) return v;
    return parent[v] = find(parent[v]);
}

void unite(int u, int v, int val) {
    int a = find(u);
    int b = find(v);
    deg[u]++;
    deg[v]++;
    if (a == b) {
        maxdeg[a] = max({maxdeg[a], deg[u], deg[v]});
        edges[a]++;
        if (!marked[a] && (maxdeg[a] >= 3 || edges[a] == comps[a].size())) {
            marked[a] = true;
            for (int node: comps[a]) nonline[node] = min(nonline[node], val);
        }
    } else {
        if (comps[a].size() > comps[b].size()) swap(a, b);
        maxdeg[b] = max({maxdeg[b], deg[u], deg[v]});
        if (marked[a] && !marked[b]) {
            marked[b] = true;
            for (int node: comps[b]) nonline[node] = min(nonline[node], val);
        }
        while (!comps[a].empty()) {
            int node = comps[a].back();
            comps[a].pop_back();
            comps[b].push_back(node);
            merges[node].push_back({val, u, v, b});
            maxdeg[b] = max(maxdeg[b], deg[node]);
            if (marked[b] && !marked[a]) nonline[node] = min(nonline[node], val);
        }
        edges[b] += edges[a] + 1;
        if (!marked[a] && !marked[b] && (maxdeg[b] >= 3 || edges[b] == comps[b].size())) {
            marked[b] = true;
            for (int node: comps[b]) nonline[node] = min(nonline[node], val);
        }
        parent[a] = b;
    }
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
    forto(N, i) {
        parent.push_back(i);

        vector<int> here1;
        here1.push_back(i);
        comps.push_back(here1);

        deg.push_back(0);

        maxdeg.push_back(0);

        edges.push_back(0);

        marked.push_back(false);

        nonline.push_back(1e9 + 5);

        vector<array<int, 4>> here2;
        merges.push_back(here2);
    }

    vector<array<int, 3>> sorted;
    forto(M, i) sorted.push_back({W[i], U[i], V[i]});
    sortl(sorted);
    forto(M, i) {
        int w = sorted[i][0];
        int u = sorted[i][1];
        int v = sorted[i][2];
        unite(u, v, w);
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    int one = nonline[X];
    if (one == 1e9 + 5) return -1;
    vector<array<int, 5>> events;
    for (array<int, 4> event: merges[X]) events.push_back({event[0], event[1], event[2], event[3], 0});
    for (array<int, 4> event: merges[Y]) events.push_back({event[0], event[1], event[2], event[3], 1});
    sortl(events);
    int parX = X;
    int parY = Y;
    int two;
    for (array<int, 5> event: events) {
        if (event[4] == 0) parX = event[3];
        else parY = event[3];
        if (parX == parY) {
            two = event[0];
            break;
        }
    }
    return max(one, two);
}

Compilation message

swap.cpp: In function 'void unite(int, int, int)':
swap.cpp:93:55: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         if (!marked[a] && (maxdeg[a] >= 3 || edges[a] == comps[a].size())) {
swap.cpp:113:69: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |         if (!marked[a] && !marked[b] && (maxdeg[b] >= 3 || edges[b] == comps[b].size())) {
swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
swap.cpp:122:5: note: in expansion of macro 'forto'
  122 |     forto(N, i) {
      |     ^~~~~
swap.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
swap.cpp:144:5: note: in expansion of macro 'forto'
  144 |     forto(M, i) sorted.push_back({W[i], U[i], V[i]});
      |     ^~~~~
swap.cpp:15:35: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   15 | #define forto(name, var) for (int (var) = 0; (var) < (name); (var)++)
      |                                   ^
swap.cpp:146:5: note: in expansion of macro 'forto'
  146 |     forto(M, i) {
      |     ^~~~~
swap.cpp: In function 'int getMinimumFuelCapacity(int, int)':
swap.cpp:163:9: warning: 'two' may be used uninitialized in this function [-Wmaybe-uninitialized]
  163 |     int two;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 66 ms 26592 KB Output is correct
10 Correct 81 ms 31364 KB Output is correct
11 Correct 76 ms 30384 KB Output is correct
12 Correct 82 ms 32684 KB Output is correct
13 Correct 50 ms 18900 KB Output is correct
14 Correct 61 ms 26544 KB Output is correct
15 Correct 106 ms 33436 KB Output is correct
16 Correct 100 ms 32128 KB Output is correct
17 Correct 106 ms 34736 KB Output is correct
18 Correct 79 ms 20912 KB Output is correct
19 Correct 71 ms 8396 KB Output is correct
20 Correct 170 ms 33860 KB Output is correct
21 Correct 155 ms 33132 KB Output is correct
22 Correct 172 ms 34996 KB Output is correct
23 Correct 99 ms 21444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 84 ms 18968 KB Output is correct
4 Correct 86 ms 19480 KB Output is correct
5 Correct 86 ms 19560 KB Output is correct
6 Correct 95 ms 19224 KB Output is correct
7 Correct 86 ms 19216 KB Output is correct
8 Correct 80 ms 19036 KB Output is correct
9 Correct 81 ms 19500 KB Output is correct
10 Correct 85 ms 19304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 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 0 ms 604 KB Output is correct
11 Correct 1 ms 564 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 0 ms 620 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 66 ms 26592 KB Output is correct
11 Correct 81 ms 31364 KB Output is correct
12 Correct 76 ms 30384 KB Output is correct
13 Correct 82 ms 32684 KB Output is correct
14 Correct 50 ms 18900 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 1 ms 564 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 1 ms 604 KB Output is correct
22 Correct 0 ms 620 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 0 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 1 ms 604 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 604 KB Output is correct
34 Correct 9 ms 3796 KB Output is correct
35 Correct 98 ms 32172 KB Output is correct
36 Correct 76 ms 31732 KB Output is correct
37 Correct 82 ms 31172 KB Output is correct
38 Correct 76 ms 29872 KB Output is correct
39 Correct 69 ms 28592 KB Output is correct
40 Correct 66 ms 25776 KB Output is correct
41 Correct 90 ms 33200 KB Output is correct
42 Correct 92 ms 33196 KB Output is correct
43 Correct 49 ms 19120 KB Output is correct
44 Correct 73 ms 27828 KB Output is correct
45 Correct 59 ms 20396 KB Output is correct
46 Correct 83 ms 32552 KB Output is correct
47 Correct 67 ms 25620 KB Output is correct
48 Correct 58 ms 20152 KB Output is correct
49 Correct 36 ms 8328 KB Output is correct
50 Correct 29 ms 5860 KB Output is correct
51 Correct 53 ms 14904 KB Output is correct
52 Correct 96 ms 32244 KB Output is correct
53 Correct 84 ms 25384 KB Output is correct
54 Correct 102 ms 36796 KB Output is correct
55 Correct 50 ms 18860 KB Output is correct
56 Correct 79 ms 22824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 66 ms 26592 KB Output is correct
10 Correct 81 ms 31364 KB Output is correct
11 Correct 76 ms 30384 KB Output is correct
12 Correct 82 ms 32684 KB Output is correct
13 Correct 50 ms 18900 KB Output is correct
14 Correct 61 ms 26544 KB Output is correct
15 Correct 106 ms 33436 KB Output is correct
16 Correct 100 ms 32128 KB Output is correct
17 Correct 106 ms 34736 KB Output is correct
18 Correct 79 ms 20912 KB Output is correct
19 Correct 84 ms 18968 KB Output is correct
20 Correct 86 ms 19480 KB Output is correct
21 Correct 86 ms 19560 KB Output is correct
22 Correct 95 ms 19224 KB Output is correct
23 Correct 86 ms 19216 KB Output is correct
24 Correct 80 ms 19036 KB Output is correct
25 Correct 81 ms 19500 KB Output is correct
26 Correct 85 ms 19304 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 564 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 0 ms 620 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 9 ms 3796 KB Output is correct
37 Correct 98 ms 32172 KB Output is correct
38 Correct 76 ms 31732 KB Output is correct
39 Correct 82 ms 31172 KB Output is correct
40 Correct 76 ms 29872 KB Output is correct
41 Correct 69 ms 28592 KB Output is correct
42 Correct 66 ms 25776 KB Output is correct
43 Correct 90 ms 33200 KB Output is correct
44 Correct 92 ms 33196 KB Output is correct
45 Correct 49 ms 19120 KB Output is correct
46 Correct 73 ms 27828 KB Output is correct
47 Correct 15 ms 4048 KB Output is correct
48 Correct 175 ms 34820 KB Output is correct
49 Correct 157 ms 33744 KB Output is correct
50 Correct 170 ms 32984 KB Output is correct
51 Correct 154 ms 32036 KB Output is correct
52 Correct 143 ms 28948 KB Output is correct
53 Correct 118 ms 21052 KB Output is correct
54 Correct 162 ms 34584 KB Output is correct
55 Correct 164 ms 35348 KB Output is correct
56 Correct 98 ms 20764 KB Output is correct
57 Correct 150 ms 33184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 66 ms 26592 KB Output is correct
11 Correct 81 ms 31364 KB Output is correct
12 Correct 76 ms 30384 KB Output is correct
13 Correct 82 ms 32684 KB Output is correct
14 Correct 50 ms 18900 KB Output is correct
15 Correct 61 ms 26544 KB Output is correct
16 Correct 106 ms 33436 KB Output is correct
17 Correct 100 ms 32128 KB Output is correct
18 Correct 106 ms 34736 KB Output is correct
19 Correct 79 ms 20912 KB Output is correct
20 Correct 84 ms 18968 KB Output is correct
21 Correct 86 ms 19480 KB Output is correct
22 Correct 86 ms 19560 KB Output is correct
23 Correct 95 ms 19224 KB Output is correct
24 Correct 86 ms 19216 KB Output is correct
25 Correct 80 ms 19036 KB Output is correct
26 Correct 81 ms 19500 KB Output is correct
27 Correct 85 ms 19304 KB Output is correct
28 Correct 0 ms 604 KB Output is correct
29 Correct 1 ms 564 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 0 ms 620 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 9 ms 3796 KB Output is correct
38 Correct 98 ms 32172 KB Output is correct
39 Correct 76 ms 31732 KB Output is correct
40 Correct 82 ms 31172 KB Output is correct
41 Correct 76 ms 29872 KB Output is correct
42 Correct 69 ms 28592 KB Output is correct
43 Correct 66 ms 25776 KB Output is correct
44 Correct 90 ms 33200 KB Output is correct
45 Correct 92 ms 33196 KB Output is correct
46 Correct 49 ms 19120 KB Output is correct
47 Correct 73 ms 27828 KB Output is correct
48 Correct 15 ms 4048 KB Output is correct
49 Correct 175 ms 34820 KB Output is correct
50 Correct 157 ms 33744 KB Output is correct
51 Correct 170 ms 32984 KB Output is correct
52 Correct 154 ms 32036 KB Output is correct
53 Correct 143 ms 28948 KB Output is correct
54 Correct 118 ms 21052 KB Output is correct
55 Correct 162 ms 34584 KB Output is correct
56 Correct 164 ms 35348 KB Output is correct
57 Correct 98 ms 20764 KB Output is correct
58 Correct 150 ms 33184 KB Output is correct
59 Correct 71 ms 8396 KB Output is correct
60 Correct 170 ms 33860 KB Output is correct
61 Correct 155 ms 33132 KB Output is correct
62 Correct 172 ms 34996 KB Output is correct
63 Correct 99 ms 21444 KB Output is correct
64 Correct 0 ms 604 KB Output is correct
65 Correct 1 ms 604 KB Output is correct
66 Correct 1 ms 604 KB Output is correct
67 Correct 1 ms 348 KB Output is correct
68 Correct 0 ms 604 KB Output is correct
69 Correct 1 ms 604 KB Output is correct
70 Correct 1 ms 604 KB Output is correct
71 Correct 1 ms 604 KB Output is correct
72 Correct 1 ms 604 KB Output is correct
73 Correct 1 ms 604 KB Output is correct
74 Correct 59 ms 20396 KB Output is correct
75 Correct 83 ms 32552 KB Output is correct
76 Correct 67 ms 25620 KB Output is correct
77 Correct 58 ms 20152 KB Output is correct
78 Correct 36 ms 8328 KB Output is correct
79 Correct 29 ms 5860 KB Output is correct
80 Correct 53 ms 14904 KB Output is correct
81 Correct 96 ms 32244 KB Output is correct
82 Correct 84 ms 25384 KB Output is correct
83 Correct 102 ms 36796 KB Output is correct
84 Correct 50 ms 18860 KB Output is correct
85 Correct 79 ms 22824 KB Output is correct
86 Correct 38 ms 9028 KB Output is correct
87 Correct 163 ms 37016 KB Output is correct
88 Correct 154 ms 36764 KB Output is correct
89 Correct 107 ms 25040 KB Output is correct
90 Correct 89 ms 14032 KB Output is correct
91 Correct 88 ms 15652 KB Output is correct
92 Correct 110 ms 21308 KB Output is correct
93 Correct 175 ms 38636 KB Output is correct
94 Correct 156 ms 33176 KB Output is correct
95 Correct 206 ms 44724 KB Output is correct
96 Correct 98 ms 24864 KB Output is correct
97 Correct 122 ms 29496 KB Output is correct