Submission #1029720

# Submission time Handle Problem Language Result Execution time Memory
1029720 2024-07-21T08:34:18 Z shenfe1 Reconstruction Project (JOI22_reconstruction) C++17
7 / 100
5000 ms 13240 KB
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(s) (int)s.size()
#define ppb pop_back
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define lb lower_bound
// #define int ll

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAX=2e5+10;

int n,m;
vector<pair<int,pii>> e;

struct DSU{
    int f[MAX];

    void init(int n){
        for(int i=1;i<=n;i++)f[i]=i;
    }

    int get(int v){
        return (f[v]==v?v:f[v]=get(f[v]));
    }

    void unite(int a,int b){
        a=get(a);
        b=get(b);
        f[a]=b;
    }
}D;

int make(int a,pii x){
    if(D.get(x.F)!=D.get(x.S)){
        D.unite(x.F,x.S);
        return a;
    }
    return 0;
}

ll solve(int d){
    int r=lb(all(e),make_pair(d,make_pair(0,0)))-e.begin();
    int l=r-1;
    D.init(n);
    ll ans=0;
    for(int i=1;i<=m;i++){
        if(r<sz(e)&&l>=0){
            if(e[r].F-d<=d-e[l].F){
                ans+=make(e[r].F-d,e[r].S);
                r++;
            }
            else{
                ans+=make(d-e[l].F,e[l].S);
                l--;
            }
        }
        else if(r<sz(e)){
            ans+=make(e[r].F-d,e[r].S);
            r++;
        }
        else{
            ans+=make(d-e[l].F,e[l].S);
            l--;
        }
    }
    return ans;
}

void solve(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        e.pb({c,{a,b}});
    }
    sort(all(e));
    // cout<<e[0].F<<" "<<e[1].F<<"\n";
    int q;
    cin>>q;
    while(q--){
        int d;
        cin>>d;
        cout<<solve(d)<<"\n";
    }
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--)solve();
}
# 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 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 1 ms 348 KB Output is correct
13 Correct 1 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 1 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 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 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 1 ms 348 KB Output is correct
13 Correct 1 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 1 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 1 ms 344 KB Output is correct
20 Correct 27 ms 2004 KB Output is correct
21 Correct 27 ms 2000 KB Output is correct
22 Correct 27 ms 2004 KB Output is correct
23 Correct 26 ms 1964 KB Output is correct
24 Correct 28 ms 2000 KB Output is correct
25 Correct 25 ms 2004 KB Output is correct
26 Correct 27 ms 2004 KB Output is correct
27 Correct 33 ms 2000 KB Output is correct
28 Correct 26 ms 2000 KB Output is correct
29 Correct 27 ms 2004 KB Output is correct
30 Correct 30 ms 2000 KB Output is correct
31 Correct 44 ms 2004 KB Output is correct
32 Correct 28 ms 2000 KB Output is correct
33 Correct 28 ms 2000 KB Output is correct
34 Correct 36 ms 2004 KB Output is correct
35 Correct 48 ms 2016 KB Output is correct
# Verdict Execution time Memory 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 Execution timed out 5096 ms 2056 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 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 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 1 ms 348 KB Output is correct
13 Correct 1 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 1 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 344 KB Output is correct
20 Execution timed out 5046 ms 13240 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 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 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 1 ms 348 KB Output is correct
13 Correct 1 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 1 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 1 ms 344 KB Output is correct
20 Correct 27 ms 2004 KB Output is correct
21 Correct 27 ms 2000 KB Output is correct
22 Correct 27 ms 2004 KB Output is correct
23 Correct 26 ms 1964 KB Output is correct
24 Correct 28 ms 2000 KB Output is correct
25 Correct 25 ms 2004 KB Output is correct
26 Correct 27 ms 2004 KB Output is correct
27 Correct 33 ms 2000 KB Output is correct
28 Correct 26 ms 2000 KB Output is correct
29 Correct 27 ms 2004 KB Output is correct
30 Correct 30 ms 2000 KB Output is correct
31 Correct 44 ms 2004 KB Output is correct
32 Correct 28 ms 2000 KB Output is correct
33 Correct 28 ms 2000 KB Output is correct
34 Correct 36 ms 2004 KB Output is correct
35 Correct 48 ms 2016 KB Output is correct
36 Execution timed out 5044 ms 2004 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# 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 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 1 ms 348 KB Output is correct
13 Correct 1 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 1 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 1 ms 344 KB Output is correct
20 Correct 27 ms 2004 KB Output is correct
21 Correct 27 ms 2000 KB Output is correct
22 Correct 27 ms 2004 KB Output is correct
23 Correct 26 ms 1964 KB Output is correct
24 Correct 28 ms 2000 KB Output is correct
25 Correct 25 ms 2004 KB Output is correct
26 Correct 27 ms 2004 KB Output is correct
27 Correct 33 ms 2000 KB Output is correct
28 Correct 26 ms 2000 KB Output is correct
29 Correct 27 ms 2004 KB Output is correct
30 Correct 30 ms 2000 KB Output is correct
31 Correct 44 ms 2004 KB Output is correct
32 Correct 28 ms 2000 KB Output is correct
33 Correct 28 ms 2000 KB Output is correct
34 Correct 36 ms 2004 KB Output is correct
35 Correct 48 ms 2016 KB Output is correct
36 Correct 1 ms 344 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 5096 ms 2056 KB Time limit exceeded
40 Halted 0 ms 0 KB -