Submission #999478

# Submission time Handle Problem Language Result Execution time Memory
999478 2024-06-15T14:54:34 Z vladilius Reconstruction Project (JOI22_reconstruction) C++17
42 / 100
5000 ms 427608 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define arr3 array<int, 3>
const int inf = 1e9 + 5;

struct ST{
    vector<vector<pii>> g;
    vector<pii> ed;
    vector<int> gd;
    int n, m, A;
    bool type;
    ST(int ns, int ms){
        n = ns;
        m = ms;
        g.resize(n + 1);
        ed.resize(m + 1);
    }
    void set_mini(){
        A = m + 1;
        type = 1;
    }
    void set_maxi(){
        A = 0;
        type = 0;
    }
    int f(int x, int y){
        if (type){
            return min(x, y);
        }
        return max(x, y);
    }
    vector<pii> st;
    bool ind;
    void dfs(int v, int pr, int pre, int& t){
        // cout<<"дідько "<<v<<" "<<pr<<" "<<t<<" "<<"\n";
        if (ind) return;
        st.pb({v, pre});
        if (v == t){
            ind = 1;
            return;
        }
        for (auto [i, j]: g[v]){
            if (i == pr) continue;
            dfs(i, v, j, t);
        }
        if (ind) return;
        st.pop_back();
    }
    void add(int x, int y, int i){
        ed[i] = {x, y};
        st.clear();
        ind = 0;
        dfs(x, 0, A, y);
        if (st.size() > 1){
            int mn = A;
            for (auto [j, ii]: st) mn = f(mn, ii);
            auto [u, v] = ed[mn];
            gd.erase(find(gd.begin(), gd.end(), mn));
            for (int i = 0; i < g[u].size(); i++){
                if (g[u][i].ff == v){
                    g[u].erase(g[u].begin() + i);
                    break;
                }
            }
            for (int i = 0; i < g[v].size(); i++){
                if (g[v][i].ff == u){
                    g[v].erase(g[v].begin() + i);
                    break;
                }
            }
        }
        g[x].pb({y, i});
        g[y].pb({x, i});
        gd.pb(i);
    }
    void clear(){
        for (int i = 1; i <= n; i++){
            g[i].clear();
        }
        ed.clear();
        gd.clear();
    }
};

struct dsu{
    vector<int> sz, p;
    int n;
    ll W;
    dsu(int ns){
        n = ns;
        p.resize(n + 1);
        sz.resize(n + 1);
        for (int i = 1; i <= n; i++){
            p[i] = i;
            sz[i] = 1;
        }
        W = 0;
    }
    int get(int v){
        if (p[v] != v){
            p[v] = get(p[v]);
        }
        return p[v];
    }
    void unite(int x, int y, int w){
        x = get(x); y = get(y);
        if (x == y) return;
        if (sz[x] > sz[y]) swap(x, y);
        p[x] = y;
        sz[y] += sz[x];
        W += w;
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, m; cin>>n>>m;
    vector<arr3> ed = {{0, 0, 0}};
    for (int i = 1; i <= m; i++){
        int a, b, w; cin>>a>>b>>w;
        ed.pb({a, b, w});
    }
    ed.pb({0, 0, inf});
    
    auto cmp = [&](arr3& x, arr3& y){
        return x[2] < y[2];
    };
    sort(ed.begin(), ed.end(), cmp);
    
    vector<int> actl[m + 2];
    ST T(n, m);
    T.set_mini();
    for (int i = 1; i <= m; i++){
        T.add(ed[i][0], ed[i][1], i);
        actl[i] = T.gd;
    }
    
    T.clear();
    T.set_maxi();
    vector<int> actr[m + 2];
    for (int i = m; i > 0; i--){
        T.add(ed[i][0], ed[i][1], i);
        actr[i] = T.gd;
    }
    
    int q; cin>>q;
    vector<pii> qs;
    for (int i = 1; i <= q; i++){
        int x; cin>>x;
        qs.pb({x, i});
    }
    sort(qs.begin(), qs.end());

    vector<ll> out(q + 1);
    int j1 = 0;
    for (int i = 0; i <= m; i++){
        vector<int> edg;
        for (auto p: actl[i]) edg.pb(p);
        for (auto p: actr[i + 1]) edg.pb(p);
        
        int l = ed[i][2], r = ed[i + 1][2] - 1;
        int j2 = j1;
        while (j2 < qs.size() && l <= qs[j2].ff && qs[j2].ff <= r){
            j2++;
        }
        while (j1 < j2){
            int x = qs[j1].ff;
            vector<arr3> ed1;
            for (int p: edg) ed1.pb({ed[p][0], ed[p][1], abs(ed[p][2] - x)});
            sort(ed1.begin(), ed1.end(), cmp);
            dsu T(n);
            for (auto [x, y, w]: ed1) T.unite(x, y, w);
            out[qs[j1].ss] = T.W;
            j1++;
        }
    }
    
    for (int i = 1; i <= q; i++) cout<<out[i]<<"\n";
}

Compilation message

reconstruction.cpp: In member function 'void ST::add(int, int, int)':
reconstruction.cpp:65:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |             for (int i = 0; i < g[u].size(); i++){
      |                             ~~^~~~~~~~~~~~~
reconstruction.cpp:71:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |             for (int i = 0; i < g[v].size(); i++){
      |                             ~~^~~~~~~~~~~~~
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:172:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  172 |         while (j2 < qs.size() && l <= qs[j2].ff && qs[j2].ff <= r){
      |                ~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 980 ms 402140 KB Output is correct
21 Correct 650 ms 380508 KB Output is correct
22 Correct 853 ms 397000 KB Output is correct
23 Correct 894 ms 401092 KB Output is correct
24 Correct 852 ms 401852 KB Output is correct
25 Correct 975 ms 402120 KB Output is correct
26 Correct 990 ms 402376 KB Output is correct
27 Correct 1010 ms 402116 KB Output is correct
28 Correct 982 ms 402116 KB Output is correct
29 Correct 989 ms 402116 KB Output is correct
30 Correct 1008 ms 402176 KB Output is correct
31 Correct 1032 ms 402048 KB Output is correct
32 Correct 992 ms 402068 KB Output is correct
33 Correct 980 ms 402120 KB Output is correct
34 Correct 999 ms 402284 KB Output is correct
35 Correct 994 ms 402116 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 Execution timed out 5065 ms 427608 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Execution timed out 5072 ms 28764 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 980 ms 402140 KB Output is correct
21 Correct 650 ms 380508 KB Output is correct
22 Correct 853 ms 397000 KB Output is correct
23 Correct 894 ms 401092 KB Output is correct
24 Correct 852 ms 401852 KB Output is correct
25 Correct 975 ms 402120 KB Output is correct
26 Correct 990 ms 402376 KB Output is correct
27 Correct 1010 ms 402116 KB Output is correct
28 Correct 982 ms 402116 KB Output is correct
29 Correct 989 ms 402116 KB Output is correct
30 Correct 1008 ms 402176 KB Output is correct
31 Correct 1032 ms 402048 KB Output is correct
32 Correct 992 ms 402068 KB Output is correct
33 Correct 980 ms 402120 KB Output is correct
34 Correct 999 ms 402284 KB Output is correct
35 Correct 994 ms 402116 KB Output is correct
36 Correct 2033 ms 402908 KB Output is correct
37 Correct 1508 ms 381308 KB Output is correct
38 Correct 1959 ms 397740 KB Output is correct
39 Correct 2025 ms 401856 KB Output is correct
40 Correct 1954 ms 402696 KB Output is correct
41 Correct 1994 ms 402676 KB Output is correct
42 Correct 2077 ms 402992 KB Output is correct
43 Correct 2082 ms 402884 KB Output is correct
44 Correct 1460 ms 403016 KB Output is correct
45 Correct 1395 ms 402884 KB Output is correct
46 Correct 2141 ms 402836 KB Output is correct
47 Correct 2067 ms 402940 KB Output is correct
48 Correct 2041 ms 402884 KB Output is correct
49 Correct 2096 ms 403020 KB Output is correct
50 Correct 1203 ms 403140 KB Output is correct
51 Correct 1677 ms 402860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 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 460 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 600 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 980 ms 402140 KB Output is correct
21 Correct 650 ms 380508 KB Output is correct
22 Correct 853 ms 397000 KB Output is correct
23 Correct 894 ms 401092 KB Output is correct
24 Correct 852 ms 401852 KB Output is correct
25 Correct 975 ms 402120 KB Output is correct
26 Correct 990 ms 402376 KB Output is correct
27 Correct 1010 ms 402116 KB Output is correct
28 Correct 982 ms 402116 KB Output is correct
29 Correct 989 ms 402116 KB Output is correct
30 Correct 1008 ms 402176 KB Output is correct
31 Correct 1032 ms 402048 KB Output is correct
32 Correct 992 ms 402068 KB Output is correct
33 Correct 980 ms 402120 KB Output is correct
34 Correct 999 ms 402284 KB Output is correct
35 Correct 994 ms 402116 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Execution timed out 5065 ms 427608 KB Time limit exceeded
40 Halted 0 ms 0 KB -