This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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];
ll 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |