#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 2e5 + 5;
int a[N];
int dep[N];
vector<int> g[N];
vector<pair<int, int>> t[N];
int dfs(int root, int v, int p) {
if (a[v] > a[root]) {
return 0;
}
int ret = v;
for (int u : g[v]) {
if (u == p) {
continue;
}
dep[u] = dep[v] + 1;
int ret2 = dfs(root, u, v);
if (a[ret2] > a[ret]) {
ret = ret2;
}
if (v == root) {
t[v].push_back({ret2, dep[ret2]});
}
}
return ret;
}
long long dfs2(int v) {
long long ret = 0;
for (auto [u, w] : t[v]) {
long long ret2 = dfs2(u);
ret = max(ret, ret2 + w);
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int root = 1;
for (int i = 1; i <= n; ++i) {
if (a[i] == n) {
root = i;
}
}
for (int i = 1; i <= n; ++i) {
dep[i] = 0;
dfs(i, i, i);
}
cout << dfs2(root);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
10072 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
10072 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9804 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
10072 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9804 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
110 ms |
10588 KB |
Output is correct |
19 |
Correct |
116 ms |
10588 KB |
Output is correct |
20 |
Correct |
99 ms |
10588 KB |
Output is correct |
21 |
Correct |
7 ms |
10584 KB |
Output is correct |
22 |
Correct |
9 ms |
10588 KB |
Output is correct |
23 |
Correct |
11 ms |
10388 KB |
Output is correct |
24 |
Correct |
6 ms |
10332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
10072 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9804 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
110 ms |
10588 KB |
Output is correct |
19 |
Correct |
116 ms |
10588 KB |
Output is correct |
20 |
Correct |
99 ms |
10588 KB |
Output is correct |
21 |
Correct |
7 ms |
10584 KB |
Output is correct |
22 |
Correct |
9 ms |
10588 KB |
Output is correct |
23 |
Correct |
11 ms |
10388 KB |
Output is correct |
24 |
Correct |
6 ms |
10332 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
125 ms |
10580 KB |
Output is correct |
27 |
Correct |
115 ms |
10576 KB |
Output is correct |
28 |
Correct |
117 ms |
10580 KB |
Output is correct |
29 |
Correct |
120 ms |
10588 KB |
Output is correct |
30 |
Correct |
11 ms |
10076 KB |
Output is correct |
31 |
Correct |
21 ms |
10076 KB |
Output is correct |
32 |
Correct |
12 ms |
10080 KB |
Output is correct |
33 |
Correct |
14 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
10072 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9804 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
110 ms |
10588 KB |
Output is correct |
19 |
Correct |
116 ms |
10588 KB |
Output is correct |
20 |
Correct |
99 ms |
10588 KB |
Output is correct |
21 |
Correct |
7 ms |
10584 KB |
Output is correct |
22 |
Correct |
9 ms |
10588 KB |
Output is correct |
23 |
Correct |
11 ms |
10388 KB |
Output is correct |
24 |
Correct |
6 ms |
10332 KB |
Output is correct |
25 |
Execution timed out |
2021 ms |
43432 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Execution timed out |
2087 ms |
23032 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
4 ms |
9820 KB |
Output is correct |
3 |
Correct |
5 ms |
9820 KB |
Output is correct |
4 |
Correct |
5 ms |
9820 KB |
Output is correct |
5 |
Correct |
4 ms |
9820 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9820 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
5 ms |
10072 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
11 |
Correct |
6 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9804 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
4 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
9820 KB |
Output is correct |
16 |
Correct |
5 ms |
9820 KB |
Output is correct |
17 |
Correct |
4 ms |
9820 KB |
Output is correct |
18 |
Correct |
110 ms |
10588 KB |
Output is correct |
19 |
Correct |
116 ms |
10588 KB |
Output is correct |
20 |
Correct |
99 ms |
10588 KB |
Output is correct |
21 |
Correct |
7 ms |
10584 KB |
Output is correct |
22 |
Correct |
9 ms |
10588 KB |
Output is correct |
23 |
Correct |
11 ms |
10388 KB |
Output is correct |
24 |
Correct |
6 ms |
10332 KB |
Output is correct |
25 |
Correct |
4 ms |
9820 KB |
Output is correct |
26 |
Correct |
125 ms |
10580 KB |
Output is correct |
27 |
Correct |
115 ms |
10576 KB |
Output is correct |
28 |
Correct |
117 ms |
10580 KB |
Output is correct |
29 |
Correct |
120 ms |
10588 KB |
Output is correct |
30 |
Correct |
11 ms |
10076 KB |
Output is correct |
31 |
Correct |
21 ms |
10076 KB |
Output is correct |
32 |
Correct |
12 ms |
10080 KB |
Output is correct |
33 |
Correct |
14 ms |
10076 KB |
Output is correct |
34 |
Execution timed out |
2021 ms |
43432 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |