#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)size(x)
#define pb push_back
#define ff first
#define ss second
typedef long long ll;
typedef pair<int, int> pp;
void dbg(){
cerr << endl;
}
template<typename H, typename... T> void dbg(H h, T... t){
cerr << h << ", ";
dbg(t...);
}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
#ifndef ONLINE_JUDGE
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">: ", dbg(__VA_ARGS__)
#else
#define er(...) 0
#endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 1e5 + 5, maxk = 31, lg = 18, inf = ll(1e18) + 5;
int par[maxn][lg], h[maxn];
vector<int> adj[maxn];
void dfs(int r, int p){
par[r][0] = p;
rep(j,1,lg){
par[r][j] = par[par[r][j-1]][j-1];
}
for(int c: adj[r]) if(c - p){
h[c] = h[r] + 1;
dfs(c, r);
}
}
int lca(int u, int v){
if(h[u] < h[v]) swap(u, v);
per(i,lg-1,0) if(h[par[u][i]] >= h[v]) u = par[u][i];
if(u == v) return u;
per(i,lg-1,0) if(par[u][i] - par[v][i]) u = par[u][i], v = par[v][i];
return par[u][0];
}
int dist(int u, int v){
return h[u] + h[v] - 2*h[lca(u, v)];
}
int pr[maxn];
int get(int x){
return pr[x] == -1? x: pr[x] = get(pr[x]);
}
ll dp[maxn];
int main(){ IOS();
int n; cin >> n;
vector<int> h(n);
rep(i,0,n){
cin >> h[i];
pr[i] = -1;
}
rep(i,1,n){
int u, v; cin >> u >> v; u--, v--;
adj[u].pb(v), adj[v].pb(u);
}
dfs(0, 0);
vector<int> srt(n); iota(all(srt), 0);
sort(all(srt), [&](int u, int v){
return h[u] < h[v];
});
for(int r: srt){
for(int c: adj[r]) if(h[c] < h[r]){ c = get(c);
dp[r] = max(dp[r], dp[c] + dist(c, r));
pr[c] = r;
}
}
cout << dp[srt.back()] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
5 ms |
3412 KB |
Output is correct |
19 |
Correct |
5 ms |
3496 KB |
Output is correct |
20 |
Correct |
6 ms |
3516 KB |
Output is correct |
21 |
Correct |
6 ms |
3508 KB |
Output is correct |
22 |
Correct |
6 ms |
3540 KB |
Output is correct |
23 |
Correct |
6 ms |
3500 KB |
Output is correct |
24 |
Correct |
5 ms |
3500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
5 ms |
3412 KB |
Output is correct |
19 |
Correct |
5 ms |
3496 KB |
Output is correct |
20 |
Correct |
6 ms |
3516 KB |
Output is correct |
21 |
Correct |
6 ms |
3508 KB |
Output is correct |
22 |
Correct |
6 ms |
3540 KB |
Output is correct |
23 |
Correct |
6 ms |
3500 KB |
Output is correct |
24 |
Correct |
5 ms |
3500 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
5 ms |
3412 KB |
Output is correct |
27 |
Correct |
5 ms |
3492 KB |
Output is correct |
28 |
Correct |
5 ms |
3380 KB |
Output is correct |
29 |
Correct |
5 ms |
3412 KB |
Output is correct |
30 |
Correct |
5 ms |
3300 KB |
Output is correct |
31 |
Correct |
5 ms |
3284 KB |
Output is correct |
32 |
Correct |
6 ms |
3284 KB |
Output is correct |
33 |
Correct |
6 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
5 ms |
3412 KB |
Output is correct |
19 |
Correct |
5 ms |
3496 KB |
Output is correct |
20 |
Correct |
6 ms |
3516 KB |
Output is correct |
21 |
Correct |
6 ms |
3508 KB |
Output is correct |
22 |
Correct |
6 ms |
3540 KB |
Output is correct |
23 |
Correct |
6 ms |
3500 KB |
Output is correct |
24 |
Correct |
5 ms |
3500 KB |
Output is correct |
25 |
Runtime error |
23 ms |
8060 KB |
Execution killed with signal 11 |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Runtime error |
23 ms |
8504 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
1 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
5 ms |
3412 KB |
Output is correct |
19 |
Correct |
5 ms |
3496 KB |
Output is correct |
20 |
Correct |
6 ms |
3516 KB |
Output is correct |
21 |
Correct |
6 ms |
3508 KB |
Output is correct |
22 |
Correct |
6 ms |
3540 KB |
Output is correct |
23 |
Correct |
6 ms |
3500 KB |
Output is correct |
24 |
Correct |
5 ms |
3500 KB |
Output is correct |
25 |
Correct |
1 ms |
2644 KB |
Output is correct |
26 |
Correct |
5 ms |
3412 KB |
Output is correct |
27 |
Correct |
5 ms |
3492 KB |
Output is correct |
28 |
Correct |
5 ms |
3380 KB |
Output is correct |
29 |
Correct |
5 ms |
3412 KB |
Output is correct |
30 |
Correct |
5 ms |
3300 KB |
Output is correct |
31 |
Correct |
5 ms |
3284 KB |
Output is correct |
32 |
Correct |
6 ms |
3284 KB |
Output is correct |
33 |
Correct |
6 ms |
3284 KB |
Output is correct |
34 |
Runtime error |
23 ms |
8060 KB |
Execution killed with signal 11 |
35 |
Halted |
0 ms |
0 KB |
- |