답안 #837288

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
837288 2023-08-25T09:15:22 Z jasmin Cat Exercise (JOI23_ho_t4) C++17
7 / 100
1 ms 596 KB
//JOI 2023 Final Round
#include<bits/stdc++.h>
using namespace std;
#define int long long

void assertx(bool x){
    if(!x){
        cout << "27\n";
    }
}

struct ufind{
    vector<int> chef;

    ufind(int n){
        chef.assign(n, 0);
        iota(chef.begin(), chef.end(), 0);
    }

    int find(int a){
        if(chef[a]==a) return a;
        return chef[a] = find(chef[a]);
    }

    void unite(int a, int b){
        a=find(a); b=find(b);

        chef[b]=a;
    }
};

struct tree{
    int n;
    vector<vector<int> > adi;

    vector<int> tin;
    vector<int> tout;
    vector<int> h;
    int t=0;

    const int L=25;
    vector<vector<int> > p;

    tree(int ninput){
        n=ninput;

        adi.assign(n, {});

        tin.assign(n, 0);
        tout.assign(n, 0);
        h.assign(n, 0);

        p.assign(L, vector<int> (n, -1));
    }

    void dfs(int v, int par){
        tin[v]=t;
        t++;

        p[0][v]= par;
        for(int i=1; i<L; i++){
            if(p[i-1][v] == -1) break; 

            p[i][v] = p[p[i-1][v]][i-1];
        }

        for(auto u: adi[v]){
            if(u==par) continue;

            assertx(0<=u && u<n);

            h[u] = h[v]+1;
            dfs(u, v);
        }

        tout[v]=t;
        t++;
    }

    bool ancestor(int a, int b){
        return tin[a]<=tin[b] && tout[b]<=tout[a];
    }

    int lca(int a, int b){
        if(ancestor(a, b)) return a;
        if(ancestor(b, a)) return b;

        for(int i=L-1; i>=0; i--){
            if(p[i][a]!=-1 && !ancestor(p[i][a], b)){
                a = p[i][a];
            }
        }

        return p[0][a];
    }

    int dist(int a, int b){

        int x=lca(a, b);
        int dist = h[a]-h[x] + h[b]-h[x];

        return dist;
    }
};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> h(n);
    for(int i=0; i<n; i++){
        cin >> h[i];
    }

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


    vector<int> sorted(n);
    iota(sorted.begin(), sorted.end(), 0);
    sort(sorted.begin(), sorted.end(), [&](int i, int j){
        return h[i] < h[j];
    });

    vector<int> ans(n, 0);
    ufind uf(n);
    int maxi=0;
    for(auto v: sorted){

        for(auto u: g.adi[v]){
            if(h[u] > h[v]) continue;
        
            u=uf.find(u);

            ans[v] = max(ans[v], ans[u] + g.dist(v, u));
            uf.unite(v, u);
        }

        maxi = max(maxi, ans[v]);

    }

    cout << maxi << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Runtime error 1 ms 596 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Runtime error 1 ms 596 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Runtime error 1 ms 596 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Runtime error 1 ms 596 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Runtime error 1 ms 468 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Runtime error 1 ms 596 KB Execution killed with signal 11
12 Halted 0 ms 0 KB -