#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005;
int n;
int niz[mxN];
vector<int> g[mxN];
int poz[mxN];
int d[mxN];
int deda[mxN][20];
void dfs(int v, int p){
deda[v][0] = p;
ff(i,1,19)deda[v][i] = deda[deda[v][i - 1]][i - 1];
for(auto u : g[v]){
if(u != p){
d[u] = d[v] + 1;
dfs(u, v);
}
}
}
int LCA(int x, int y){
if(d[x] < d[y])swap(x, y);
fb(i,19,0)if((d[x] - d[y]) & (1 << i))x = deda[x][i];
fb(i,19,0)if(deda[x][i] != deda[y][i])x = deda[x][i], y = deda[y][i];
return (x == y ? x : deda[x][0]);
}
int dist(int x, int y){
return d[x] + d[y] - 2 * d[LCA(x, y)];
}
ll sum[mxN];
int par[mxN];
int findpar(int x){
return (x == par[x] ? x : par[x] = findpar(par[x]));
}
void unite(int x, int y){
x = findpar(x), y = findpar(y);
par[y] = x;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
ff(i,1,n)cin >> niz[i], poz[niz[i]] = i;
ff(i,1,n - 1){
int u, v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
dfs(1, 0);
ff(i,1,n)par[i] = i;
ff(i,1,n){
int v = poz[i];
for(auto c : g[v]){
if(niz[c] < niz[v]){
int a = findpar(c);
sum[v] = max(sum[v], sum[a] + dist(a, v));
unite(v, a);
}
}
}
cout << sum[poz[n]] << '\n';
return 0;
}
/*
5
5 1 2 3 4
1 2
2 3
3 4
4 5
// probati bojenje sahovski
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5216 KB |
Output is correct |
13 |
Correct |
2 ms |
5216 KB |
Output is correct |
14 |
Correct |
4 ms |
5216 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5216 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5216 KB |
Output is correct |
13 |
Correct |
2 ms |
5216 KB |
Output is correct |
14 |
Correct |
4 ms |
5216 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5216 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
6240 KB |
Output is correct |
19 |
Correct |
5 ms |
6240 KB |
Output is correct |
20 |
Correct |
4 ms |
6256 KB |
Output is correct |
21 |
Correct |
4 ms |
6244 KB |
Output is correct |
22 |
Correct |
5 ms |
6240 KB |
Output is correct |
23 |
Correct |
4 ms |
6052 KB |
Output is correct |
24 |
Correct |
5 ms |
6240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5216 KB |
Output is correct |
13 |
Correct |
2 ms |
5216 KB |
Output is correct |
14 |
Correct |
4 ms |
5216 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5216 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
6240 KB |
Output is correct |
19 |
Correct |
5 ms |
6240 KB |
Output is correct |
20 |
Correct |
4 ms |
6256 KB |
Output is correct |
21 |
Correct |
4 ms |
6244 KB |
Output is correct |
22 |
Correct |
5 ms |
6240 KB |
Output is correct |
23 |
Correct |
4 ms |
6052 KB |
Output is correct |
24 |
Correct |
5 ms |
6240 KB |
Output is correct |
25 |
Correct |
3 ms |
4956 KB |
Output is correct |
26 |
Correct |
5 ms |
5980 KB |
Output is correct |
27 |
Correct |
5 ms |
5980 KB |
Output is correct |
28 |
Correct |
6 ms |
6240 KB |
Output is correct |
29 |
Correct |
5 ms |
5984 KB |
Output is correct |
30 |
Correct |
5 ms |
5728 KB |
Output is correct |
31 |
Correct |
5 ms |
5704 KB |
Output is correct |
32 |
Correct |
5 ms |
5724 KB |
Output is correct |
33 |
Correct |
4 ms |
5728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5216 KB |
Output is correct |
13 |
Correct |
2 ms |
5216 KB |
Output is correct |
14 |
Correct |
4 ms |
5216 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5216 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
6240 KB |
Output is correct |
19 |
Correct |
5 ms |
6240 KB |
Output is correct |
20 |
Correct |
4 ms |
6256 KB |
Output is correct |
21 |
Correct |
4 ms |
6244 KB |
Output is correct |
22 |
Correct |
5 ms |
6240 KB |
Output is correct |
23 |
Correct |
4 ms |
6052 KB |
Output is correct |
24 |
Correct |
5 ms |
6240 KB |
Output is correct |
25 |
Correct |
96 ms |
47864 KB |
Output is correct |
26 |
Correct |
97 ms |
47952 KB |
Output is correct |
27 |
Correct |
96 ms |
47980 KB |
Output is correct |
28 |
Correct |
116 ms |
47956 KB |
Output is correct |
29 |
Correct |
120 ms |
47932 KB |
Output is correct |
30 |
Correct |
120 ms |
48004 KB |
Output is correct |
31 |
Correct |
138 ms |
48064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
3 ms |
5212 KB |
Output is correct |
3 |
Correct |
152 ms |
35336 KB |
Output is correct |
4 |
Correct |
115 ms |
35408 KB |
Output is correct |
5 |
Correct |
112 ms |
35380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4952 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5216 KB |
Output is correct |
13 |
Correct |
2 ms |
5216 KB |
Output is correct |
14 |
Correct |
4 ms |
5216 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5216 KB |
Output is correct |
17 |
Correct |
2 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
6240 KB |
Output is correct |
19 |
Correct |
5 ms |
6240 KB |
Output is correct |
20 |
Correct |
4 ms |
6256 KB |
Output is correct |
21 |
Correct |
4 ms |
6244 KB |
Output is correct |
22 |
Correct |
5 ms |
6240 KB |
Output is correct |
23 |
Correct |
4 ms |
6052 KB |
Output is correct |
24 |
Correct |
5 ms |
6240 KB |
Output is correct |
25 |
Correct |
3 ms |
4956 KB |
Output is correct |
26 |
Correct |
5 ms |
5980 KB |
Output is correct |
27 |
Correct |
5 ms |
5980 KB |
Output is correct |
28 |
Correct |
6 ms |
6240 KB |
Output is correct |
29 |
Correct |
5 ms |
5984 KB |
Output is correct |
30 |
Correct |
5 ms |
5728 KB |
Output is correct |
31 |
Correct |
5 ms |
5704 KB |
Output is correct |
32 |
Correct |
5 ms |
5724 KB |
Output is correct |
33 |
Correct |
4 ms |
5728 KB |
Output is correct |
34 |
Correct |
96 ms |
47864 KB |
Output is correct |
35 |
Correct |
97 ms |
47952 KB |
Output is correct |
36 |
Correct |
96 ms |
47980 KB |
Output is correct |
37 |
Correct |
116 ms |
47956 KB |
Output is correct |
38 |
Correct |
120 ms |
47932 KB |
Output is correct |
39 |
Correct |
120 ms |
48004 KB |
Output is correct |
40 |
Correct |
138 ms |
48064 KB |
Output is correct |
41 |
Correct |
3 ms |
4952 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
152 ms |
35336 KB |
Output is correct |
44 |
Correct |
115 ms |
35408 KB |
Output is correct |
45 |
Correct |
112 ms |
35380 KB |
Output is correct |
46 |
Correct |
189 ms |
42460 KB |
Output is correct |
47 |
Correct |
187 ms |
41200 KB |
Output is correct |
48 |
Correct |
208 ms |
42836 KB |
Output is correct |
49 |
Correct |
181 ms |
41348 KB |
Output is correct |
50 |
Correct |
158 ms |
35412 KB |
Output is correct |
51 |
Correct |
147 ms |
35408 KB |
Output is correct |
52 |
Correct |
173 ms |
35664 KB |
Output is correct |
53 |
Correct |
145 ms |
35412 KB |
Output is correct |