Submission #704668

# Submission time Handle Problem Language Result Execution time Memory
704668 2023-03-02T15:17:35 Z piOOE Reconstruction Project (JOI22_reconstruction) C++17
42 / 100
5000 ms 223252 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

constexpr int Q = 1e5 + 1, N = 500;

vector<int> save[Q];
array<int, 3> e[Q];

int fa[N], sz[N];

void init() {
    iota(fa, fa + N, 0);
    fill_n(sz, N, 1);
}

int leader(int x) {
    return x == fa[x] ? x : fa[x] = leader(fa[x]);
}

bool unite(int x, int y) {
    x = leader(x), y = leader(y);

    if (x == y) {
        return false;
    }

    if (sz[x] < sz[y]) {
        swap(x, y);
    }

    fa[y] = x;
    sz[x] += sz[y];

    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; ++i) {
        cin >> e[i][1] >> e[i][2] >> e[i][0];
        e[i][1] -= 1, e[i][2] -= 1;
    }

    sort(e, e + m);

    int q;
    cin >> q;

    vector<int> X(q);
    vector<ll> ans(q);

    for (int &x : X) {
        cin >> x;
    }

    constexpr int inf = 2e9;

    for (int t = 0; t < 2; ++t) {
        vector<int> mst;

        int pnt = 0;

        for (int i = 0; i <= m; ++i) {
            if (i > 0) {
                mst.insert(mst.begin(), (t == 0 ? m - i : i - 1));
                init();

                for (int ii = 0; ii < mst.size(); ++ii) {
                    if (!unite(e[mst[ii]][1], e[mst[ii]][2])) {
                        mst.erase(mst.begin() + ii);
                        break;
                    }
                }

                if (t == 0) {
                    save[m - i] = mst;
                }
            }

            if (t == 1) {
                vector<int> oth = save[i];

                while (pnt < q && X[pnt] < (i == m ? inf : e[i][0])) {
                    init();

                    int L = 0, R = 0, cnt = 0;

                    while (cnt < n - 1) {
                        int weightL = L == mst.size() ? inf : X[pnt] - e[mst[L]][0];
                        int weightR = R == oth.size() ? inf : e[oth[R]][0] - X[pnt];

                        if (weightL < weightR) {
                            if (unite(e[mst[L]][1], e[mst[L]][2])) {
                                ans[pnt] += weightL;
                                cnt += 1;
                            }

                            L += 1;
                        } else {
                            if (unite(e[oth[R]][1], e[oth[R]][2])) {
                                ans[pnt] += weightR;
                                cnt += 1;
                            }

                            R += 1;
                        }
                    }

                    pnt += 1;
                }
            }
        }
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << '\n';
    }

    return 0;
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:75:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |                 for (int ii = 0; ii < mst.size(); ++ii) {
      |                                  ~~~^~~~~~~~~~~~
reconstruction.cpp:96:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |                         int weightL = L == mst.size() ? inf : X[pnt] - e[mst[L]][0];
      |                                       ~~^~~~~~~~~~~~~
reconstruction.cpp:97:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |                         int weightR = R == oth.size() ? inf : e[oth[R]][0] - X[pnt];
      |                                       ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2676 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2676 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2676 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2676 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2628 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1418 ms 202340 KB Output is correct
21 Correct 780 ms 191768 KB Output is correct
22 Correct 791 ms 199912 KB Output is correct
23 Correct 888 ms 201644 KB Output is correct
24 Correct 1108 ms 202212 KB Output is correct
25 Correct 1333 ms 202148 KB Output is correct
26 Correct 1398 ms 202232 KB Output is correct
27 Correct 1396 ms 202164 KB Output is correct
28 Correct 1423 ms 202316 KB Output is correct
29 Correct 701 ms 195548 KB Output is correct
30 Correct 1404 ms 202196 KB Output is correct
31 Correct 1391 ms 202332 KB Output is correct
32 Correct 1391 ms 202264 KB Output is correct
33 Correct 1396 ms 202248 KB Output is correct
34 Correct 754 ms 137336 KB Output is correct
35 Correct 1403 ms 202204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2676 KB Output is correct
4 Execution timed out 5061 ms 223252 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2676 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2676 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2628 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Execution timed out 5034 ms 25540 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2676 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2676 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2628 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1418 ms 202340 KB Output is correct
21 Correct 780 ms 191768 KB Output is correct
22 Correct 791 ms 199912 KB Output is correct
23 Correct 888 ms 201644 KB Output is correct
24 Correct 1108 ms 202212 KB Output is correct
25 Correct 1333 ms 202148 KB Output is correct
26 Correct 1398 ms 202232 KB Output is correct
27 Correct 1396 ms 202164 KB Output is correct
28 Correct 1423 ms 202316 KB Output is correct
29 Correct 701 ms 195548 KB Output is correct
30 Correct 1404 ms 202196 KB Output is correct
31 Correct 1391 ms 202332 KB Output is correct
32 Correct 1391 ms 202264 KB Output is correct
33 Correct 1396 ms 202248 KB Output is correct
34 Correct 754 ms 137336 KB Output is correct
35 Correct 1403 ms 202204 KB Output is correct
36 Correct 1990 ms 202788 KB Output is correct
37 Correct 1458 ms 193508 KB Output is correct
38 Correct 1492 ms 200336 KB Output is correct
39 Correct 1570 ms 202284 KB Output is correct
40 Correct 1826 ms 202632 KB Output is correct
41 Correct 1832 ms 202768 KB Output is correct
42 Correct 1913 ms 202888 KB Output is correct
43 Correct 1926 ms 202780 KB Output is correct
44 Correct 1753 ms 202812 KB Output is correct
45 Correct 906 ms 196228 KB Output is correct
46 Correct 1892 ms 202788 KB Output is correct
47 Correct 1926 ms 202792 KB Output is correct
48 Correct 1886 ms 202840 KB Output is correct
49 Correct 1899 ms 202812 KB Output is correct
50 Correct 907 ms 138056 KB Output is correct
51 Correct 1728 ms 202804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2676 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2676 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2676 KB Output is correct
17 Correct 2 ms 2648 KB Output is correct
18 Correct 2 ms 2628 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1418 ms 202340 KB Output is correct
21 Correct 780 ms 191768 KB Output is correct
22 Correct 791 ms 199912 KB Output is correct
23 Correct 888 ms 201644 KB Output is correct
24 Correct 1108 ms 202212 KB Output is correct
25 Correct 1333 ms 202148 KB Output is correct
26 Correct 1398 ms 202232 KB Output is correct
27 Correct 1396 ms 202164 KB Output is correct
28 Correct 1423 ms 202316 KB Output is correct
29 Correct 701 ms 195548 KB Output is correct
30 Correct 1404 ms 202196 KB Output is correct
31 Correct 1391 ms 202332 KB Output is correct
32 Correct 1391 ms 202264 KB Output is correct
33 Correct 1396 ms 202248 KB Output is correct
34 Correct 754 ms 137336 KB Output is correct
35 Correct 1403 ms 202204 KB Output is correct
36 Correct 2 ms 2644 KB Output is correct
37 Correct 2 ms 2644 KB Output is correct
38 Correct 2 ms 2676 KB Output is correct
39 Execution timed out 5061 ms 223252 KB Time limit exceeded
40 Halted 0 ms 0 KB -