This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
set<int>adj[MAXN];
set<int>nums[MAXN];
int dsu[MAXN];
int n, p[MAXN];
int pos[MAXN];
bool vis[MAXN];
int nxtset;
vector<int> euler;
int l[MAXN], r[MAXN], dep[MAXN];
void dfs0(int node, int prv) {
euler.push_back(node);
dep[node] = (prv == -1 ? 0 : dep[prv] + 1);
for(int x: adj[node]) {
if(x != prv) {
dfs0(x, node);
euler.push_back(node);
}
}
}
int st[19][2*MAXN];
int lcadep(int x, int y) {
int m1 = min(l[x], l[y]);
int m2 = max(r[x], r[y]);
int k = 32 - __builtin_clz(m2-m1+1) - 1;
return min(st[k][m1], st[k][m2-(1<<k)+1]);
}
int dist(int x, int y) {
return dep[x] + dep[y] - 2 * lcadep(x, y);
}
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
void solve(int tc) {
cin >> n;
for(int i=1; i<=n; i++) cin >> p[i];
for(int i=1; i<=n; i++) pos[p[i]] = i;
vector<pair<int, int>> edges[n+1];
for(int i=1; i<n; i++) {
int a, b;
cin >> a >> b;
edges[max(p[a],p[b])].push_back({a,b});
adj[a].insert(b);
adj[b].insert(a);
}
dfs0(1, -1);
for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
for(int i=euler.size()-1; i>=0; i--) l[euler[i]] = i;
for(int i=0; i<euler.size(); i++) st[0][i] = dep[euler[i]];
for(int i=1; i<19; i++) {
for(int j=0; j+(1<<i)-1<euler.size(); j++) {
st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
}
}
for(int i=1; i<=n; i++) dsu[i]=i;
for(int i=1; i<=n; i++) nums[i].insert(p[i]);
int dp[n+1];
for(int i=1; i<=n; i++) dp[i] = 0;
for(int i=1; i<=n; i++) {
for(pair<int,int> x: edges[i]) {
int to = (p[x.first] == i ? x.second : x.first);
to = set_of(to);
int mx = *nums[to].rbegin();
dp[i] = max(dp[i], dist(pos[i], pos[mx]) + dp[mx]);
}
for(pair<int,int> x: edges[i]) {
int u = set_of(x.first), v = set_of(x.second);
if(nums[u].size() < nums[v].size()) nums[u].swap(nums[v]);
for(int x: nums[v]) nums[u].insert(x);
nums[v].clear();
union_(u,v);
nums[u].swap(nums[set_of(u)]);
}
}
cout << dp[n] << "\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
Compilation message (stderr)
Main.cpp: In function 'void solve(long long int)':
Main.cpp:73:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
| ~^~~~~~~~~~~~~
Main.cpp:75:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i=0; i<euler.size(); i++) st[0][i] = dep[euler[i]];
| ~^~~~~~~~~~~~~
Main.cpp:77:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int j=0; j+(1<<i)-1<euler.size(); j++) {
| ~~~~~~~~~~^~~~~~~~~~~~~
# | 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... |