#include <bits/stdc++.h>
#define loop(i,a,b) for(int i = (a); i < (b); i ++)
#define pb push_back
#define ins insert
#define pii pair<int,int>
#define ff first
#define ss second
#define op(x) cerr << #x << " = " << x << endl;
#define opa(x) cerr << #x << " = " << x << ", ";
#define spac cerr << ' ';
#define entr cerr << endl;
#define STL(x) cerr << #x << " : "; for(auto &qwe:x) cerr << qwe << ' '; cerr << endl;
#define ARR(x, nnn) cerr << #x << " : "; loop(qwe,0,nnn) cerr << x[qwe] << ' '; cerr << endl;
#define bye exit(0);
using namespace std;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
inline int rng(int l, int r){ return uniform_int_distribution<int>(l, r)(RNG); }
ostream& operator<<(ostream& os, pii A){ os << "[" << A.ff << ", " << A.ss << "]"; }
const int mxn = (int)(2e5) + 10;
int a[mxn];
vector<int> E[mxn];
bool took[mxn];
long long dp[mxn];
void find_max(int v, int par, int dis, int& pos_dis, int& pos, int& mx){
if(a[v] > mx){
mx = a[v];
pos = v;
pos_dis = dis;
}
for(auto &i:E[v]){
if(i == par) continue;
if(!took[i]){
find_max(i, v, dis + 1, pos_dis, pos, mx);
}
}
}
void dfs(int v){
took[v] = true;
for(auto &i:E[v]){
if(!took[i]){
int mx_pos, mx_val = -999, det;
find_max(i, v, 1, det, mx_pos, mx_val);
dfs(mx_pos);
dp[v] = max(dp[v], dp[mx_pos] + det);
}
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
loop(i,0,n) cin >> a[i];
int st;
loop(i,0,n) if(a[i] == n) st = i;
loop(i,0,n-1){
int u, v;
cin >> u >> v;
u--; v--;
E[u].pb(v);
E[v].pb(u);
}
dfs(st);
cout << dp[st] << '\n';
}
/*
5
1 5 3 4 5
*/
Compilation message
Main.cpp: In function 'std::ostream& operator<<(std::ostream&, std::pair<int, int>)':
Main.cpp:18:84: warning: no return statement in function returning non-void [-Wreturn-type]
18 | ostream& operator<<(ostream& os, pii A){ os << "[" << A.ff << ", " << A.ss << "]"; }
| ^
Main.cpp: In function 'int main()':
Main.cpp:67:18: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | cout << dp[st] << '\n';
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6784 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
7256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6784 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
7256 KB |
Output is correct |
18 |
Correct |
87 ms |
7260 KB |
Output is correct |
19 |
Correct |
79 ms |
7256 KB |
Output is correct |
20 |
Correct |
94 ms |
7300 KB |
Output is correct |
21 |
Correct |
3 ms |
7256 KB |
Output is correct |
22 |
Correct |
3 ms |
7004 KB |
Output is correct |
23 |
Correct |
4 ms |
7004 KB |
Output is correct |
24 |
Correct |
4 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6784 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
7256 KB |
Output is correct |
18 |
Correct |
87 ms |
7260 KB |
Output is correct |
19 |
Correct |
79 ms |
7256 KB |
Output is correct |
20 |
Correct |
94 ms |
7300 KB |
Output is correct |
21 |
Correct |
3 ms |
7256 KB |
Output is correct |
22 |
Correct |
3 ms |
7004 KB |
Output is correct |
23 |
Correct |
4 ms |
7004 KB |
Output is correct |
24 |
Correct |
4 ms |
7004 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
84 ms |
7248 KB |
Output is correct |
27 |
Correct |
85 ms |
7760 KB |
Output is correct |
28 |
Correct |
86 ms |
7288 KB |
Output is correct |
29 |
Correct |
83 ms |
7300 KB |
Output is correct |
30 |
Correct |
7 ms |
7032 KB |
Output is correct |
31 |
Correct |
11 ms |
7012 KB |
Output is correct |
32 |
Correct |
7 ms |
7052 KB |
Output is correct |
33 |
Correct |
11 ms |
7060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6784 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
7256 KB |
Output is correct |
18 |
Correct |
87 ms |
7260 KB |
Output is correct |
19 |
Correct |
79 ms |
7256 KB |
Output is correct |
20 |
Correct |
94 ms |
7300 KB |
Output is correct |
21 |
Correct |
3 ms |
7256 KB |
Output is correct |
22 |
Correct |
3 ms |
7004 KB |
Output is correct |
23 |
Correct |
4 ms |
7004 KB |
Output is correct |
24 |
Correct |
4 ms |
7004 KB |
Output is correct |
25 |
Execution timed out |
2051 ms |
29656 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Execution timed out |
2054 ms |
17596 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6784 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
7256 KB |
Output is correct |
18 |
Correct |
87 ms |
7260 KB |
Output is correct |
19 |
Correct |
79 ms |
7256 KB |
Output is correct |
20 |
Correct |
94 ms |
7300 KB |
Output is correct |
21 |
Correct |
3 ms |
7256 KB |
Output is correct |
22 |
Correct |
3 ms |
7004 KB |
Output is correct |
23 |
Correct |
4 ms |
7004 KB |
Output is correct |
24 |
Correct |
4 ms |
7004 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
84 ms |
7248 KB |
Output is correct |
27 |
Correct |
85 ms |
7760 KB |
Output is correct |
28 |
Correct |
86 ms |
7288 KB |
Output is correct |
29 |
Correct |
83 ms |
7300 KB |
Output is correct |
30 |
Correct |
7 ms |
7032 KB |
Output is correct |
31 |
Correct |
11 ms |
7012 KB |
Output is correct |
32 |
Correct |
7 ms |
7052 KB |
Output is correct |
33 |
Correct |
11 ms |
7060 KB |
Output is correct |
34 |
Execution timed out |
2051 ms |
29656 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |