Submission #899529

# Submission time Handle Problem Language Result Execution time Memory
899529 2024-01-06T11:38:09 Z Ice_man Cat Exercise (JOI23_ho_t4) C++14
100 / 100
304 ms 58048 KB
#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;
}

long long n;

long long h[maxn] , depth[maxn];

vector <long long> v[maxn];

void read()
{
    cin >> n;

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

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

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

    }

}

long long bin_lift[maxn][maxlog];

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

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

        dfs(nb , node);

    }
}

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


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

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

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

    for(long long 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(long long 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);

}

long long parent[maxn];

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

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

long long pom[maxn];

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

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

            ///control

            if(nb <= node)
            {
                long long 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 time Memory Grader output
1 Correct 4 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 3 ms 12788 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 2 ms 12728 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 3 ms 12788 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 2 ms 12728 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 5 ms 12892 KB Output is correct
19 Correct 6 ms 12892 KB Output is correct
20 Correct 5 ms 12892 KB Output is correct
21 Correct 5 ms 12892 KB Output is correct
22 Correct 6 ms 12892 KB Output is correct
23 Correct 7 ms 12828 KB Output is correct
24 Correct 5 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 3 ms 12788 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 2 ms 12728 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 5 ms 12892 KB Output is correct
19 Correct 6 ms 12892 KB Output is correct
20 Correct 5 ms 12892 KB Output is correct
21 Correct 5 ms 12892 KB Output is correct
22 Correct 6 ms 12892 KB Output is correct
23 Correct 7 ms 12828 KB Output is correct
24 Correct 5 ms 12892 KB Output is correct
25 Correct 2 ms 12636 KB Output is correct
26 Correct 7 ms 12892 KB Output is correct
27 Correct 5 ms 12892 KB Output is correct
28 Correct 6 ms 12912 KB Output is correct
29 Correct 6 ms 12892 KB Output is correct
30 Correct 6 ms 12892 KB Output is correct
31 Correct 7 ms 12892 KB Output is correct
32 Correct 6 ms 12892 KB Output is correct
33 Correct 6 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 3 ms 12788 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 2 ms 12728 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 5 ms 12892 KB Output is correct
19 Correct 6 ms 12892 KB Output is correct
20 Correct 5 ms 12892 KB Output is correct
21 Correct 5 ms 12892 KB Output is correct
22 Correct 6 ms 12892 KB Output is correct
23 Correct 7 ms 12828 KB Output is correct
24 Correct 5 ms 12892 KB Output is correct
25 Correct 190 ms 51716 KB Output is correct
26 Correct 186 ms 57940 KB Output is correct
27 Correct 193 ms 58048 KB Output is correct
28 Correct 239 ms 57940 KB Output is correct
29 Correct 225 ms 55372 KB Output is correct
30 Correct 219 ms 57324 KB Output is correct
31 Correct 227 ms 56668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 12644 KB Output is correct
2 Correct 2 ms 12840 KB Output is correct
3 Correct 214 ms 50408 KB Output is correct
4 Correct 210 ms 50516 KB Output is correct
5 Correct 216 ms 50444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 12632 KB Output is correct
2 Correct 2 ms 12632 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 2 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12632 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 2 ms 12636 KB Output is correct
10 Correct 2 ms 12636 KB Output is correct
11 Correct 2 ms 12636 KB Output is correct
12 Correct 3 ms 12788 KB Output is correct
13 Correct 2 ms 12632 KB Output is correct
14 Correct 2 ms 12636 KB Output is correct
15 Correct 2 ms 12728 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 5 ms 12892 KB Output is correct
19 Correct 6 ms 12892 KB Output is correct
20 Correct 5 ms 12892 KB Output is correct
21 Correct 5 ms 12892 KB Output is correct
22 Correct 6 ms 12892 KB Output is correct
23 Correct 7 ms 12828 KB Output is correct
24 Correct 5 ms 12892 KB Output is correct
25 Correct 2 ms 12636 KB Output is correct
26 Correct 7 ms 12892 KB Output is correct
27 Correct 5 ms 12892 KB Output is correct
28 Correct 6 ms 12912 KB Output is correct
29 Correct 6 ms 12892 KB Output is correct
30 Correct 6 ms 12892 KB Output is correct
31 Correct 7 ms 12892 KB Output is correct
32 Correct 6 ms 12892 KB Output is correct
33 Correct 6 ms 12892 KB Output is correct
34 Correct 190 ms 51716 KB Output is correct
35 Correct 186 ms 57940 KB Output is correct
36 Correct 193 ms 58048 KB Output is correct
37 Correct 239 ms 57940 KB Output is correct
38 Correct 225 ms 55372 KB Output is correct
39 Correct 219 ms 57324 KB Output is correct
40 Correct 227 ms 56668 KB Output is correct
41 Correct 2 ms 12644 KB Output is correct
42 Correct 2 ms 12840 KB Output is correct
43 Correct 214 ms 50408 KB Output is correct
44 Correct 210 ms 50516 KB Output is correct
45 Correct 216 ms 50444 KB Output is correct
46 Correct 196 ms 56936 KB Output is correct
47 Correct 224 ms 56916 KB Output is correct
48 Correct 192 ms 56960 KB Output is correct
49 Correct 209 ms 56912 KB Output is correct
50 Correct 233 ms 53708 KB Output is correct
51 Correct 304 ms 53936 KB Output is correct
52 Correct 231 ms 53524 KB Output is correct
53 Correct 238 ms 53584 KB Output is correct