Submission #935837

# Submission time Handle Problem Language Result Execution time Memory
935837 2024-02-29T16:07:27 Z anton Cat Exercise (JOI23_ho_t4) C++17
0 / 100
1 ms 348 KB
#include<bits/stdc++.h>


using namespace std;
#define int long long
#define pii pair<int, int>
int n;

const int MAX_N = 5000;

vector<int> adj[MAX_N];
int H[MAX_N];
int score[MAX_N];

struct DSU{
    vector<int> dsu;
    vector<int> sz;
    vector<pair<int, pii>> max_v;

    DSU(int len){
        dsu.resize(len);
        sz.resize(len);
        max_v.resize(len);

        for(int i = 0; i<len; i++){
            dsu[i] =i;
            sz[i]= 1;
            max_v[i] = {H[i], {i, 0}};
        }
    }

    int getC(int a){
        if(dsu[a] == a){
            return a;
        }
        int b= getC(dsu[a]);
        dsu[a] = b;
        return b;
    }

    void merge(int a, int b){
        if(sz[a]<sz[b]){
            swap(a, b);
        }
        dsu[b] = a;
        sz[a] += sz[b];
        if(max_v[a].first<max_v[b].first){
            max_v[a] = max_v[b];
        }
    }
};


signed main(){
    cin>>n;

    vector<pii> pts;

    for(int i = 0; i<n; i++){
        cin>>H[i];
        pts.push_back({H[i], i});
    }


    for(int i = 0; i<n-1; i++){
        int a, b;
        cin>>a>>b;
        adj[a-1].push_back(b-1);
        adj[b-1].push_back(a-1);
    }

    sort(pts.begin(), pts.end());

    DSU dsu =DSU(n);
    
    for(int i = 0; i<n; i++){
        int u = pts[i].second;
        int s_after = 0;
        for(auto v: adj[u]){
            if(H[v]<H[u]){
                //cout<<v<<" score "<<dsu.max_v[dsu.getC(v)].first<<" "<<dsu.max_v[dsu.getC(v)].second.first<<" "<<dsu.max_v[dsu.getC(v)].second.second<<endl;
                s_after = max(s_after, dsu.max_v[dsu.getC(v)].second.second + abs(u-dsu.max_v[dsu.getC(v)].second.first));
                dsu.merge(u, v);
            }
        }
        dsu.max_v[dsu.getC(u)] = {H[u],{u, s_after}};
        //cout<<u<<" "<<score[u]<<endl;
    }

    cout<<dsu.max_v[dsu.getC(0)].second.second<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -