답안 #1042094

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1042094 2024-08-02T14:23:35 Z RecursiveCo 자매 도시 (APIO20_swap) C++17
50 / 100
169 ms 29232 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);
        }
        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<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: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;
      |         ^~~
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 62 ms 21476 KB Output is correct
10 Correct 69 ms 25516 KB Output is correct
11 Correct 73 ms 24492 KB Output is correct
12 Correct 79 ms 25884 KB Output is correct
13 Correct 45 ms 18340 KB Output is correct
14 Correct 61 ms 20912 KB Output is correct
15 Correct 98 ms 27568 KB Output is correct
16 Correct 101 ms 26032 KB Output is correct
17 Correct 119 ms 28108 KB Output is correct
18 Correct 88 ms 19380 KB Output is correct
19 Correct 75 ms 7628 KB Output is correct
20 Correct 153 ms 27444 KB Output is correct
21 Correct 155 ms 27500 KB Output is correct
22 Correct 154 ms 28440 KB Output is correct
23 Correct 101 ms 20204 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 83 ms 19544 KB Output is correct
4 Correct 83 ms 19732 KB Output is correct
5 Correct 87 ms 20040 KB Output is correct
6 Correct 83 ms 19644 KB Output is correct
7 Correct 84 ms 19396 KB Output is correct
8 Correct 81 ms 18940 KB Output is correct
9 Correct 82 ms 19248 KB Output is correct
10 Correct 84 ms 19052 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 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 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 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 348 KB Output is correct
24 Correct 1 ms 600 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
# 결과 실행 시간 메모리 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 348 KB Output is correct
6 Correct 1 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 0 ms 604 KB Output is correct
10 Correct 62 ms 21476 KB Output is correct
11 Correct 69 ms 25516 KB Output is correct
12 Correct 73 ms 24492 KB Output is correct
13 Correct 79 ms 25884 KB Output is correct
14 Correct 45 ms 18340 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 0 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 604 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 0 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 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 348 KB Output is correct
29 Correct 1 ms 600 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 8 ms 3308 KB Output is correct
35 Correct 80 ms 26076 KB Output is correct
36 Correct 73 ms 25564 KB Output is correct
37 Correct 75 ms 24752 KB Output is correct
38 Correct 66 ms 23988 KB Output is correct
39 Correct 68 ms 23480 KB Output is correct
40 Correct 60 ms 21680 KB Output is correct
41 Correct 73 ms 26284 KB Output is correct
42 Correct 73 ms 25964 KB Output is correct
43 Correct 46 ms 18096 KB Output is correct
44 Correct 67 ms 22644 KB Output is correct
45 Correct 60 ms 17840 KB Output is correct
46 Correct 72 ms 25716 KB Output is correct
47 Correct 63 ms 21428 KB Output is correct
48 Correct 67 ms 18864 KB Output is correct
49 Correct 43 ms 7400 KB Output is correct
50 Correct 29 ms 5636 KB Output is correct
51 Correct 53 ms 14032 KB Output is correct
52 Correct 91 ms 26672 KB Output is correct
53 Correct 85 ms 22576 KB Output is correct
54 Correct 105 ms 29232 KB Output is correct
55 Correct 45 ms 18512 KB Output is correct
56 Correct 74 ms 21844 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 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 62 ms 21476 KB Output is correct
10 Correct 69 ms 25516 KB Output is correct
11 Correct 73 ms 24492 KB Output is correct
12 Correct 79 ms 25884 KB Output is correct
13 Correct 45 ms 18340 KB Output is correct
14 Correct 61 ms 20912 KB Output is correct
15 Correct 98 ms 27568 KB Output is correct
16 Correct 101 ms 26032 KB Output is correct
17 Correct 119 ms 28108 KB Output is correct
18 Correct 88 ms 19380 KB Output is correct
19 Correct 83 ms 19544 KB Output is correct
20 Correct 83 ms 19732 KB Output is correct
21 Correct 87 ms 20040 KB Output is correct
22 Correct 83 ms 19644 KB Output is correct
23 Correct 84 ms 19396 KB Output is correct
24 Correct 81 ms 18940 KB Output is correct
25 Correct 82 ms 19248 KB Output is correct
26 Correct 84 ms 19052 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 0 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 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 8 ms 3308 KB Output is correct
37 Correct 80 ms 26076 KB Output is correct
38 Correct 73 ms 25564 KB Output is correct
39 Correct 75 ms 24752 KB Output is correct
40 Correct 66 ms 23988 KB Output is correct
41 Correct 68 ms 23480 KB Output is correct
42 Correct 60 ms 21680 KB Output is correct
43 Correct 73 ms 26284 KB Output is correct
44 Correct 73 ms 25964 KB Output is correct
45 Correct 46 ms 18096 KB Output is correct
46 Correct 67 ms 22644 KB Output is correct
47 Correct 14 ms 3536 KB Output is correct
48 Correct 154 ms 28080 KB Output is correct
49 Correct 153 ms 26904 KB Output is correct
50 Correct 149 ms 27156 KB Output is correct
51 Correct 145 ms 25892 KB Output is correct
52 Correct 141 ms 24160 KB Output is correct
53 Correct 113 ms 18740 KB Output is correct
54 Correct 157 ms 28412 KB Output is correct
55 Correct 169 ms 27932 KB Output is correct
56 Incorrect 100 ms 19988 KB Output isn't correct
57 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 348 KB Output is correct
6 Correct 1 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 0 ms 604 KB Output is correct
10 Correct 62 ms 21476 KB Output is correct
11 Correct 69 ms 25516 KB Output is correct
12 Correct 73 ms 24492 KB Output is correct
13 Correct 79 ms 25884 KB Output is correct
14 Correct 45 ms 18340 KB Output is correct
15 Correct 61 ms 20912 KB Output is correct
16 Correct 98 ms 27568 KB Output is correct
17 Correct 101 ms 26032 KB Output is correct
18 Correct 119 ms 28108 KB Output is correct
19 Correct 88 ms 19380 KB Output is correct
20 Correct 83 ms 19544 KB Output is correct
21 Correct 83 ms 19732 KB Output is correct
22 Correct 87 ms 20040 KB Output is correct
23 Correct 83 ms 19644 KB Output is correct
24 Correct 84 ms 19396 KB Output is correct
25 Correct 81 ms 18940 KB Output is correct
26 Correct 82 ms 19248 KB Output is correct
27 Correct 84 ms 19052 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 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 0 ms 604 KB Output is correct
35 Correct 0 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 8 ms 3308 KB Output is correct
38 Correct 80 ms 26076 KB Output is correct
39 Correct 73 ms 25564 KB Output is correct
40 Correct 75 ms 24752 KB Output is correct
41 Correct 66 ms 23988 KB Output is correct
42 Correct 68 ms 23480 KB Output is correct
43 Correct 60 ms 21680 KB Output is correct
44 Correct 73 ms 26284 KB Output is correct
45 Correct 73 ms 25964 KB Output is correct
46 Correct 46 ms 18096 KB Output is correct
47 Correct 67 ms 22644 KB Output is correct
48 Correct 14 ms 3536 KB Output is correct
49 Correct 154 ms 28080 KB Output is correct
50 Correct 153 ms 26904 KB Output is correct
51 Correct 149 ms 27156 KB Output is correct
52 Correct 145 ms 25892 KB Output is correct
53 Correct 141 ms 24160 KB Output is correct
54 Correct 113 ms 18740 KB Output is correct
55 Correct 157 ms 28412 KB Output is correct
56 Correct 169 ms 27932 KB Output is correct
57 Incorrect 100 ms 19988 KB Output isn't correct
58 Halted 0 ms 0 KB -