답안 #781461

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781461 2023-07-13T06:39:57 Z boris_mihov Cat Exercise (JOI23_ho_t4) C++17
100 / 100
227 ms 44676 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 par[MAXN];
Sparse sparse;
std::vector <int> g[MAXN];
llong dp[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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 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 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 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 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 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 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 5 ms 5716 KB Output is correct
19 Correct 5 ms 5884 KB Output is correct
20 Correct 4 ms 5844 KB Output is correct
21 Correct 4 ms 5720 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5764 KB Output is correct
24 Correct 4 ms 5844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 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 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 5 ms 5716 KB Output is correct
19 Correct 5 ms 5884 KB Output is correct
20 Correct 4 ms 5844 KB Output is correct
21 Correct 4 ms 5720 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5764 KB Output is correct
24 Correct 4 ms 5844 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 5 ms 5844 KB Output is correct
28 Correct 4 ms 5844 KB Output is correct
29 Correct 5 ms 5788 KB Output is correct
30 Correct 5 ms 5716 KB Output is correct
31 Correct 5 ms 5760 KB Output is correct
32 Correct 5 ms 5716 KB Output is correct
33 Correct 5 ms 5676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 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 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 5 ms 5716 KB Output is correct
19 Correct 5 ms 5884 KB Output is correct
20 Correct 4 ms 5844 KB Output is correct
21 Correct 4 ms 5720 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5764 KB Output is correct
24 Correct 4 ms 5844 KB Output is correct
25 Correct 109 ms 36268 KB Output is correct
26 Correct 97 ms 44660 KB Output is correct
27 Correct 96 ms 44676 KB Output is correct
28 Correct 215 ms 44192 KB Output is correct
29 Correct 200 ms 40152 KB Output is correct
30 Correct 175 ms 43148 KB Output is correct
31 Correct 207 ms 42192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 181 ms 31508 KB Output is correct
4 Correct 184 ms 31596 KB Output is correct
5 Correct 204 ms 31520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 2 ms 5076 KB Output is correct
7 Correct 3 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 5076 KB Output is correct
11 Correct 2 ms 5076 KB Output is correct
12 Correct 2 ms 5076 KB Output is correct
13 Correct 3 ms 5076 KB Output is correct
14 Correct 2 ms 5076 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 5 ms 5716 KB Output is correct
19 Correct 5 ms 5884 KB Output is correct
20 Correct 4 ms 5844 KB Output is correct
21 Correct 4 ms 5720 KB Output is correct
22 Correct 5 ms 5844 KB Output is correct
23 Correct 5 ms 5764 KB Output is correct
24 Correct 4 ms 5844 KB Output is correct
25 Correct 2 ms 5076 KB Output is correct
26 Correct 6 ms 5844 KB Output is correct
27 Correct 5 ms 5844 KB Output is correct
28 Correct 4 ms 5844 KB Output is correct
29 Correct 5 ms 5788 KB Output is correct
30 Correct 5 ms 5716 KB Output is correct
31 Correct 5 ms 5760 KB Output is correct
32 Correct 5 ms 5716 KB Output is correct
33 Correct 5 ms 5676 KB Output is correct
34 Correct 109 ms 36268 KB Output is correct
35 Correct 97 ms 44660 KB Output is correct
36 Correct 96 ms 44676 KB Output is correct
37 Correct 215 ms 44192 KB Output is correct
38 Correct 200 ms 40152 KB Output is correct
39 Correct 175 ms 43148 KB Output is correct
40 Correct 207 ms 42192 KB Output is correct
41 Correct 2 ms 5076 KB Output is correct
42 Correct 2 ms 5076 KB Output is correct
43 Correct 181 ms 31508 KB Output is correct
44 Correct 184 ms 31596 KB Output is correct
45 Correct 204 ms 31520 KB Output is correct
46 Correct 122 ms 41860 KB Output is correct
47 Correct 132 ms 41960 KB Output is correct
48 Correct 120 ms 41980 KB Output is correct
49 Correct 130 ms 42008 KB Output is correct
50 Correct 218 ms 35420 KB Output is correct
51 Correct 197 ms 35484 KB Output is correct
52 Correct 227 ms 35600 KB Output is correct
53 Correct 192 ms 35484 KB Output is correct