Submission #1185750

#TimeUsernameProblemLanguageResultExecution timeMemory
1185750Zbyszek99Cat Exercise (JOI23_ho_t4)C++20
100 / 100
145 ms37364 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

int rep_[200'001];
ll comp_ans[200'001];
pii comp_max[200'001];

int fint(int v)
{
    if(rep_[v] == v) return v;
    rep_[v] = fint(rep_[v]);
    return rep_[v];
}

void onion(int a, int b)
{
    a = fint(a);
    b = fint(b);
    comp_max[b] = max(comp_max[a],comp_max[b]);
    rep_[a] = b;
}

vi graph[200'001];
int P[200'001];
int depth[200'001];
int jump[200'001][18];

void dfs(int v, int pop, int d)
{
    depth[v] = d;
    jump[v][0] = pop;
    forall(it,graph[v])
    {
        if(it != pop)
        {
            dfs(it,v,d+1);
        }
    }
}

int dist(int a, int b)
{
    int a2 = a;
    int b2 = b;
    if(a == b) return 0;
    if(depth[a] < depth[b])
    {
        swap(a,b);
    }
    for(int bit = 17; bit >= 0; bit--)
    {
        if(depth[a] - (1 << bit) >= depth[b])
        {
            a = jump[a][bit];
        }
    }
    if(a == b) return depth[a2] + depth[b2] - 2*depth[a]; 
    for(int bit = 17; bit >= 0; bit--)
    {
        if(jump[a][bit] != jump[b][bit])
        {
            a = jump[a][bit];
            b = jump[b][bit];
        }
    }
    a = jump[a][0];
    return depth[a2] + depth[b2] - 2*depth[a];
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int n;
    cin >> n;
    rep2(i,1,n) cin >> P[i];
    rep(i,n-1)
    {
        int a,b;
        cin >> a >> b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    dfs(1,1,0);
    rep2(bit,1,17)
    {
        rep2(i,1,n)
        {
            jump[i][bit] = jump[jump[i][bit-1]][bit-1];
        }
    }
    rep2(i,1,n)
    {
        rep_[i] = i;
        comp_max[i] = {P[i],i};
    }
    vector<pii> sort_vert;
    rep2(i,1,n) sort_vert.pb({P[i],i});
    sort(all(sort_vert));
    ll ans = 0;
    forall(v,sort_vert)
    {
        ll new_ans = 0;
        forall(it,graph[v.ss])
        {
            if(P[it] > v.ff) continue;
            new_ans = max(new_ans,comp_ans[fint(it)] + dist(v.ss,comp_max[fint(it)].ss));
        }
        forall(it,graph[v.ss])
        {
            if(P[it] > v.ff) continue;
            onion(v.ss,it);
        }
        ans = max(ans,new_ans);
        comp_ans[fint(v.ss)] = new_ans;
    }
    cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...