#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<pll> vpll;
#define PB push_back
#define PF push_front
#define PPB pop_back
#define PPF pop_front
#define X first
#define Y second
#define MP make_pair
#define all(x) (x).begin(), (x).end()
const int mod = 1e9 + 7; //998244353;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int logo = 18;
const int MAXN = 2e5 + 7;
const int off = 1 << logo;
const int trsz = off << 1;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
struct uf{
int par[MAXN];
uf(){
for(int i=1; i<MAXN; i++) par[i] = i;
}
int find(int x){
return x == par[x] ? x : par[x] = find(par[x]);
}
}t;
ll h[MAXN], dp[MAXN], dep[MAXN], par[MAXN][logo];
vi g[MAXN];
int getLCA(int a, int b){
if(dep[a] < dep[b]) swap(a, b);
int delta = dep[a] - dep[b];
for(int i=0; i<logo; i++) if(delta & (1 << i)) a = par[a][i];
if(a == b) return a;
for(int i=logo-1; i>=0; i--) if(par[a][i] != par[b][i]) a = par[a][i], b = par[b][i];
return par[a][0];
}
void dfs(int u, int p = 1){
dep[u] = dep[p] + 1;
par[u][0] = p;
for(auto &x : g[u]) if(x != p) dfs(x, u);
}
int dis(int a, int b){
return dep[a] + dep[b] - 2 * dep[getLCA(a, b)];
}
void solve(){
int n;
cin >> n;
for(int i=1; i<=n; i++) cin >> h[i];
for(int i=1, a, b; i<n; i++){
cin >> a >> b;
g[h[a]].PB(h[b]);
g[h[b]].PB(h[a]);
}
dfs(1);
for(int j=1; j<logo; j++) for(int i=1; i<=n; i++) par[i][j] = par[par[i][j - 1]][j - 1];
for(int i=1; i<=n; i++){
for(auto &x : g[i]){
if(x > i) continue;
x = t.find(x);
dp[i] = max(dp[i], dp[x] + dis(x, i));
t.par[x] = i;
}
}
cout << dp[n] << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt = 1;
//cin >> tt;
while(tt--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11612 KB |
Output is correct |
2 |
Correct |
2 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
3 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11608 KB |
Output is correct |
9 |
Correct |
2 ms |
11624 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11612 KB |
Output is correct |
2 |
Correct |
2 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
3 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11608 KB |
Output is correct |
9 |
Correct |
2 ms |
11624 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11612 KB |
Output is correct |
12 |
Correct |
2 ms |
11640 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11608 KB |
Output is correct |
15 |
Correct |
2 ms |
11864 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11612 KB |
Output is correct |
2 |
Correct |
2 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
3 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11608 KB |
Output is correct |
9 |
Correct |
2 ms |
11624 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11612 KB |
Output is correct |
12 |
Correct |
2 ms |
11640 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11608 KB |
Output is correct |
15 |
Correct |
2 ms |
11864 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11864 KB |
Output is correct |
18 |
Correct |
4 ms |
11864 KB |
Output is correct |
19 |
Correct |
4 ms |
11868 KB |
Output is correct |
20 |
Correct |
4 ms |
11868 KB |
Output is correct |
21 |
Correct |
4 ms |
11868 KB |
Output is correct |
22 |
Correct |
4 ms |
11868 KB |
Output is correct |
23 |
Correct |
4 ms |
11868 KB |
Output is correct |
24 |
Correct |
4 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11612 KB |
Output is correct |
2 |
Correct |
2 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
3 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11608 KB |
Output is correct |
9 |
Correct |
2 ms |
11624 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11612 KB |
Output is correct |
12 |
Correct |
2 ms |
11640 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11608 KB |
Output is correct |
15 |
Correct |
2 ms |
11864 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11864 KB |
Output is correct |
18 |
Correct |
4 ms |
11864 KB |
Output is correct |
19 |
Correct |
4 ms |
11868 KB |
Output is correct |
20 |
Correct |
4 ms |
11868 KB |
Output is correct |
21 |
Correct |
4 ms |
11868 KB |
Output is correct |
22 |
Correct |
4 ms |
11868 KB |
Output is correct |
23 |
Correct |
4 ms |
11868 KB |
Output is correct |
24 |
Correct |
4 ms |
11868 KB |
Output is correct |
25 |
Correct |
2 ms |
11612 KB |
Output is correct |
26 |
Correct |
4 ms |
11936 KB |
Output is correct |
27 |
Correct |
5 ms |
11868 KB |
Output is correct |
28 |
Correct |
4 ms |
11868 KB |
Output is correct |
29 |
Correct |
4 ms |
11868 KB |
Output is correct |
30 |
Correct |
4 ms |
11868 KB |
Output is correct |
31 |
Correct |
4 ms |
11868 KB |
Output is correct |
32 |
Correct |
4 ms |
11864 KB |
Output is correct |
33 |
Correct |
5 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11612 KB |
Output is correct |
2 |
Correct |
2 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
3 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11608 KB |
Output is correct |
9 |
Correct |
2 ms |
11624 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11612 KB |
Output is correct |
12 |
Correct |
2 ms |
11640 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11608 KB |
Output is correct |
15 |
Correct |
2 ms |
11864 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11864 KB |
Output is correct |
18 |
Correct |
4 ms |
11864 KB |
Output is correct |
19 |
Correct |
4 ms |
11868 KB |
Output is correct |
20 |
Correct |
4 ms |
11868 KB |
Output is correct |
21 |
Correct |
4 ms |
11868 KB |
Output is correct |
22 |
Correct |
4 ms |
11868 KB |
Output is correct |
23 |
Correct |
4 ms |
11868 KB |
Output is correct |
24 |
Correct |
4 ms |
11868 KB |
Output is correct |
25 |
Correct |
102 ms |
49560 KB |
Output is correct |
26 |
Correct |
105 ms |
58196 KB |
Output is correct |
27 |
Correct |
102 ms |
58188 KB |
Output is correct |
28 |
Correct |
162 ms |
57704 KB |
Output is correct |
29 |
Correct |
156 ms |
53588 KB |
Output is correct |
30 |
Correct |
150 ms |
56404 KB |
Output is correct |
31 |
Correct |
142 ms |
55708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11868 KB |
Output is correct |
3 |
Correct |
137 ms |
44880 KB |
Output is correct |
4 |
Correct |
135 ms |
44880 KB |
Output is correct |
5 |
Correct |
139 ms |
44880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11612 KB |
Output is correct |
2 |
Correct |
2 ms |
11612 KB |
Output is correct |
3 |
Correct |
2 ms |
11612 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
3 ms |
11612 KB |
Output is correct |
6 |
Correct |
3 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11608 KB |
Output is correct |
9 |
Correct |
2 ms |
11624 KB |
Output is correct |
10 |
Correct |
3 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11612 KB |
Output is correct |
12 |
Correct |
2 ms |
11640 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11608 KB |
Output is correct |
15 |
Correct |
2 ms |
11864 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11864 KB |
Output is correct |
18 |
Correct |
4 ms |
11864 KB |
Output is correct |
19 |
Correct |
4 ms |
11868 KB |
Output is correct |
20 |
Correct |
4 ms |
11868 KB |
Output is correct |
21 |
Correct |
4 ms |
11868 KB |
Output is correct |
22 |
Correct |
4 ms |
11868 KB |
Output is correct |
23 |
Correct |
4 ms |
11868 KB |
Output is correct |
24 |
Correct |
4 ms |
11868 KB |
Output is correct |
25 |
Correct |
2 ms |
11612 KB |
Output is correct |
26 |
Correct |
4 ms |
11936 KB |
Output is correct |
27 |
Correct |
5 ms |
11868 KB |
Output is correct |
28 |
Correct |
4 ms |
11868 KB |
Output is correct |
29 |
Correct |
4 ms |
11868 KB |
Output is correct |
30 |
Correct |
4 ms |
11868 KB |
Output is correct |
31 |
Correct |
4 ms |
11868 KB |
Output is correct |
32 |
Correct |
4 ms |
11864 KB |
Output is correct |
33 |
Correct |
5 ms |
11868 KB |
Output is correct |
34 |
Correct |
102 ms |
49560 KB |
Output is correct |
35 |
Correct |
105 ms |
58196 KB |
Output is correct |
36 |
Correct |
102 ms |
58188 KB |
Output is correct |
37 |
Correct |
162 ms |
57704 KB |
Output is correct |
38 |
Correct |
156 ms |
53588 KB |
Output is correct |
39 |
Correct |
150 ms |
56404 KB |
Output is correct |
40 |
Correct |
142 ms |
55708 KB |
Output is correct |
41 |
Correct |
2 ms |
11608 KB |
Output is correct |
42 |
Correct |
2 ms |
11868 KB |
Output is correct |
43 |
Correct |
137 ms |
44880 KB |
Output is correct |
44 |
Correct |
135 ms |
44880 KB |
Output is correct |
45 |
Correct |
139 ms |
44880 KB |
Output is correct |
46 |
Correct |
133 ms |
55376 KB |
Output is correct |
47 |
Correct |
125 ms |
55304 KB |
Output is correct |
48 |
Correct |
125 ms |
55244 KB |
Output is correct |
49 |
Correct |
122 ms |
55380 KB |
Output is correct |
50 |
Correct |
168 ms |
48976 KB |
Output is correct |
51 |
Correct |
157 ms |
48720 KB |
Output is correct |
52 |
Correct |
155 ms |
48772 KB |
Output is correct |
53 |
Correct |
174 ms |
48980 KB |
Output is correct |