#include<bits/stdc++.h>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 2e5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const ll inf = 1e9;
const ld pre = 1e-6;
int n;
int a[mxn + 1], dep[mxn + 1], up[mxn + 1][18], p[mxn + 1], to[mxn + 1];
ll dp[mxn + 1];
vt<int>adj[mxn + 1];
vt<pii>g[mxn + 1];
int lca(int u, int v){
if(dep[u] < dep[v])swap(u, v);
int k = dep[u] - dep[v];
for(int i = 0; i < 18; i++){
if(k & (1 << i)){
u = up[u][i];
}
}
if(u == v)return(u);
for(int i = 17; i >= 0; i--){
if(up[u][i] != up[v][i]){
u = up[u][i]; v = up[v][i];
}
}
return(up[u][0]);
}
void dfs2(int s){
for(auto [i, w]: g[s]){
dfs2(i);
dp[s] = max(dp[s], dp[i] + w);
}
}
void dfs(int s, int pre){
for(auto i: adj[s]){
if(i != pre){
dep[i] = dep[s] + 1; up[i][0] = s;
dfs(i, s);
}
}
}
int fp(int u){
if(p[u] == u)return(u);
return(p[u] = fp(p[u]));
}
void unon(int u, int v){
u = fp(u); v = fp(v);
if(u == v)return;
if(a[u] < a[v])swap(u, v);
p[v] = u;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
forr(i, 1, n + 1){
cin >> a[i]; to[a[i]] = i;
}
forr(i, 1, n){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
dfs(1, -1);
forr(j, 1, 18){
forr(i, 1, n + 1){
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
forr(i, 1, n + 1)p[i] = i;
for(int i = 1; i <= n; i++){
int id = to[i];
for(auto j: adj[id]){
if(a[j] < a[id]){
int id2 = fp(j);
//cout << id << " " << id2 << " " << dep[id] + dep[id2] - 2 * dep[lca(id, id2)] << "\n";
unon(id, id2); g[id].pb(make_pair(id2, dep[id] + dep[id2] - 2 * dep[lca(id, id2)]));
}
}
}
dfs2(to[n]);
cout << dp[to[n]];
return(0);
}
Compilation message
Main.cpp: In function 'void dfs2(int)':
Main.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for(auto [i, w]: g[s]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9764 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9764 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9752 KB |
Output is correct |
12 |
Correct |
5 ms |
9740 KB |
Output is correct |
13 |
Correct |
5 ms |
9792 KB |
Output is correct |
14 |
Correct |
5 ms |
9736 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9764 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9752 KB |
Output is correct |
12 |
Correct |
5 ms |
9740 KB |
Output is correct |
13 |
Correct |
5 ms |
9792 KB |
Output is correct |
14 |
Correct |
5 ms |
9736 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9732 KB |
Output is correct |
18 |
Correct |
8 ms |
10836 KB |
Output is correct |
19 |
Correct |
8 ms |
10836 KB |
Output is correct |
20 |
Correct |
8 ms |
10896 KB |
Output is correct |
21 |
Correct |
8 ms |
10836 KB |
Output is correct |
22 |
Correct |
9 ms |
10836 KB |
Output is correct |
23 |
Correct |
9 ms |
10708 KB |
Output is correct |
24 |
Correct |
9 ms |
10816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9764 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9752 KB |
Output is correct |
12 |
Correct |
5 ms |
9740 KB |
Output is correct |
13 |
Correct |
5 ms |
9792 KB |
Output is correct |
14 |
Correct |
5 ms |
9736 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9732 KB |
Output is correct |
18 |
Correct |
8 ms |
10836 KB |
Output is correct |
19 |
Correct |
8 ms |
10836 KB |
Output is correct |
20 |
Correct |
8 ms |
10896 KB |
Output is correct |
21 |
Correct |
8 ms |
10836 KB |
Output is correct |
22 |
Correct |
9 ms |
10836 KB |
Output is correct |
23 |
Correct |
9 ms |
10708 KB |
Output is correct |
24 |
Correct |
9 ms |
10816 KB |
Output is correct |
25 |
Correct |
6 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
10768 KB |
Output is correct |
27 |
Correct |
8 ms |
10736 KB |
Output is correct |
28 |
Correct |
11 ms |
10836 KB |
Output is correct |
29 |
Correct |
9 ms |
10740 KB |
Output is correct |
30 |
Correct |
8 ms |
10452 KB |
Output is correct |
31 |
Correct |
8 ms |
10452 KB |
Output is correct |
32 |
Correct |
9 ms |
10452 KB |
Output is correct |
33 |
Correct |
8 ms |
10452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9764 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9752 KB |
Output is correct |
12 |
Correct |
5 ms |
9740 KB |
Output is correct |
13 |
Correct |
5 ms |
9792 KB |
Output is correct |
14 |
Correct |
5 ms |
9736 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9732 KB |
Output is correct |
18 |
Correct |
8 ms |
10836 KB |
Output is correct |
19 |
Correct |
8 ms |
10836 KB |
Output is correct |
20 |
Correct |
8 ms |
10896 KB |
Output is correct |
21 |
Correct |
8 ms |
10836 KB |
Output is correct |
22 |
Correct |
9 ms |
10836 KB |
Output is correct |
23 |
Correct |
9 ms |
10708 KB |
Output is correct |
24 |
Correct |
9 ms |
10816 KB |
Output is correct |
25 |
Correct |
152 ms |
53836 KB |
Output is correct |
26 |
Correct |
134 ms |
54076 KB |
Output is correct |
27 |
Correct |
135 ms |
54548 KB |
Output is correct |
28 |
Correct |
194 ms |
51412 KB |
Output is correct |
29 |
Correct |
201 ms |
51476 KB |
Output is correct |
30 |
Correct |
202 ms |
52280 KB |
Output is correct |
31 |
Correct |
196 ms |
51788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9652 KB |
Output is correct |
2 |
Correct |
6 ms |
9704 KB |
Output is correct |
3 |
Correct |
224 ms |
42312 KB |
Output is correct |
4 |
Correct |
206 ms |
40260 KB |
Output is correct |
5 |
Correct |
216 ms |
40880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9764 KB |
Output is correct |
7 |
Correct |
5 ms |
9704 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
6 ms |
9732 KB |
Output is correct |
10 |
Correct |
6 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9752 KB |
Output is correct |
12 |
Correct |
5 ms |
9740 KB |
Output is correct |
13 |
Correct |
5 ms |
9792 KB |
Output is correct |
14 |
Correct |
5 ms |
9736 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
6 ms |
9732 KB |
Output is correct |
18 |
Correct |
8 ms |
10836 KB |
Output is correct |
19 |
Correct |
8 ms |
10836 KB |
Output is correct |
20 |
Correct |
8 ms |
10896 KB |
Output is correct |
21 |
Correct |
8 ms |
10836 KB |
Output is correct |
22 |
Correct |
9 ms |
10836 KB |
Output is correct |
23 |
Correct |
9 ms |
10708 KB |
Output is correct |
24 |
Correct |
9 ms |
10816 KB |
Output is correct |
25 |
Correct |
6 ms |
9684 KB |
Output is correct |
26 |
Correct |
8 ms |
10768 KB |
Output is correct |
27 |
Correct |
8 ms |
10736 KB |
Output is correct |
28 |
Correct |
11 ms |
10836 KB |
Output is correct |
29 |
Correct |
9 ms |
10740 KB |
Output is correct |
30 |
Correct |
8 ms |
10452 KB |
Output is correct |
31 |
Correct |
8 ms |
10452 KB |
Output is correct |
32 |
Correct |
9 ms |
10452 KB |
Output is correct |
33 |
Correct |
8 ms |
10452 KB |
Output is correct |
34 |
Correct |
152 ms |
53836 KB |
Output is correct |
35 |
Correct |
134 ms |
54076 KB |
Output is correct |
36 |
Correct |
135 ms |
54548 KB |
Output is correct |
37 |
Correct |
194 ms |
51412 KB |
Output is correct |
38 |
Correct |
201 ms |
51476 KB |
Output is correct |
39 |
Correct |
202 ms |
52280 KB |
Output is correct |
40 |
Correct |
196 ms |
51788 KB |
Output is correct |
41 |
Correct |
8 ms |
9652 KB |
Output is correct |
42 |
Correct |
6 ms |
9704 KB |
Output is correct |
43 |
Correct |
224 ms |
42312 KB |
Output is correct |
44 |
Correct |
206 ms |
40260 KB |
Output is correct |
45 |
Correct |
216 ms |
40880 KB |
Output is correct |
46 |
Correct |
294 ms |
49380 KB |
Output is correct |
47 |
Correct |
290 ms |
49008 KB |
Output is correct |
48 |
Correct |
299 ms |
49724 KB |
Output is correct |
49 |
Correct |
298 ms |
49532 KB |
Output is correct |
50 |
Correct |
330 ms |
40048 KB |
Output is correct |
51 |
Correct |
333 ms |
39372 KB |
Output is correct |
52 |
Correct |
321 ms |
39284 KB |
Output is correct |
53 |
Correct |
339 ms |
39276 KB |
Output is correct |