Submission #916510

# Submission time Handle Problem Language Result Execution time Memory
916510 2024-01-26T03:14:03 Z GrandTiger1729 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
536 ms 95228 KB
#include <bits/stdc++.h>
using namespace std;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        a[i]--;
    }
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> dep(n);
    vector<vector<int>> up(n, vector<int>(__lg(n) + 1));
    {
        vector<bool> vis(n);
        function<void(int)> dfs = [&](int u)
        {
            vis[u] = 1;
            for (auto &v : g[u])
            {
                if (!vis[v])
                {
                    dep[v] = dep[u] + 1;
                    dfs(v);
                    up[v][0] = u;
                }
            }
        };
        dfs(0);
        for (int i = 1; i <= __lg(n); i++)
        {
            for (int u = 0; u < n; u++)
            {
                up[u][i] = up[up[u][i - 1]][i - 1];
            }
        }
    }
    auto lca = [&](int u, int v)
    {
        if (dep[u] < dep[v])
        {
            swap(u, v);
        }
        for (int i = __lg(n); i >= 0; i--)
        {
            if (dep[up[u][i]] >= dep[v])
            {
                u = up[u][i];
            }
        }
        if (u == v)
        {
            return u;
        }
        for (int i = __lg(n); i >= 0; i--)
        {
            if (up[u][i] != up[v][i])
            {
                u = up[u][i];
                v = up[v][i];
            }
        }
        return up[u][0];
    };
    auto dis = [&](int u, int v)
    {
        return dep[u] + dep[v] - 2 * dep[lca(u, v)];
    };
    vector<vector<pair<int, int>>> G(n);
    {
        int rt = find(a.begin(), a.end(), n - 1) - a.begin();
        vector<bool> vis(n);
        function<set<pair<int, int>>(int)> dfs = [&](int u) -> set<pair<int, int>>
        {
            set<pair<int, int>> ret;
            ret.emplace(a[u], u);
            vis[u] = 1;
            for (auto &v : g[u])
            {
                if (!vis[v])
                {
                    auto st = dfs(v);
                    st.emplace(a[u], u);
                    while (st.size() >= 2 && st.begin()->first < a[u])
                    {
                        auto x = *st.begin();
                        st.erase(st.begin());
                        auto y = *st.begin();
                        G[y.first].emplace_back(x.first, dis(y.second, x.second));
                    }
                    if (ret.size() < st.size())
                    {
                        swap(ret, st);
                    }
                    for (auto &x : st)
                    {
                        ret.insert(x);
                    }
                }
            }
            return ret;
        };
        dfs(rt);
    }
    vector<int> vis(n);
    vector<long long> dp(n);
    function<void(int)> dfs = [&](int u)
    {
        vis[u] = 1;
        for (auto &[v, w] : G[u])
        {
            if (!vis[v])
            {
                dfs(v);
                dp[u] = max(dp[u], dp[v] + w);
            }
        }
    };
    dfs(n - 1);
    cout << dp[n - 1] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 4 ms 2648 KB Output is correct
19 Correct 6 ms 2652 KB Output is correct
20 Correct 4 ms 2652 KB Output is correct
21 Correct 4 ms 2396 KB Output is correct
22 Correct 4 ms 2140 KB Output is correct
23 Correct 4 ms 2140 KB Output is correct
24 Correct 4 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 4 ms 2648 KB Output is correct
19 Correct 6 ms 2652 KB Output is correct
20 Correct 4 ms 2652 KB Output is correct
21 Correct 4 ms 2396 KB Output is correct
22 Correct 4 ms 2140 KB Output is correct
23 Correct 4 ms 2140 KB Output is correct
24 Correct 4 ms 1884 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 5 ms 2396 KB Output is correct
27 Correct 5 ms 2136 KB Output is correct
28 Correct 5 ms 2396 KB Output is correct
29 Correct 4 ms 2396 KB Output is correct
30 Correct 5 ms 1372 KB Output is correct
31 Correct 4 ms 1372 KB Output is correct
32 Correct 4 ms 1372 KB Output is correct
33 Correct 4 ms 1452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 4 ms 2648 KB Output is correct
19 Correct 6 ms 2652 KB Output is correct
20 Correct 4 ms 2652 KB Output is correct
21 Correct 4 ms 2396 KB Output is correct
22 Correct 4 ms 2140 KB Output is correct
23 Correct 4 ms 2140 KB Output is correct
24 Correct 4 ms 1884 KB Output is correct
25 Correct 240 ms 91492 KB Output is correct
26 Correct 248 ms 95228 KB Output is correct
27 Correct 240 ms 95172 KB Output is correct
28 Correct 236 ms 81356 KB Output is correct
29 Correct 224 ms 88196 KB Output is correct
30 Correct 227 ms 86352 KB Output is correct
31 Correct 240 ms 88664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 214 ms 45788 KB Output is correct
4 Correct 219 ms 46136 KB Output is correct
5 Correct 240 ms 45780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 4 ms 2648 KB Output is correct
19 Correct 6 ms 2652 KB Output is correct
20 Correct 4 ms 2652 KB Output is correct
21 Correct 4 ms 2396 KB Output is correct
22 Correct 4 ms 2140 KB Output is correct
23 Correct 4 ms 2140 KB Output is correct
24 Correct 4 ms 1884 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 5 ms 2396 KB Output is correct
27 Correct 5 ms 2136 KB Output is correct
28 Correct 5 ms 2396 KB Output is correct
29 Correct 4 ms 2396 KB Output is correct
30 Correct 5 ms 1372 KB Output is correct
31 Correct 4 ms 1372 KB Output is correct
32 Correct 4 ms 1372 KB Output is correct
33 Correct 4 ms 1452 KB Output is correct
34 Correct 240 ms 91492 KB Output is correct
35 Correct 248 ms 95228 KB Output is correct
36 Correct 240 ms 95172 KB Output is correct
37 Correct 236 ms 81356 KB Output is correct
38 Correct 224 ms 88196 KB Output is correct
39 Correct 227 ms 86352 KB Output is correct
40 Correct 240 ms 88664 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 214 ms 45788 KB Output is correct
44 Correct 219 ms 46136 KB Output is correct
45 Correct 240 ms 45780 KB Output is correct
46 Correct 496 ms 81936 KB Output is correct
47 Correct 499 ms 81744 KB Output is correct
48 Correct 536 ms 87116 KB Output is correct
49 Correct 502 ms 81980 KB Output is correct
50 Correct 396 ms 48724 KB Output is correct
51 Correct 498 ms 48832 KB Output is correct
52 Correct 440 ms 48724 KB Output is correct
53 Correct 402 ms 48852 KB Output is correct