#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5;
ll v[N],pos[N];
vector <ll> e[N];
struct DSU{
ll parent,score,maxx,power;
}v2[N];
ll leader(ll node){
if(v2[node].parent == node){
return node;
}else{
v2[node].parent = leader(v2[node].parent);
return v2[node].parent;
}
}
void connect(ll a,ll b){
a = leader(a);
b = leader(b);
if(v2[a].power > v2[b].power)swap(a,b);
v2[a].parent = b;
v2[b].power+=v2[a].power;
v2[b].score = max(v2[b].score,v2[a].score);
if(v[v2[b].maxx] < v[v2[a].maxx]){
v2[b].maxx = v2[a].maxx;
}
}
ll p[N][20],dpth[N];
void dfs(ll node,ll parent = 0,ll d = 0){
p[node][0] = parent;
dpth[node] = d;
for(auto i:e[node]){
if(i == parent)continue;;
dfs(i,node,d + 1);
}
}
ll LCA(ll a,ll b){
if(dpth[a] > dpth[b])swap(a,b);
for(ll i = 19;i >= 0;i--){
if(dpth[b] - (1<<i) >= dpth[a]){
b = p[b][i];
}
}
for(ll i = 19;i >= 0;i--){
if(p[a][i] != p[b][i]){
b = p[b][i];
a = p[a][i];
}
}
if(a != b)return p[a][0];
return a;
}
ll fastp(ll node,ll nr){
for(ll i = 19;i >= 0;i--){
if(nr - (1<<i) >= 0){
nr-=(1<<i);
node = p[node][i];
}
}
return node;
}
ll dist(ll a,ll b){
return dpth[a] + dpth[b] - 2*dpth[LCA(a,b)];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n,i,u,w,j;
cin>>n;
for(i= 0;i < n;i++){
cin>>v[i];pos[--v[i]] = i;
}
for(i = 0;i < n - 1;i++){
cin>>u>>w;u--;w--;
e[u].push_back(w);
e[w].push_back(u);
}
dfs(0);
for(j = 1;j < 20;j++){
for(i = 0;i < n;i++){
p[i][j] = p[p[i][j - 1]][j - 1];
}
}
for(i = 0;i < n;i++)v2[i] = {i,0,i,i};
for(i = 0;i < n;i++){
ll score = 1;
for(auto j:e[pos[i]]){
if(v[pos[i]] > v[j]){
///good and fucked already
ll l = leader(j);
score = max(v2[l].score + dist(v2[l].maxx,pos[i]),score);
}
}
//cout<<pos[i]<<' '<<score<<'\n';
v2[pos[i]].score = score;
for(auto j:e[pos[i]]){
if(v[pos[i]] > v[j]){
connect(pos[i],j);
}
}
}
cout<<v2[leader(0)].score - 1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
5036 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5040 KB |
Output is correct |
9 |
Correct |
4 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
5036 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5040 KB |
Output is correct |
9 |
Correct |
4 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
5036 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5040 KB |
Output is correct |
9 |
Correct |
4 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
18 |
Correct |
7 ms |
6452 KB |
Output is correct |
19 |
Correct |
7 ms |
6452 KB |
Output is correct |
20 |
Correct |
7 ms |
6484 KB |
Output is correct |
21 |
Correct |
8 ms |
6456 KB |
Output is correct |
22 |
Correct |
6 ms |
6460 KB |
Output is correct |
23 |
Correct |
6 ms |
6484 KB |
Output is correct |
24 |
Correct |
6 ms |
6484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
5036 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5040 KB |
Output is correct |
9 |
Correct |
4 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
18 |
Correct |
7 ms |
6452 KB |
Output is correct |
19 |
Correct |
7 ms |
6452 KB |
Output is correct |
20 |
Correct |
7 ms |
6484 KB |
Output is correct |
21 |
Correct |
8 ms |
6456 KB |
Output is correct |
22 |
Correct |
6 ms |
6460 KB |
Output is correct |
23 |
Correct |
6 ms |
6484 KB |
Output is correct |
24 |
Correct |
6 ms |
6484 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
6 ms |
6328 KB |
Output is correct |
27 |
Correct |
7 ms |
6440 KB |
Output is correct |
28 |
Correct |
6 ms |
6356 KB |
Output is correct |
29 |
Correct |
7 ms |
6484 KB |
Output is correct |
30 |
Correct |
7 ms |
6272 KB |
Output is correct |
31 |
Correct |
6 ms |
6356 KB |
Output is correct |
32 |
Correct |
7 ms |
6252 KB |
Output is correct |
33 |
Correct |
6 ms |
6324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
5036 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5040 KB |
Output is correct |
9 |
Correct |
4 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
18 |
Correct |
7 ms |
6452 KB |
Output is correct |
19 |
Correct |
7 ms |
6452 KB |
Output is correct |
20 |
Correct |
7 ms |
6484 KB |
Output is correct |
21 |
Correct |
8 ms |
6456 KB |
Output is correct |
22 |
Correct |
6 ms |
6460 KB |
Output is correct |
23 |
Correct |
6 ms |
6484 KB |
Output is correct |
24 |
Correct |
6 ms |
6484 KB |
Output is correct |
25 |
Correct |
170 ms |
63352 KB |
Output is correct |
26 |
Correct |
153 ms |
61868 KB |
Output is correct |
27 |
Correct |
161 ms |
62412 KB |
Output is correct |
28 |
Correct |
223 ms |
60372 KB |
Output is correct |
29 |
Correct |
224 ms |
60312 KB |
Output is correct |
30 |
Correct |
242 ms |
61660 KB |
Output is correct |
31 |
Correct |
227 ms |
60640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5036 KB |
Output is correct |
3 |
Correct |
253 ms |
57724 KB |
Output is correct |
4 |
Correct |
261 ms |
55516 KB |
Output is correct |
5 |
Correct |
259 ms |
56104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4960 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
5036 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5040 KB |
Output is correct |
9 |
Correct |
4 ms |
5036 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5040 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5032 KB |
Output is correct |
18 |
Correct |
7 ms |
6452 KB |
Output is correct |
19 |
Correct |
7 ms |
6452 KB |
Output is correct |
20 |
Correct |
7 ms |
6484 KB |
Output is correct |
21 |
Correct |
8 ms |
6456 KB |
Output is correct |
22 |
Correct |
6 ms |
6460 KB |
Output is correct |
23 |
Correct |
6 ms |
6484 KB |
Output is correct |
24 |
Correct |
6 ms |
6484 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
6 ms |
6328 KB |
Output is correct |
27 |
Correct |
7 ms |
6440 KB |
Output is correct |
28 |
Correct |
6 ms |
6356 KB |
Output is correct |
29 |
Correct |
7 ms |
6484 KB |
Output is correct |
30 |
Correct |
7 ms |
6272 KB |
Output is correct |
31 |
Correct |
6 ms |
6356 KB |
Output is correct |
32 |
Correct |
7 ms |
6252 KB |
Output is correct |
33 |
Correct |
6 ms |
6324 KB |
Output is correct |
34 |
Correct |
170 ms |
63352 KB |
Output is correct |
35 |
Correct |
153 ms |
61868 KB |
Output is correct |
36 |
Correct |
161 ms |
62412 KB |
Output is correct |
37 |
Correct |
223 ms |
60372 KB |
Output is correct |
38 |
Correct |
224 ms |
60312 KB |
Output is correct |
39 |
Correct |
242 ms |
61660 KB |
Output is correct |
40 |
Correct |
227 ms |
60640 KB |
Output is correct |
41 |
Correct |
3 ms |
4948 KB |
Output is correct |
42 |
Correct |
3 ms |
5036 KB |
Output is correct |
43 |
Correct |
253 ms |
57724 KB |
Output is correct |
44 |
Correct |
261 ms |
55516 KB |
Output is correct |
45 |
Correct |
259 ms |
56104 KB |
Output is correct |
46 |
Correct |
313 ms |
58536 KB |
Output is correct |
47 |
Correct |
326 ms |
57372 KB |
Output is correct |
48 |
Correct |
337 ms |
58828 KB |
Output is correct |
49 |
Correct |
318 ms |
57676 KB |
Output is correct |
50 |
Correct |
313 ms |
56884 KB |
Output is correct |
51 |
Correct |
345 ms |
57172 KB |
Output is correct |
52 |
Correct |
314 ms |
57304 KB |
Output is correct |
53 |
Correct |
331 ms |
54880 KB |
Output is correct |