#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int inf = 1000000000;
int n;
vector<int> g[maxn];
int depth[maxn];
long long dp[maxn];
int permutation[maxn];
void dfs(int node, int par, int root)
{
for (auto next_node : g[node])
{
if (next_node != par && next_node < root)
{
depth[next_node] = 1 + depth[node];
dfs(next_node, node, root);
}
}
}
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> permutation[i];
}
for (int i = 1; i <= n - 1; ++i)
{
int x, y;
cin >> x >> y;
x = permutation[x];
y = permutation[y];
g[x].push_back(y);
g[y].push_back(x);
}
dp[1] = 0;
for (int i = 2; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
depth[j] = -inf;
}
depth[i] = 0;
dfs(i, -1, i);
for (int j = i - 1; j >= 1; --j)
{
dp[i] = max(dp[i], dp[j] + depth[j]);
}
}
cout << dp[n] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5020 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5020 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5052 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5020 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5052 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5052 KB |
Output is correct |
18 |
Correct |
104 ms |
5548 KB |
Output is correct |
19 |
Correct |
96 ms |
5548 KB |
Output is correct |
20 |
Correct |
110 ms |
5612 KB |
Output is correct |
21 |
Correct |
28 ms |
5588 KB |
Output is correct |
22 |
Correct |
24 ms |
5412 KB |
Output is correct |
23 |
Correct |
24 ms |
5428 KB |
Output is correct |
24 |
Correct |
29 ms |
5580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5020 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5052 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5052 KB |
Output is correct |
18 |
Correct |
104 ms |
5548 KB |
Output is correct |
19 |
Correct |
96 ms |
5548 KB |
Output is correct |
20 |
Correct |
110 ms |
5612 KB |
Output is correct |
21 |
Correct |
28 ms |
5588 KB |
Output is correct |
22 |
Correct |
24 ms |
5412 KB |
Output is correct |
23 |
Correct |
24 ms |
5428 KB |
Output is correct |
24 |
Correct |
29 ms |
5580 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
79 ms |
5532 KB |
Output is correct |
27 |
Correct |
79 ms |
5512 KB |
Output is correct |
28 |
Correct |
78 ms |
5452 KB |
Output is correct |
29 |
Correct |
87 ms |
5492 KB |
Output is correct |
30 |
Correct |
29 ms |
5320 KB |
Output is correct |
31 |
Correct |
33 ms |
5340 KB |
Output is correct |
32 |
Correct |
29 ms |
5344 KB |
Output is correct |
33 |
Correct |
34 ms |
5316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5020 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5052 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5052 KB |
Output is correct |
18 |
Correct |
104 ms |
5548 KB |
Output is correct |
19 |
Correct |
96 ms |
5548 KB |
Output is correct |
20 |
Correct |
110 ms |
5612 KB |
Output is correct |
21 |
Correct |
28 ms |
5588 KB |
Output is correct |
22 |
Correct |
24 ms |
5412 KB |
Output is correct |
23 |
Correct |
24 ms |
5428 KB |
Output is correct |
24 |
Correct |
29 ms |
5580 KB |
Output is correct |
25 |
Execution timed out |
2058 ms |
17696 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Execution timed out |
2052 ms |
16772 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
5020 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
5020 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
5020 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5052 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
5020 KB |
Output is correct |
17 |
Correct |
3 ms |
5052 KB |
Output is correct |
18 |
Correct |
104 ms |
5548 KB |
Output is correct |
19 |
Correct |
96 ms |
5548 KB |
Output is correct |
20 |
Correct |
110 ms |
5612 KB |
Output is correct |
21 |
Correct |
28 ms |
5588 KB |
Output is correct |
22 |
Correct |
24 ms |
5412 KB |
Output is correct |
23 |
Correct |
24 ms |
5428 KB |
Output is correct |
24 |
Correct |
29 ms |
5580 KB |
Output is correct |
25 |
Correct |
2 ms |
4948 KB |
Output is correct |
26 |
Correct |
79 ms |
5532 KB |
Output is correct |
27 |
Correct |
79 ms |
5512 KB |
Output is correct |
28 |
Correct |
78 ms |
5452 KB |
Output is correct |
29 |
Correct |
87 ms |
5492 KB |
Output is correct |
30 |
Correct |
29 ms |
5320 KB |
Output is correct |
31 |
Correct |
33 ms |
5340 KB |
Output is correct |
32 |
Correct |
29 ms |
5344 KB |
Output is correct |
33 |
Correct |
34 ms |
5316 KB |
Output is correct |
34 |
Execution timed out |
2058 ms |
17696 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |