# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
737316 | 2023-05-07T04:33:16 Z | nguyentunglam | Cat Exercise (JOI23_ho_t4) | C++17 | 376 ms | 44844 KB |
#include<bits/stdc++.h> #define fi first #define se second #define endl "\n" #define ii pair<int, int> using namespace std; const int N = 2e5 + 10; int par[20][N], h[N], p[N], pos[N], lab[N]; long long f[N]; vector<int> adj[N]; void dfs(int u) { for(int j = 1; j <= 18; j++) par[j][u] = par[j - 1][par[j - 1][u]]; for(int &v : adj[u]) if (v != par[0][u]) { par[0][v] = u; h[v] = h[u] + 1; dfs(v); } } int lca(int u, int v) { if (h[u] < h[v]) swap(u, v); int delta = h[u] - h[v]; for(int j = 18; j >= 0; j--) if (delta >> j & 1) u = par[j][u]; if (u == v) return u; for(int j = 18; j >= 0; j--) if (par[j][u] != par[j][v]) { u = par[j][u]; v = par[j][v]; } return par[0][u]; } int dist(int u, int v) { return h[u] + h[v] - 2 * h[lca(u, v)]; } int root(int v) { return lab[v] == v ? v : lab[v] = root(lab[v]); } int main() { #define task "" cin.tie(0) -> sync_with_stdio(0); if (fopen ("task.inp", "r")) { freopen ("task.inp", "r", stdin); freopen ("task.out", "w", stdout); } if (fopen (task".inp", "r")) { freopen (task".inp", "r", stdin); freopen (task".out", "w", stdout); } int n; cin >> n; for(int i = 1; i <= n; i++) cin >> p[i], pos[p[i]] = i; for(int i = 1; i < n; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1); for(int i = 1; i <= n; i++) lab[i] = i; for(int i = 1; i <= n; i++) { int u = pos[i]; for(int &v : adj[u]) if (p[v] < i) { int ver = root(v); f[u] = max(f[u], f[ver] + dist(u, ver)); } for(int &v : adj[u]) if (p[v] < i) { int ver = root(v); lab[ver] = u; } } cout << f[pos[n]]; }
Compilation message
# | 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 | 3 ms | 5076 KB | Output is correct |
4 | Correct | 3 ms | 5076 KB | Output is correct |
5 | Correct | 2 ms | 5076 KB | Output is correct |
6 | Correct | 3 ms | 5204 KB | Output is correct |
7 | Correct | 3 ms | 5076 KB | Output is correct |
8 | Correct | 3 ms | 5076 KB | Output is correct |
9 | Correct | 3 ms | 5076 KB | Output is correct |
10 | Correct | 3 ms | 5076 KB | Output is correct |
# | 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 | 3 ms | 5076 KB | Output is correct |
4 | Correct | 3 ms | 5076 KB | Output is correct |
5 | Correct | 2 ms | 5076 KB | Output is correct |
6 | Correct | 3 ms | 5204 KB | Output is correct |
7 | Correct | 3 ms | 5076 KB | Output is correct |
8 | Correct | 3 ms | 5076 KB | Output is correct |
9 | Correct | 3 ms | 5076 KB | Output is correct |
10 | Correct | 3 ms | 5076 KB | Output is correct |
11 | Correct | 3 ms | 5204 KB | Output is correct |
12 | Correct | 3 ms | 5204 KB | Output is correct |
13 | Correct | 3 ms | 5204 KB | Output is correct |
14 | Correct | 3 ms | 5204 KB | Output is correct |
15 | Correct | 3 ms | 5204 KB | Output is correct |
16 | Correct | 3 ms | 5204 KB | Output is correct |
17 | Correct | 3 ms | 5204 KB | Output is correct |
# | 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 | 3 ms | 5076 KB | Output is correct |
4 | Correct | 3 ms | 5076 KB | Output is correct |
5 | Correct | 2 ms | 5076 KB | Output is correct |
6 | Correct | 3 ms | 5204 KB | Output is correct |
7 | Correct | 3 ms | 5076 KB | Output is correct |
8 | Correct | 3 ms | 5076 KB | Output is correct |
9 | Correct | 3 ms | 5076 KB | Output is correct |
10 | Correct | 3 ms | 5076 KB | Output is correct |
11 | Correct | 3 ms | 5204 KB | Output is correct |
12 | Correct | 3 ms | 5204 KB | Output is correct |
13 | Correct | 3 ms | 5204 KB | Output is correct |
14 | Correct | 3 ms | 5204 KB | Output is correct |
15 | Correct | 3 ms | 5204 KB | Output is correct |
16 | Correct | 3 ms | 5204 KB | Output is correct |
17 | Correct | 3 ms | 5204 KB | Output is correct |
18 | Correct | 6 ms | 6100 KB | Output is correct |
19 | Correct | 5 ms | 6224 KB | Output is correct |
20 | Correct | 7 ms | 6100 KB | Output is correct |
21 | Correct | 7 ms | 6100 KB | Output is correct |
22 | Correct | 5 ms | 6100 KB | Output is correct |
23 | Correct | 6 ms | 6100 KB | Output is correct |
24 | Correct | 7 ms | 6076 KB | Output is correct |
# | 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 | 3 ms | 5076 KB | Output is correct |
4 | Correct | 3 ms | 5076 KB | Output is correct |
5 | Correct | 2 ms | 5076 KB | Output is correct |
6 | Correct | 3 ms | 5204 KB | Output is correct |
7 | Correct | 3 ms | 5076 KB | Output is correct |
8 | Correct | 3 ms | 5076 KB | Output is correct |
9 | Correct | 3 ms | 5076 KB | Output is correct |
10 | Correct | 3 ms | 5076 KB | Output is correct |
11 | Correct | 3 ms | 5204 KB | Output is correct |
12 | Correct | 3 ms | 5204 KB | Output is correct |
13 | Correct | 3 ms | 5204 KB | Output is correct |
14 | Correct | 3 ms | 5204 KB | Output is correct |
15 | Correct | 3 ms | 5204 KB | Output is correct |
16 | Correct | 3 ms | 5204 KB | Output is correct |
17 | Correct | 3 ms | 5204 KB | Output is correct |
18 | Correct | 6 ms | 6100 KB | Output is correct |
19 | Correct | 5 ms | 6224 KB | Output is correct |
20 | Correct | 7 ms | 6100 KB | Output is correct |
21 | Correct | 7 ms | 6100 KB | Output is correct |
22 | Correct | 5 ms | 6100 KB | Output is correct |
23 | Correct | 6 ms | 6100 KB | Output is correct |
24 | Correct | 7 ms | 6076 KB | Output is correct |
25 | Correct | 3 ms | 5076 KB | Output is correct |
26 | Correct | 6 ms | 5884 KB | Output is correct |
27 | Correct | 6 ms | 5972 KB | Output is correct |
28 | Correct | 6 ms | 5840 KB | Output is correct |
29 | Correct | 6 ms | 5972 KB | Output is correct |
30 | Correct | 6 ms | 5820 KB | Output is correct |
31 | Correct | 6 ms | 5716 KB | Output is correct |
32 | Correct | 8 ms | 5824 KB | Output is correct |
33 | Correct | 8 ms | 5716 KB | Output is correct |
# | 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 | 3 ms | 5076 KB | Output is correct |
4 | Correct | 3 ms | 5076 KB | Output is correct |
5 | Correct | 2 ms | 5076 KB | Output is correct |
6 | Correct | 3 ms | 5204 KB | Output is correct |
7 | Correct | 3 ms | 5076 KB | Output is correct |
8 | Correct | 3 ms | 5076 KB | Output is correct |
9 | Correct | 3 ms | 5076 KB | Output is correct |
10 | Correct | 3 ms | 5076 KB | Output is correct |
11 | Correct | 3 ms | 5204 KB | Output is correct |
12 | Correct | 3 ms | 5204 KB | Output is correct |
13 | Correct | 3 ms | 5204 KB | Output is correct |
14 | Correct | 3 ms | 5204 KB | Output is correct |
15 | Correct | 3 ms | 5204 KB | Output is correct |
16 | Correct | 3 ms | 5204 KB | Output is correct |
17 | Correct | 3 ms | 5204 KB | Output is correct |
18 | Correct | 6 ms | 6100 KB | Output is correct |
19 | Correct | 5 ms | 6224 KB | Output is correct |
20 | Correct | 7 ms | 6100 KB | Output is correct |
21 | Correct | 7 ms | 6100 KB | Output is correct |
22 | Correct | 5 ms | 6100 KB | Output is correct |
23 | Correct | 6 ms | 6100 KB | Output is correct |
24 | Correct | 7 ms | 6076 KB | Output is correct |
25 | Correct | 99 ms | 43340 KB | Output is correct |
26 | Correct | 105 ms | 44456 KB | Output is correct |
27 | Correct | 108 ms | 44844 KB | Output is correct |
28 | Correct | 151 ms | 43284 KB | Output is correct |
29 | Correct | 171 ms | 43524 KB | Output is correct |
30 | Correct | 155 ms | 44172 KB | Output is correct |
31 | Correct | 148 ms | 43732 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 5076 KB | Output is correct |
2 | Correct | 3 ms | 5184 KB | Output is correct |
3 | Correct | 181 ms | 31008 KB | Output is correct |
4 | Correct | 173 ms | 30840 KB | Output is correct |
5 | Correct | 171 ms | 30844 KB | Output is correct |
# | 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 | 3 ms | 5076 KB | Output is correct |
4 | Correct | 3 ms | 5076 KB | Output is correct |
5 | Correct | 2 ms | 5076 KB | Output is correct |
6 | Correct | 3 ms | 5204 KB | Output is correct |
7 | Correct | 3 ms | 5076 KB | Output is correct |
8 | Correct | 3 ms | 5076 KB | Output is correct |
9 | Correct | 3 ms | 5076 KB | Output is correct |
10 | Correct | 3 ms | 5076 KB | Output is correct |
11 | Correct | 3 ms | 5204 KB | Output is correct |
12 | Correct | 3 ms | 5204 KB | Output is correct |
13 | Correct | 3 ms | 5204 KB | Output is correct |
14 | Correct | 3 ms | 5204 KB | Output is correct |
15 | Correct | 3 ms | 5204 KB | Output is correct |
16 | Correct | 3 ms | 5204 KB | Output is correct |
17 | Correct | 3 ms | 5204 KB | Output is correct |
18 | Correct | 6 ms | 6100 KB | Output is correct |
19 | Correct | 5 ms | 6224 KB | Output is correct |
20 | Correct | 7 ms | 6100 KB | Output is correct |
21 | Correct | 7 ms | 6100 KB | Output is correct |
22 | Correct | 5 ms | 6100 KB | Output is correct |
23 | Correct | 6 ms | 6100 KB | Output is correct |
24 | Correct | 7 ms | 6076 KB | Output is correct |
25 | Correct | 3 ms | 5076 KB | Output is correct |
26 | Correct | 6 ms | 5884 KB | Output is correct |
27 | Correct | 6 ms | 5972 KB | Output is correct |
28 | Correct | 6 ms | 5840 KB | Output is correct |
29 | Correct | 6 ms | 5972 KB | Output is correct |
30 | Correct | 6 ms | 5820 KB | Output is correct |
31 | Correct | 6 ms | 5716 KB | Output is correct |
32 | Correct | 8 ms | 5824 KB | Output is correct |
33 | Correct | 8 ms | 5716 KB | Output is correct |
34 | Correct | 99 ms | 43340 KB | Output is correct |
35 | Correct | 105 ms | 44456 KB | Output is correct |
36 | Correct | 108 ms | 44844 KB | Output is correct |
37 | Correct | 151 ms | 43284 KB | Output is correct |
38 | Correct | 171 ms | 43524 KB | Output is correct |
39 | Correct | 155 ms | 44172 KB | Output is correct |
40 | Correct | 148 ms | 43732 KB | Output is correct |
41 | Correct | 3 ms | 5076 KB | Output is correct |
42 | Correct | 3 ms | 5184 KB | Output is correct |
43 | Correct | 181 ms | 31008 KB | Output is correct |
44 | Correct | 173 ms | 30840 KB | Output is correct |
45 | Correct | 171 ms | 30844 KB | Output is correct |
46 | Correct | 327 ms | 38332 KB | Output is correct |
47 | Correct | 301 ms | 36824 KB | Output is correct |
48 | Correct | 342 ms | 39204 KB | Output is correct |
49 | Correct | 376 ms | 37320 KB | Output is correct |
50 | Correct | 349 ms | 31528 KB | Output is correct |
51 | Correct | 275 ms | 30980 KB | Output is correct |
52 | Correct | 269 ms | 30996 KB | Output is correct |
53 | Correct | 282 ms | 30968 KB | Output is correct |