답안 #1042091

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1042091 2024-08-02T14:21:59 Z RecursiveCo 자매 도시 (APIO20_swap) C++17
50 / 100
176 ms 29772 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<pair<int, int>>> 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, b});
            maxdeg[b] = max(maxdeg[b], deg[node]);
            if (marked[b] && !marked[a]) nonline[node] = min(nonline[node], val);
        }
        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;
        edges[b] += edges[a] + 1;
    }
}

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<pair<int, int>> 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, 3>> events;
    for (pair<int, int> event: merges[X]) events.push_back({event.first, event.second, 0});
    for (pair<int, int> event: merges[Y]) events.push_back({event.first, event.second, 1});
    sortl(events);
    int parX = X;
    int parY = Y;
    int two;
    for (array<int, 3> event: events) {
        if (event[2] == 0) parX = event[1];
        else parY = event[1];
        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:112: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]
  112 |         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;
      |         ^~~
# 결과 실행 시간 메모리 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 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 0 ms 604 KB Output is correct
9 Correct 61 ms 20868 KB Output is correct
10 Correct 74 ms 25520 KB Output is correct
11 Correct 69 ms 24220 KB Output is correct
12 Correct 79 ms 25776 KB Output is correct
13 Correct 47 ms 18072 KB Output is correct
14 Correct 61 ms 21172 KB Output is correct
15 Correct 100 ms 27052 KB Output is correct
16 Correct 103 ms 26084 KB Output is correct
17 Correct 104 ms 27564 KB Output is correct
18 Correct 77 ms 19376 KB Output is correct
19 Correct 66 ms 7644 KB Output is correct
20 Correct 157 ms 27952 KB Output is correct
21 Correct 156 ms 26988 KB Output is correct
22 Correct 176 ms 28468 KB Output is correct
23 Correct 99 ms 20756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 82 ms 18772 KB Output is correct
4 Correct 82 ms 19380 KB Output is correct
5 Correct 85 ms 19784 KB Output is correct
6 Correct 82 ms 19304 KB Output is correct
7 Correct 83 ms 19476 KB Output is correct
8 Correct 87 ms 19296 KB Output is correct
9 Correct 98 ms 18996 KB Output is correct
10 Correct 81 ms 19048 KB Output is correct
# 결과 실행 시간 메모리 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 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 0 ms 604 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 488 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 348 KB Output is correct
20 Correct 1 ms 612 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 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 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 0 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 0 ms 604 KB Output is correct
10 Correct 61 ms 20868 KB Output is correct
11 Correct 74 ms 25520 KB Output is correct
12 Correct 69 ms 24220 KB Output is correct
13 Correct 79 ms 25776 KB Output is correct
14 Correct 47 ms 18072 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 1 ms 488 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 612 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 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 600 KB Output is correct
34 Correct 9 ms 3284 KB Output is correct
35 Correct 78 ms 26316 KB Output is correct
36 Correct 77 ms 25636 KB Output is correct
37 Correct 71 ms 25004 KB Output is correct
38 Correct 75 ms 23980 KB Output is correct
39 Correct 68 ms 23216 KB Output is correct
40 Correct 62 ms 21676 KB Output is correct
41 Correct 80 ms 26540 KB Output is correct
42 Correct 73 ms 26280 KB Output is correct
43 Correct 54 ms 17844 KB Output is correct
44 Correct 68 ms 23072 KB Output is correct
45 Correct 61 ms 17368 KB Output is correct
46 Correct 71 ms 25516 KB Output is correct
47 Correct 67 ms 21932 KB Output is correct
48 Correct 56 ms 18684 KB Output is correct
49 Correct 37 ms 8924 KB Output is correct
50 Correct 30 ms 5636 KB Output is correct
51 Correct 58 ms 13888 KB Output is correct
52 Correct 89 ms 26672 KB Output is correct
53 Correct 84 ms 23596 KB Output is correct
54 Correct 102 ms 29772 KB Output is correct
55 Correct 48 ms 18352 KB Output is correct
56 Correct 75 ms 21556 KB Output is correct
# 결과 실행 시간 메모리 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 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 0 ms 604 KB Output is correct
9 Correct 61 ms 20868 KB Output is correct
10 Correct 74 ms 25520 KB Output is correct
11 Correct 69 ms 24220 KB Output is correct
12 Correct 79 ms 25776 KB Output is correct
13 Correct 47 ms 18072 KB Output is correct
14 Correct 61 ms 21172 KB Output is correct
15 Correct 100 ms 27052 KB Output is correct
16 Correct 103 ms 26084 KB Output is correct
17 Correct 104 ms 27564 KB Output is correct
18 Correct 77 ms 19376 KB Output is correct
19 Correct 82 ms 18772 KB Output is correct
20 Correct 82 ms 19380 KB Output is correct
21 Correct 85 ms 19784 KB Output is correct
22 Correct 82 ms 19304 KB Output is correct
23 Correct 83 ms 19476 KB Output is correct
24 Correct 87 ms 19296 KB Output is correct
25 Correct 98 ms 18996 KB Output is correct
26 Correct 81 ms 19048 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 488 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 9 ms 3284 KB Output is correct
37 Correct 78 ms 26316 KB Output is correct
38 Correct 77 ms 25636 KB Output is correct
39 Correct 71 ms 25004 KB Output is correct
40 Correct 75 ms 23980 KB Output is correct
41 Correct 68 ms 23216 KB Output is correct
42 Correct 62 ms 21676 KB Output is correct
43 Correct 80 ms 26540 KB Output is correct
44 Correct 73 ms 26280 KB Output is correct
45 Correct 54 ms 17844 KB Output is correct
46 Correct 68 ms 23072 KB Output is correct
47 Correct 14 ms 3540 KB Output is correct
48 Correct 162 ms 28176 KB Output is correct
49 Correct 150 ms 26936 KB Output is correct
50 Correct 150 ms 26904 KB Output is correct
51 Correct 148 ms 26188 KB Output is correct
52 Correct 135 ms 24156 KB Output is correct
53 Correct 112 ms 18600 KB Output is correct
54 Correct 158 ms 28440 KB Output is correct
55 Correct 158 ms 28276 KB Output is correct
56 Incorrect 98 ms 19736 KB Output isn't correct
57 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 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 0 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 0 ms 604 KB Output is correct
10 Correct 61 ms 20868 KB Output is correct
11 Correct 74 ms 25520 KB Output is correct
12 Correct 69 ms 24220 KB Output is correct
13 Correct 79 ms 25776 KB Output is correct
14 Correct 47 ms 18072 KB Output is correct
15 Correct 61 ms 21172 KB Output is correct
16 Correct 100 ms 27052 KB Output is correct
17 Correct 103 ms 26084 KB Output is correct
18 Correct 104 ms 27564 KB Output is correct
19 Correct 77 ms 19376 KB Output is correct
20 Correct 82 ms 18772 KB Output is correct
21 Correct 82 ms 19380 KB Output is correct
22 Correct 85 ms 19784 KB Output is correct
23 Correct 82 ms 19304 KB Output is correct
24 Correct 83 ms 19476 KB Output is correct
25 Correct 87 ms 19296 KB Output is correct
26 Correct 98 ms 18996 KB Output is correct
27 Correct 81 ms 19048 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 0 ms 604 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 1 ms 600 KB Output is correct
34 Correct 1 ms 488 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 9 ms 3284 KB Output is correct
38 Correct 78 ms 26316 KB Output is correct
39 Correct 77 ms 25636 KB Output is correct
40 Correct 71 ms 25004 KB Output is correct
41 Correct 75 ms 23980 KB Output is correct
42 Correct 68 ms 23216 KB Output is correct
43 Correct 62 ms 21676 KB Output is correct
44 Correct 80 ms 26540 KB Output is correct
45 Correct 73 ms 26280 KB Output is correct
46 Correct 54 ms 17844 KB Output is correct
47 Correct 68 ms 23072 KB Output is correct
48 Correct 14 ms 3540 KB Output is correct
49 Correct 162 ms 28176 KB Output is correct
50 Correct 150 ms 26936 KB Output is correct
51 Correct 150 ms 26904 KB Output is correct
52 Correct 148 ms 26188 KB Output is correct
53 Correct 135 ms 24156 KB Output is correct
54 Correct 112 ms 18600 KB Output is correct
55 Correct 158 ms 28440 KB Output is correct
56 Correct 158 ms 28276 KB Output is correct
57 Incorrect 98 ms 19736 KB Output isn't correct
58 Halted 0 ms 0 KB -