Submission #894207

# Submission time Handle Problem Language Result Execution time Memory
894207 2023-12-28T02:54:58 Z box Cat Exercise (JOI23_ho_t4) C++17
100 / 100
276 ms 45652 KB
#include "bits/stdc++.h"

using namespace std;

const int N = 2e5, L = 18;

#ifndef LOCAL
#define cerr if (0) cerr
#endif

int n, p[N];
int pp[L][N], d[N];
vector <int> g[N];
long long dp[N];
int f[N];

int uf(int i) {
    return f[i] == i ? i : f[i] = uf(f[i]);
}

void dfs(int i) {
    for (int l = 1; l < L; l++) pp[l][i] = pp[l - 1][pp[l - 1][i]];
    for (int j: g[i])
        if (pp[0][i] != j) {
            pp[0][j] = i;
            d[j] = d[i] + 1;
            dfs(j);
        }
}
int lca(int i, int j) {
    if (d[i] < d[j]) swap(i, j);
    int k = d[i] - d[j];
    for (int l = 0; l < L; l++)
        if (k >> l & 1) i = pp[l][i];
    if (i == j) return i;
    for (int l = L - 1; l >= 0; l--)
        if (pp[l][i] != pp[l][j]) i = pp[l][i], j = pp[l][j];
    return pp[0][i];
}
int dis(int i, int j) {
    return d[i] + d[j] - 2 * d[lca(i, j)];
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> p[i], p[i]--;
    for (int h = 0; h < n - 1; h++) {
        int i, j;
        cin >> i >> j, i--, j--;
        i = p[i], j = p[j];
        cerr << "edge " << i << " " << j << endl;
        g[i].push_back(j);
        g[j].push_back(i);
    }
    pp[0][0] = 0;
    dfs(0);
    for (int i = 0; i < n; i++) f[i] = i;
    for (int i = 0; i < n; i++) {
        for (int j: g[i])
            if (j < i) {
                int c = uf(j);
                cerr << c << " " << i << "|" << dis(c, i) << endl;
                dp[i] = max(dp[i], dp[c] + dis(c, i));
                f[c] = i;
            }
        cerr << "dp[" << i << "]=" << dp[i] << endl;
    }
    cout << * max_element(dp, dp + n) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18776 KB Output is correct
8 Correct 3 ms 19032 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18776 KB Output is correct
8 Correct 3 ms 19032 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18780 KB Output is correct
11 Correct 3 ms 18780 KB Output is correct
12 Correct 3 ms 18920 KB Output is correct
13 Correct 3 ms 18960 KB Output is correct
14 Correct 3 ms 18780 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18780 KB Output is correct
17 Correct 3 ms 18776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18776 KB Output is correct
8 Correct 3 ms 19032 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18780 KB Output is correct
11 Correct 3 ms 18780 KB Output is correct
12 Correct 3 ms 18920 KB Output is correct
13 Correct 3 ms 18960 KB Output is correct
14 Correct 3 ms 18780 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18780 KB Output is correct
17 Correct 3 ms 18776 KB Output is correct
18 Correct 7 ms 19288 KB Output is correct
19 Correct 6 ms 19548 KB Output is correct
20 Correct 13 ms 19548 KB Output is correct
21 Correct 7 ms 19288 KB Output is correct
22 Correct 7 ms 19292 KB Output is correct
23 Correct 7 ms 19292 KB Output is correct
24 Correct 6 ms 19288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18776 KB Output is correct
8 Correct 3 ms 19032 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18780 KB Output is correct
11 Correct 3 ms 18780 KB Output is correct
12 Correct 3 ms 18920 KB Output is correct
13 Correct 3 ms 18960 KB Output is correct
14 Correct 3 ms 18780 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18780 KB Output is correct
17 Correct 3 ms 18776 KB Output is correct
18 Correct 7 ms 19288 KB Output is correct
19 Correct 6 ms 19548 KB Output is correct
20 Correct 13 ms 19548 KB Output is correct
21 Correct 7 ms 19288 KB Output is correct
22 Correct 7 ms 19292 KB Output is correct
23 Correct 7 ms 19292 KB Output is correct
24 Correct 6 ms 19288 KB Output is correct
25 Correct 3 ms 18780 KB Output is correct
26 Correct 7 ms 19292 KB Output is correct
27 Correct 7 ms 19524 KB Output is correct
28 Correct 7 ms 19404 KB Output is correct
29 Correct 7 ms 19292 KB Output is correct
30 Correct 8 ms 19036 KB Output is correct
31 Correct 7 ms 19196 KB Output is correct
32 Correct 7 ms 19036 KB Output is correct
33 Correct 7 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18776 KB Output is correct
8 Correct 3 ms 19032 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18780 KB Output is correct
11 Correct 3 ms 18780 KB Output is correct
12 Correct 3 ms 18920 KB Output is correct
13 Correct 3 ms 18960 KB Output is correct
14 Correct 3 ms 18780 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18780 KB Output is correct
17 Correct 3 ms 18776 KB Output is correct
18 Correct 7 ms 19288 KB Output is correct
19 Correct 6 ms 19548 KB Output is correct
20 Correct 13 ms 19548 KB Output is correct
21 Correct 7 ms 19288 KB Output is correct
22 Correct 7 ms 19292 KB Output is correct
23 Correct 7 ms 19292 KB Output is correct
24 Correct 6 ms 19288 KB Output is correct
25 Correct 185 ms 39304 KB Output is correct
26 Correct 189 ms 45476 KB Output is correct
27 Correct 196 ms 45652 KB Output is correct
28 Correct 228 ms 44940 KB Output is correct
29 Correct 231 ms 39364 KB Output is correct
30 Correct 244 ms 43348 KB Output is correct
31 Correct 276 ms 42540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18780 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 272 ms 32916 KB Output is correct
4 Correct 227 ms 32968 KB Output is correct
5 Correct 226 ms 32852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 18776 KB Output is correct
2 Correct 3 ms 18780 KB Output is correct
3 Correct 3 ms 18776 KB Output is correct
4 Correct 3 ms 18776 KB Output is correct
5 Correct 3 ms 18780 KB Output is correct
6 Correct 3 ms 18780 KB Output is correct
7 Correct 3 ms 18776 KB Output is correct
8 Correct 3 ms 19032 KB Output is correct
9 Correct 3 ms 18780 KB Output is correct
10 Correct 3 ms 18780 KB Output is correct
11 Correct 3 ms 18780 KB Output is correct
12 Correct 3 ms 18920 KB Output is correct
13 Correct 3 ms 18960 KB Output is correct
14 Correct 3 ms 18780 KB Output is correct
15 Correct 3 ms 18776 KB Output is correct
16 Correct 3 ms 18780 KB Output is correct
17 Correct 3 ms 18776 KB Output is correct
18 Correct 7 ms 19288 KB Output is correct
19 Correct 6 ms 19548 KB Output is correct
20 Correct 13 ms 19548 KB Output is correct
21 Correct 7 ms 19288 KB Output is correct
22 Correct 7 ms 19292 KB Output is correct
23 Correct 7 ms 19292 KB Output is correct
24 Correct 6 ms 19288 KB Output is correct
25 Correct 3 ms 18780 KB Output is correct
26 Correct 7 ms 19292 KB Output is correct
27 Correct 7 ms 19524 KB Output is correct
28 Correct 7 ms 19404 KB Output is correct
29 Correct 7 ms 19292 KB Output is correct
30 Correct 8 ms 19036 KB Output is correct
31 Correct 7 ms 19196 KB Output is correct
32 Correct 7 ms 19036 KB Output is correct
33 Correct 7 ms 19036 KB Output is correct
34 Correct 185 ms 39304 KB Output is correct
35 Correct 189 ms 45476 KB Output is correct
36 Correct 196 ms 45652 KB Output is correct
37 Correct 228 ms 44940 KB Output is correct
38 Correct 231 ms 39364 KB Output is correct
39 Correct 244 ms 43348 KB Output is correct
40 Correct 276 ms 42540 KB Output is correct
41 Correct 3 ms 18780 KB Output is correct
42 Correct 3 ms 18780 KB Output is correct
43 Correct 272 ms 32916 KB Output is correct
44 Correct 227 ms 32968 KB Output is correct
45 Correct 226 ms 32852 KB Output is correct
46 Correct 222 ms 41800 KB Output is correct
47 Correct 223 ms 41852 KB Output is correct
48 Correct 216 ms 41836 KB Output is correct
49 Correct 207 ms 41812 KB Output is correct
50 Correct 252 ms 33100 KB Output is correct
51 Correct 271 ms 33108 KB Output is correct
52 Correct 246 ms 33152 KB Output is correct
53 Correct 262 ms 33132 KB Output is correct