답안 #960988

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960988 2024-04-11T10:36:37 Z Andrei_ierdnA Cat Exercise (JOI23_ho_t4) C++17
100 / 100
145 ms 62544 KB
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int n, i, h[200010], u, v, p[200010], depth[200010];
vector<int> nb[200010];
long long dp[200010];

namespace euler
{
    int t, sparseMin[19][400010], firstPoz[200010];
    void appendNode(int node)
    {
        sparseMin[0][++t] = depth[node];
    }
    void appendNodeFirst(int node)
    {
        appendNode(node);
        firstPoz[node] = t;
    }
    void computeSparse()
    {
        for (int i = 1; (1 << i) <= t; i++) {
            for (int j = 1; j + (1 << i) - 1 <= t; j++) {
                sparseMin[i][j] = min(sparseMin[i-1][j], sparseMin[i-1][j+(1<<(i-1))]);
            }
        }
    }
    int querySparseMin(int l, int r)
    {
        int len = 31 - __builtin_clz(r-l+1);
        return min(sparseMin[len][l], sparseMin[len][r-(1<<len)+1]);
    }
    int getNodeDist(int a, int b)
    {
        int minDepth = querySparseMin(min(firstPoz[a], firstPoz[b]), max(firstPoz[a], firstPoz[b]));
        return depth[a] + depth[b] - 2*minDepth;
    }
}

namespace dsu
{
    int nrdsu, dsu[200010], maxi[200010];
    void init()
    {
        nrdsu = n;
        for (int i = 1; i <= n; i++) {
            dsu[i] = -1;
            maxi[i] = i;
        }
    }
    int getRoot(int node)
    {
        if (dsu[node] < 0) return node;
        return dsu[node] = getRoot(dsu[node]);
    }
    bool dsuMerge(int a, int b)
    {
        a = getRoot(a);
        b = getRoot(b);
        if (a == b) return false;
        if (dsu[a] > dsu[b]) swap(a, b);
        dsu[a] += dsu[b];
        dsu[b] = a;
        maxi[a] = max(maxi[a], maxi[b]);
        maxi[b] = 0;
        nrdsu--;
        return true;
    }
}

void dfs1(int node)
{
    euler::appendNodeFirst(node);
    for (auto x : nb[node]) {
        if (!p[x]) {
            p[x] = node;
            depth[x] = depth[node] + 1;
            dfs1(x);
            euler::appendNode(node);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for (i = 1; i <= n; i++) {
        cin >> h[i];
    }
    for (i = 1; i < n; i++) {
        cin >> u >> v;
        u = h[u];
        v = h[v];
        nb[u].push_back(v);
        nb[v].push_back(u);
    }
    p[1] = 1;
    depth[1] = 1;
    dfs1(1);
    euler::computeSparse();
    dsu::init();
    dp[1] = 0;
    for (int i = 2; i <= n; i++) {
        for (auto x : nb[i]) {
            if (x <= i) {
                int bigboy = dsu::maxi[dsu::getRoot(x)];
                dp[i] = max(dp[i], euler::getNodeDist(i, bigboy) + dp[bigboy]);
                dsu::dsuMerge(i, x);
            }
        }
    }
    cout << dp[n];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 16896 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16876 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 2 ms 16780 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 16896 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16876 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 2 ms 16780 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 4 ms 22876 KB Output is correct
13 Correct 4 ms 22876 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 4 ms 22940 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 16896 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16876 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 2 ms 16780 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 4 ms 22876 KB Output is correct
13 Correct 4 ms 22876 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 4 ms 22940 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
18 Correct 5 ms 29532 KB Output is correct
19 Correct 5 ms 29788 KB Output is correct
20 Correct 6 ms 29788 KB Output is correct
21 Correct 6 ms 29532 KB Output is correct
22 Correct 6 ms 29784 KB Output is correct
23 Correct 6 ms 29496 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 16896 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16876 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 2 ms 16780 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 4 ms 22876 KB Output is correct
13 Correct 4 ms 22876 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 4 ms 22940 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
18 Correct 5 ms 29532 KB Output is correct
19 Correct 5 ms 29788 KB Output is correct
20 Correct 6 ms 29788 KB Output is correct
21 Correct 6 ms 29532 KB Output is correct
22 Correct 6 ms 29784 KB Output is correct
23 Correct 6 ms 29496 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
25 Correct 2 ms 14684 KB Output is correct
26 Correct 6 ms 29628 KB Output is correct
27 Correct 6 ms 29532 KB Output is correct
28 Correct 6 ms 29532 KB Output is correct
29 Correct 6 ms 29532 KB Output is correct
30 Correct 6 ms 29276 KB Output is correct
31 Correct 6 ms 29276 KB Output is correct
32 Correct 6 ms 29276 KB Output is correct
33 Correct 6 ms 29276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 16896 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16876 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 2 ms 16780 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 4 ms 22876 KB Output is correct
13 Correct 4 ms 22876 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 4 ms 22940 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
18 Correct 5 ms 29532 KB Output is correct
19 Correct 5 ms 29788 KB Output is correct
20 Correct 6 ms 29788 KB Output is correct
21 Correct 6 ms 29532 KB Output is correct
22 Correct 6 ms 29784 KB Output is correct
23 Correct 6 ms 29496 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
25 Correct 85 ms 56328 KB Output is correct
26 Correct 73 ms 62544 KB Output is correct
27 Correct 75 ms 62372 KB Output is correct
28 Correct 99 ms 61948 KB Output is correct
29 Correct 145 ms 56396 KB Output is correct
30 Correct 95 ms 60320 KB Output is correct
31 Correct 102 ms 59104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14680 KB Output is correct
2 Correct 4 ms 22876 KB Output is correct
3 Correct 111 ms 50048 KB Output is correct
4 Correct 99 ms 49948 KB Output is correct
5 Correct 113 ms 50000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 3 ms 16896 KB Output is correct
3 Correct 3 ms 16732 KB Output is correct
4 Correct 3 ms 16732 KB Output is correct
5 Correct 3 ms 16876 KB Output is correct
6 Correct 3 ms 16732 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 2 ms 16780 KB Output is correct
10 Correct 3 ms 16732 KB Output is correct
11 Correct 3 ms 22876 KB Output is correct
12 Correct 4 ms 22876 KB Output is correct
13 Correct 4 ms 22876 KB Output is correct
14 Correct 4 ms 22876 KB Output is correct
15 Correct 3 ms 22876 KB Output is correct
16 Correct 4 ms 22940 KB Output is correct
17 Correct 4 ms 22876 KB Output is correct
18 Correct 5 ms 29532 KB Output is correct
19 Correct 5 ms 29788 KB Output is correct
20 Correct 6 ms 29788 KB Output is correct
21 Correct 6 ms 29532 KB Output is correct
22 Correct 6 ms 29784 KB Output is correct
23 Correct 6 ms 29496 KB Output is correct
24 Correct 6 ms 29788 KB Output is correct
25 Correct 2 ms 14684 KB Output is correct
26 Correct 6 ms 29628 KB Output is correct
27 Correct 6 ms 29532 KB Output is correct
28 Correct 6 ms 29532 KB Output is correct
29 Correct 6 ms 29532 KB Output is correct
30 Correct 6 ms 29276 KB Output is correct
31 Correct 6 ms 29276 KB Output is correct
32 Correct 6 ms 29276 KB Output is correct
33 Correct 6 ms 29276 KB Output is correct
34 Correct 85 ms 56328 KB Output is correct
35 Correct 73 ms 62544 KB Output is correct
36 Correct 75 ms 62372 KB Output is correct
37 Correct 99 ms 61948 KB Output is correct
38 Correct 145 ms 56396 KB Output is correct
39 Correct 95 ms 60320 KB Output is correct
40 Correct 102 ms 59104 KB Output is correct
41 Correct 2 ms 14680 KB Output is correct
42 Correct 4 ms 22876 KB Output is correct
43 Correct 111 ms 50048 KB Output is correct
44 Correct 99 ms 49948 KB Output is correct
45 Correct 113 ms 50000 KB Output is correct
46 Correct 88 ms 58820 KB Output is correct
47 Correct 104 ms 58688 KB Output is correct
48 Correct 88 ms 58708 KB Output is correct
49 Correct 85 ms 58708 KB Output is correct
50 Correct 119 ms 50204 KB Output is correct
51 Correct 116 ms 50068 KB Output is correct
52 Correct 122 ms 50144 KB Output is correct
53 Correct 141 ms 50512 KB Output is correct