//#include "pour.h"
#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rd);
}
const int N = 2e5 + 7;
const int Mod = 111539786;
const int szBL = 916;
const ll INF = 1e18;
const int BASE = 137;
int n, m;
vector<int> ke[N];
int a[N];
int dp[N];
int high[N], par[N][25];
void dfs (int u, int p) {
high[u] = high[p] + 1;
par[u][0] = p;
rep (i, 1, 20) par[u][i] = par[par[u][i - 1]][i - 1];
iter (&v, ke[u]) if (v != p) {
dfs (v, u);
}
}
int Lca (int u, int v) {
if (high[v] < high[u]) swap (u, v);
int delta = high[v] - high[u];
rep (i, 0, 20) if (bit(delta, i)) {
v = par[v][i];
}
if (v == u) return v;
reb (i, 20, 0) {
if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
}
return par[v][0];
}
int Dist (int u, int v) {
return high[u] + high[v] - 2 * high[Lca(u, v)];
}
struct Disjoin_set {
int lab[N], sz[N], mx[N];
void init (int n) {
rep (i, 1, n) lab[i] = i, sz[i] = 1, mx[i] = i;
}
int Find (int u) {
return u == lab[u] ? u : lab[u] = Find(lab[u]);
}
void Join (int u, int v) {
u = Find(u);
v = Find(v);
if (u == v) return;
if (sz[u] < sz[v]) swap (u, v);
sz[u] += sz[v];
lab[v] = u;
mx[u] = max(mx[u], mx[v]);
}
}DSU;
void solution () {
cin >> n;
rep (i, 1, n) {
cin >> a[i];
}
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
ke[a[u]].push_back(a[v]);
ke[a[v]].push_back(a[u]);
}
dfs (1, 0);
DSU.init(n);
dp[1] = 0;
rep (u, 1, n) {
iter (&v, ke[u]) {
if (u > v) {
int mx = DSU.mx[DSU.Find(v)];
dp[u] = max(dp[u], dp[mx] + Dist(u, mx));
DSU.Join(u, v);
}
}
// cout << dp[u] <<"\n";
}
cout << dp[n] <<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
int main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// file ("c");
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
3 2
0 1 1
0 0 1
0 0 0
3 3
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
11 |
Correct |
4 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
11 |
Correct |
4 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6180 KB |
Output is correct |
21 |
Correct |
4 ms |
5976 KB |
Output is correct |
22 |
Correct |
5 ms |
6236 KB |
Output is correct |
23 |
Correct |
6 ms |
6124 KB |
Output is correct |
24 |
Correct |
5 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
11 |
Correct |
4 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6180 KB |
Output is correct |
21 |
Correct |
4 ms |
5976 KB |
Output is correct |
22 |
Correct |
5 ms |
6236 KB |
Output is correct |
23 |
Correct |
6 ms |
6124 KB |
Output is correct |
24 |
Correct |
5 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
4956 KB |
Output is correct |
26 |
Correct |
5 ms |
5980 KB |
Output is correct |
27 |
Correct |
5 ms |
6204 KB |
Output is correct |
28 |
Correct |
4 ms |
6168 KB |
Output is correct |
29 |
Correct |
4 ms |
5992 KB |
Output is correct |
30 |
Correct |
5 ms |
5980 KB |
Output is correct |
31 |
Correct |
4 ms |
5948 KB |
Output is correct |
32 |
Correct |
4 ms |
5976 KB |
Output is correct |
33 |
Correct |
4 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
11 |
Correct |
4 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6180 KB |
Output is correct |
21 |
Correct |
4 ms |
5976 KB |
Output is correct |
22 |
Correct |
5 ms |
6236 KB |
Output is correct |
23 |
Correct |
6 ms |
6124 KB |
Output is correct |
24 |
Correct |
5 ms |
6236 KB |
Output is correct |
25 |
Incorrect |
85 ms |
44052 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5168 KB |
Output is correct |
3 |
Correct |
153 ms |
39188 KB |
Output is correct |
4 |
Correct |
139 ms |
39248 KB |
Output is correct |
5 |
Correct |
132 ms |
39304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
2 ms |
4952 KB |
Output is correct |
10 |
Correct |
2 ms |
5168 KB |
Output is correct |
11 |
Correct |
4 ms |
5212 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
5164 KB |
Output is correct |
16 |
Correct |
3 ms |
5208 KB |
Output is correct |
17 |
Correct |
3 ms |
5212 KB |
Output is correct |
18 |
Correct |
5 ms |
5976 KB |
Output is correct |
19 |
Correct |
4 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6180 KB |
Output is correct |
21 |
Correct |
4 ms |
5976 KB |
Output is correct |
22 |
Correct |
5 ms |
6236 KB |
Output is correct |
23 |
Correct |
6 ms |
6124 KB |
Output is correct |
24 |
Correct |
5 ms |
6236 KB |
Output is correct |
25 |
Correct |
3 ms |
4956 KB |
Output is correct |
26 |
Correct |
5 ms |
5980 KB |
Output is correct |
27 |
Correct |
5 ms |
6204 KB |
Output is correct |
28 |
Correct |
4 ms |
6168 KB |
Output is correct |
29 |
Correct |
4 ms |
5992 KB |
Output is correct |
30 |
Correct |
5 ms |
5980 KB |
Output is correct |
31 |
Correct |
4 ms |
5948 KB |
Output is correct |
32 |
Correct |
4 ms |
5976 KB |
Output is correct |
33 |
Correct |
4 ms |
5980 KB |
Output is correct |
34 |
Incorrect |
85 ms |
44052 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |