제출 #899527

#제출 시각아이디문제언어결과실행 시간메모리
899527Ice_manCat Exercise (JOI23_ho_t4)C++14
54 / 100
209 ms37052 KiB
#include <iostream>
#include <chrono>
#include <vector>

#define maxn 200005
#define maxlog 20
#define INF 1000000010
#define LINF 1000000000000000005
#define endl '\n'
#define pb(x) push_back(x)
#define X first
#define Y second
#define control cout<<"passed"<<endl;

#pragma GCC optimize("O3" , "Ofast" , "unroll-loops" , "fast-math")
#pragma GCC target("avx2")

using namespace std;

std::chrono::high_resolution_clock::time_point startT, currT;
constexpr double TIME_MULT = 1;

double timePassed()
{
    using namespace std::chrono;
    currT = high_resolution_clock::now();
    double time = duration_cast<duration<double>>(currT - startT).count();
    return time * TIME_MULT;
}

int n;

int h[maxn] , depth[maxn];

vector <int> v[maxn];

void read()
{
    cin >> n;

    for(int i = 1; i <= n; i++)
        cin >> h[i];

    int from , to;
    for(int i = 1; i < n; i++)
    {
        cin >> from >> to;

        v[h[from]].pb(h[to]);
        v[h[to]].pb(h[from]);

    }

}

int bin_lift[maxn][maxlog];

void dfs(int node , int _parent)
{
    for(int nb : v[node])
    {
        if(nb == _parent)
            continue;

        depth[nb] = depth[node] + 1;
        bin_lift[nb][0] = node;

        dfs(nb , node);

    }
}

void calc()
{
    for(int i = 1; i < maxlog; i++)
        for(int j = 1; j <= n; j++)
            bin_lift[j][i] = bin_lift[bin_lift[j][i - 1]][i - 1];
}


int get_distance(int a , int b)
{
    int dist = 0;

    if(depth[b] > depth[a])
        swap(a ,b);

    int need = depth[a] - depth[b];

    for(int power = maxlog - 1; power > -1; power--)
    {
        if(need >= (1 << power))
        {
            a = bin_lift[a][power];

            dist += (1 << power);
            need -= (1 << power);
        }
    }

    if(a == b)
        return dist;

    for(int power = maxlog - 1; power > -1; power--)
    {
        if(bin_lift[a][power] != bin_lift[b][power])
        {
            a = bin_lift[a][power];
            b = bin_lift[b][power];

            dist += (1 << (power + 1));
        }
    }

    return (dist + 2);

}

int parent[maxn];

int find_parent(int node)
{
    ///cout << node << endl;

    return parent[node] == -1? node : parent[node] = find_parent(parent[node]);
}

int pom[maxn];

void solve()
{
    for(int i = 1; i <= n; i++)
        parent[i] = -1;

    for(int node = 1; node <= n; node++)
        for(int nb : v[node])
        {
            nb = find_parent(nb);

            ///control

            if(nb <= node)
            {
                int d = get_distance(node , nb);
                pom[node] = max(pom[node] , pom[nb] + d);

                parent[nb] = node;
            }
        }

    cout << pom[n] << endl;

}


int main()
{

/**#ifdef ONLINE_JUDGE
    freopen("taxi.in", "r", stdin);
    freopen("taxi.out", "w", stdout);
#endif*/

    /**ios_base::sync_with_stdio(false);
    cin.tie(nullptr);*/

    startT = std::chrono::high_resolution_clock::now();

    read();

    ///control

    dfs(1 , -1);

    ///control

    calc();

    ///control

    solve();

    return 0;
}
#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...