Submission #781456

# Submission time Handle Problem Language Result Execution time Memory
781456 2023-07-13T06:37:17 Z boris_mihov Cat Exercise (JOI23_ho_t4) C++17
54 / 100
218 ms 35784 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXLOG = 18;
const int MAXN = 200000 + 10;
const int INF  = 1e9;

int n;
struct Sparse
{
    int sparse[MAXLOG][MAXN];
    int dep[MAXN];

    void build(int par[], int d[])
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            sparse[0][i] = par[i];
            dep[i] = d[i];
        }

        for (int log = 1 ; (1 << log) <= n ; ++log)
        {
            for (int i = 1 ; i <= n ; ++i)
            {
                sparse[log][i] = sparse[log - 1][sparse[log - 1][i]];
            }
        }
    }

    void equalize(int &u, int v)
    {
        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (dep[sparse[log][u]] >= dep[v])
            {
                u = sparse[log][u];
            }
        }
    }

    int getLCA(int u, int v)
    {
        if (u == v)
        {
            return u;
        }

        for (int log = MAXLOG - 1 ; log >= 0 ; --log)
        {
            if (sparse[log][u] != sparse[log][v])
            {
                u = sparse[log][u];
                v = sparse[log][v];
            }
        }

        return sparse[0][u];
    }

    int findLCA(int u, int v)
    {
        if (dep[u] < dep[v])
        {
            std::swap(u, v);
        }

        equalize(u, v);
        return getLCA(u, v);
    }

    int findDist(int u, int v)
    {
        return dep[u] + dep[v] - 2 * dep[findLCA(u, v)];
    }
};

struct DSU
{
    int par[MAXN];
    int dep[MAXN];
    int root[MAXN];

    void build()
    {
        for (int i = 1 ; i <= n ; ++i)
        {
            dep[i] = 1;
            par[i] = root[i] = i;
        }
    }

    int find(int u)
    {
        if (par[u] == u) return u;
        return par[u] = find(par[u]);
    }

    void connect(int u, int v)
    {
        u = find(u);
        v = find(v);
        assert(u != v);

        if (dep[u] < dep[v])
        {
            std::swap(u, v);
        }

        if (dep[u] == dep[v])
        {
            dep[v]++;
        }

        par[u] = v;
    }
};

int d[MAXN];
int dp[MAXN];
int par[MAXN];
Sparse sparse;
std::vector <int> g[MAXN];
DSU dsu;

void dfs(int node, int p)
{
    par[node] = p;
    d[node] = d[p] + 1;
    for (const int &u : g[node])
    {
        if (u == p)
        {
            continue;
        }

        dfs(u, node);
    }
}

void solve()
{
    dfs(1, 0);
    dsu.build();
    sparse.build(par, d);
    for (int i = 1 ; i <= n ; ++i)
    {
        for (const int &u : g[i])
        {
            if (u > i)
            {
                continue;
            }

            dp[i] = std::max(dp[i], dp[dsu.root[dsu.find(u)]] + sparse.findDist(i, dsu.root[dsu.find(u)]));
            dsu.connect(i, u);
        }

        dsu.root[dsu.find(i)] = i;
    }

    std::cout << dp[n] << '\n';
}

void input()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> d[i];
    }

    for (int i = 2 ; i <= n ; ++i)
    {
        int u, v;
        std::cin >> u >> v;
        g[d[u]].push_back(d[v]);
        g[d[v]].push_back(d[u]);
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5028 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5028 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5160 KB Output is correct
18 Correct 5 ms 5844 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 4 ms 5972 KB Output is correct
21 Correct 4 ms 5844 KB Output is correct
22 Correct 4 ms 5844 KB Output is correct
23 Correct 4 ms 5844 KB Output is correct
24 Correct 5 ms 5844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5028 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5160 KB Output is correct
18 Correct 5 ms 5844 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 4 ms 5972 KB Output is correct
21 Correct 4 ms 5844 KB Output is correct
22 Correct 4 ms 5844 KB Output is correct
23 Correct 4 ms 5844 KB Output is correct
24 Correct 5 ms 5844 KB Output is correct
25 Correct 2 ms 5032 KB Output is correct
26 Correct 4 ms 5844 KB Output is correct
27 Correct 4 ms 5812 KB Output is correct
28 Correct 5 ms 5844 KB Output is correct
29 Correct 6 ms 5912 KB Output is correct
30 Correct 7 ms 5716 KB Output is correct
31 Correct 7 ms 5684 KB Output is correct
32 Correct 5 ms 5716 KB Output is correct
33 Correct 5 ms 5716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5028 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5160 KB Output is correct
18 Correct 5 ms 5844 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 4 ms 5972 KB Output is correct
21 Correct 4 ms 5844 KB Output is correct
22 Correct 4 ms 5844 KB Output is correct
23 Correct 4 ms 5844 KB Output is correct
24 Correct 5 ms 5844 KB Output is correct
25 Incorrect 105 ms 35784 KB Output isn't correct
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 218 ms 31080 KB Output is correct
4 Correct 183 ms 34500 KB Output is correct
5 Correct 190 ms 34508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5076 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 2 ms 5036 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 2 ms 5076 KB Output is correct
14 Correct 3 ms 5076 KB Output is correct
15 Correct 3 ms 5028 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5160 KB Output is correct
18 Correct 5 ms 5844 KB Output is correct
19 Correct 4 ms 5972 KB Output is correct
20 Correct 4 ms 5972 KB Output is correct
21 Correct 4 ms 5844 KB Output is correct
22 Correct 4 ms 5844 KB Output is correct
23 Correct 4 ms 5844 KB Output is correct
24 Correct 5 ms 5844 KB Output is correct
25 Correct 2 ms 5032 KB Output is correct
26 Correct 4 ms 5844 KB Output is correct
27 Correct 4 ms 5812 KB Output is correct
28 Correct 5 ms 5844 KB Output is correct
29 Correct 6 ms 5912 KB Output is correct
30 Correct 7 ms 5716 KB Output is correct
31 Correct 7 ms 5684 KB Output is correct
32 Correct 5 ms 5716 KB Output is correct
33 Correct 5 ms 5716 KB Output is correct
34 Incorrect 105 ms 35784 KB Output isn't correct
35 Halted 0 ms 0 KB -