#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 = 20;
const int MAXN = 1e6 + 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;
int 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
33628 KB |
Output is correct |
2 |
Correct |
7 ms |
33740 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
7 ms |
33732 KB |
Output is correct |
6 |
Correct |
7 ms |
33628 KB |
Output is correct |
7 |
Correct |
7 ms |
33628 KB |
Output is correct |
8 |
Correct |
7 ms |
33736 KB |
Output is correct |
9 |
Correct |
7 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
33628 KB |
Output is correct |
2 |
Correct |
7 ms |
33740 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
7 ms |
33732 KB |
Output is correct |
6 |
Correct |
7 ms |
33628 KB |
Output is correct |
7 |
Correct |
7 ms |
33628 KB |
Output is correct |
8 |
Correct |
7 ms |
33736 KB |
Output is correct |
9 |
Correct |
7 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
7 ms |
33628 KB |
Output is correct |
12 |
Correct |
7 ms |
33628 KB |
Output is correct |
13 |
Correct |
7 ms |
33676 KB |
Output is correct |
14 |
Correct |
8 ms |
33628 KB |
Output is correct |
15 |
Correct |
7 ms |
33628 KB |
Output is correct |
16 |
Correct |
7 ms |
33628 KB |
Output is correct |
17 |
Correct |
7 ms |
33628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
33628 KB |
Output is correct |
2 |
Correct |
7 ms |
33740 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
7 ms |
33732 KB |
Output is correct |
6 |
Correct |
7 ms |
33628 KB |
Output is correct |
7 |
Correct |
7 ms |
33628 KB |
Output is correct |
8 |
Correct |
7 ms |
33736 KB |
Output is correct |
9 |
Correct |
7 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
7 ms |
33628 KB |
Output is correct |
12 |
Correct |
7 ms |
33628 KB |
Output is correct |
13 |
Correct |
7 ms |
33676 KB |
Output is correct |
14 |
Correct |
8 ms |
33628 KB |
Output is correct |
15 |
Correct |
7 ms |
33628 KB |
Output is correct |
16 |
Correct |
7 ms |
33628 KB |
Output is correct |
17 |
Correct |
7 ms |
33628 KB |
Output is correct |
18 |
Correct |
9 ms |
34044 KB |
Output is correct |
19 |
Correct |
9 ms |
34140 KB |
Output is correct |
20 |
Correct |
9 ms |
34136 KB |
Output is correct |
21 |
Correct |
9 ms |
33884 KB |
Output is correct |
22 |
Correct |
9 ms |
34136 KB |
Output is correct |
23 |
Correct |
9 ms |
34140 KB |
Output is correct |
24 |
Correct |
9 ms |
34140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
33628 KB |
Output is correct |
2 |
Correct |
7 ms |
33740 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
7 ms |
33732 KB |
Output is correct |
6 |
Correct |
7 ms |
33628 KB |
Output is correct |
7 |
Correct |
7 ms |
33628 KB |
Output is correct |
8 |
Correct |
7 ms |
33736 KB |
Output is correct |
9 |
Correct |
7 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
7 ms |
33628 KB |
Output is correct |
12 |
Correct |
7 ms |
33628 KB |
Output is correct |
13 |
Correct |
7 ms |
33676 KB |
Output is correct |
14 |
Correct |
8 ms |
33628 KB |
Output is correct |
15 |
Correct |
7 ms |
33628 KB |
Output is correct |
16 |
Correct |
7 ms |
33628 KB |
Output is correct |
17 |
Correct |
7 ms |
33628 KB |
Output is correct |
18 |
Correct |
9 ms |
34044 KB |
Output is correct |
19 |
Correct |
9 ms |
34140 KB |
Output is correct |
20 |
Correct |
9 ms |
34136 KB |
Output is correct |
21 |
Correct |
9 ms |
33884 KB |
Output is correct |
22 |
Correct |
9 ms |
34136 KB |
Output is correct |
23 |
Correct |
9 ms |
34140 KB |
Output is correct |
24 |
Correct |
9 ms |
34140 KB |
Output is correct |
25 |
Correct |
7 ms |
33680 KB |
Output is correct |
26 |
Correct |
10 ms |
34156 KB |
Output is correct |
27 |
Correct |
9 ms |
34136 KB |
Output is correct |
28 |
Correct |
9 ms |
34140 KB |
Output is correct |
29 |
Correct |
9 ms |
34140 KB |
Output is correct |
30 |
Correct |
10 ms |
33884 KB |
Output is correct |
31 |
Correct |
9 ms |
33924 KB |
Output is correct |
32 |
Correct |
9 ms |
33932 KB |
Output is correct |
33 |
Correct |
9 ms |
33884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
33628 KB |
Output is correct |
2 |
Correct |
7 ms |
33740 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
7 ms |
33732 KB |
Output is correct |
6 |
Correct |
7 ms |
33628 KB |
Output is correct |
7 |
Correct |
7 ms |
33628 KB |
Output is correct |
8 |
Correct |
7 ms |
33736 KB |
Output is correct |
9 |
Correct |
7 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
7 ms |
33628 KB |
Output is correct |
12 |
Correct |
7 ms |
33628 KB |
Output is correct |
13 |
Correct |
7 ms |
33676 KB |
Output is correct |
14 |
Correct |
8 ms |
33628 KB |
Output is correct |
15 |
Correct |
7 ms |
33628 KB |
Output is correct |
16 |
Correct |
7 ms |
33628 KB |
Output is correct |
17 |
Correct |
7 ms |
33628 KB |
Output is correct |
18 |
Correct |
9 ms |
34044 KB |
Output is correct |
19 |
Correct |
9 ms |
34140 KB |
Output is correct |
20 |
Correct |
9 ms |
34136 KB |
Output is correct |
21 |
Correct |
9 ms |
33884 KB |
Output is correct |
22 |
Correct |
9 ms |
34136 KB |
Output is correct |
23 |
Correct |
9 ms |
34140 KB |
Output is correct |
24 |
Correct |
9 ms |
34140 KB |
Output is correct |
25 |
Incorrect |
93 ms |
63404 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33680 KB |
Output is correct |
3 |
Correct |
129 ms |
58804 KB |
Output is correct |
4 |
Correct |
124 ms |
58704 KB |
Output is correct |
5 |
Correct |
124 ms |
58592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
33628 KB |
Output is correct |
2 |
Correct |
7 ms |
33740 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
7 ms |
33732 KB |
Output is correct |
6 |
Correct |
7 ms |
33628 KB |
Output is correct |
7 |
Correct |
7 ms |
33628 KB |
Output is correct |
8 |
Correct |
7 ms |
33736 KB |
Output is correct |
9 |
Correct |
7 ms |
33628 KB |
Output is correct |
10 |
Correct |
7 ms |
33628 KB |
Output is correct |
11 |
Correct |
7 ms |
33628 KB |
Output is correct |
12 |
Correct |
7 ms |
33628 KB |
Output is correct |
13 |
Correct |
7 ms |
33676 KB |
Output is correct |
14 |
Correct |
8 ms |
33628 KB |
Output is correct |
15 |
Correct |
7 ms |
33628 KB |
Output is correct |
16 |
Correct |
7 ms |
33628 KB |
Output is correct |
17 |
Correct |
7 ms |
33628 KB |
Output is correct |
18 |
Correct |
9 ms |
34044 KB |
Output is correct |
19 |
Correct |
9 ms |
34140 KB |
Output is correct |
20 |
Correct |
9 ms |
34136 KB |
Output is correct |
21 |
Correct |
9 ms |
33884 KB |
Output is correct |
22 |
Correct |
9 ms |
34136 KB |
Output is correct |
23 |
Correct |
9 ms |
34140 KB |
Output is correct |
24 |
Correct |
9 ms |
34140 KB |
Output is correct |
25 |
Correct |
7 ms |
33680 KB |
Output is correct |
26 |
Correct |
10 ms |
34156 KB |
Output is correct |
27 |
Correct |
9 ms |
34136 KB |
Output is correct |
28 |
Correct |
9 ms |
34140 KB |
Output is correct |
29 |
Correct |
9 ms |
34140 KB |
Output is correct |
30 |
Correct |
10 ms |
33884 KB |
Output is correct |
31 |
Correct |
9 ms |
33924 KB |
Output is correct |
32 |
Correct |
9 ms |
33932 KB |
Output is correct |
33 |
Correct |
9 ms |
33884 KB |
Output is correct |
34 |
Incorrect |
93 ms |
63404 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |