This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/***Farhan132***/
// #pragma GCC optimize("Ofast,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
typedef pair<ll , ll> ii;
typedef tuple < ll, ll, ll > tp;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clz(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
const ll mod = (ll) 998244353;
// const ll mod = (ll) 1e9 + 7;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)2e18;
// ll dx[]={0,1,0,-1};
// ll dy[]={1,0,-1,0};
// ll dxx[]={0,1,0,-1,1,1,-1,-1};
// ll dyy[]={1,0,-1,0,1,-1,1,-1};
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 2e5 + 5;
ll dp[N];
vector < ll > v[N];
const ll LG = 19;
ll par[LG][N], depth[N];
void dfs_LCA(ll s, ll p, ll d){
depth[s] = d;
par[0][s] = p;
for(ll i = 1; i < LG; i++){
par[i][s] = par[i - 1][par[i - 1][s]];
}
for(auto u : v[s]){
if(u - p){
dfs_LCA(u , s, d + 1);
}
}
return;
}
ll lca(ll a, ll b){
if(depth[a] > depth[b]) swap(a, b);
for(ll i = LG - 1; i >= 0; i--){
if(depth[b] - (1 << i) >= depth[a]) b = par[i][b];
}
if(a == b) return a;
for(ll i = LG - 1; i >= 0; i--){
if(par[i][a] != par[i][b]) a = par[i][a], b = par[i][b];
}
return par[0][a];
}
ll dist(ll a, ll b){
return depth[a] + depth[b] - 2*depth[lca(a, b)];
}
ll lift(ll a, ll x){
for(ll i = LG - 1; i >= 0; i--){
if((x >> i)&1) a = par[i][a];
}
return a;
}
ll kth(ll a, ll b, ll k){
ll d = dist(a, b);
assert(k <= d);
ll L = lca(a, b);
if(depth[a] - depth[L] >= k){
return lift(a, k);
}
return lift(b, d - k);
}
struct DSU{
vector < int > par;
vector < int > mx;
void start(int n){
par.resize(n + 1);
mx.resize(n + 1);
for(int i = 0; i <= n; i++) par[i] = i, mx[i] = i;
}
int find(int a){
if(a == par[a]) return a;
return par[a] = find(par[a]);
}
void merge(int x, int y){
x = find(x); y = find(y);
if(x == y) return;
par[x] = y;
mx[y] = max(mx[y], mx[x]);
return;
}
}T;
ll p[N], c[N];
void solve(){
ll n; cin >> n;
for(ll i = 1; i <= n; i++){
cin >> p[i];
}
for(ll i = 1; i < n; i++){
ll x, y; cin >> x >> y;
x = p[x]; y = p[y];
v[x].pb(y);
v[y].pb(x);
}
dfs_LCA(1, 0, 1);
T.start(n + 1);
mem(c, 0);
for(ll i = 1; i <= n; i++){
c[i] = 1;
dp[i] = 0;
for(auto u : v[i]){
if(c[u]){
ll n1 = T.mx[T.find(u)];
dp[i] = max(dp[i], dp[n1] + dist(n1, i));
T.merge(i, u);
}
}
}
cout << dp[n] << '\n';
return;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
auto start_time = clock();
#else
// freopen("subsequence.in", "r", stdin);
// freopen("subsequence.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//precalc();
ll T = 1, CT = 0;// cin >> T;
while(T--){
// cout << "Case #" << ++CT << ": ";
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
#endif
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:170:15: warning: unused variable 'CT' [-Wunused-variable]
170 | ll T = 1, CT = 0;// cin >> T;
| ^~
# | 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... |