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("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include <time.h>
#endif
#define all(a) (a).begin(), (a).end()
#define rev(a) (a).rbegin(), (a).rend()
#define F first
#define S second
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) \
{ \
++recur_depth; \
auto x_ = x; \
--recur_depth; \
cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \
<< __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \
}
#else
#define dbg(x)
#endif
using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> ii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> llll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;
typedef vector<vii> vvii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
template <class type1>
using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag,
tree_order_statistics_node_update>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
!is_same<T_container, string>::value,
typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e9;
const ld EPS = 1e-9;
template<typename T>
struct ST {
const int MAXLG = 22;
int n;
vector<vector<T>> st;
vi lg2;
ST(int n, vector<T>& a) : n(n) {
st = vector<vector<T>>(MAXLG, vector<T>(n));
for (int i=0; i<n; i++) st[0][i] = a[i];
for (int j=1; j<MAXLG; j++) for (int i=0; i + (1 << j) <= n; i++)
st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
lg2 = vi(n + 10);
for (int i=2; i<n + 10; i++) lg2[i] = lg2[i / 2] + 1;
}
T query(int l, int r) {
if (l > r) swap(l, r);
int k = lg2[r - l + 1];
return min(st[k][l], st[k][r - (1 << k) + 1]);
}
};
struct DSU {
int n;
vi par, sz, mx;
DSU(int n) : n(n), par(vi(n, -1)), sz(vi(n, 1)) {
mx = vi(n);
for (int i=0; i<n; i++) mx[i] = i;
}
int find(int x) {
if (par[x] == -1) return x;
return par[x] = find(par[x]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (sz[v] > sz[u]) swap(u, v);
par[v] = u;
sz[u] += sz[v];
mx[u] = max(mx[u], mx[v]);
}
};
void solve() {
int n; cin >> n;
vi p(n); for (int &x : p) cin >> x, x--;
vvi adj(n);
vii ed(n - 1);
for (int i=0; i<n - 1; i++) {
cin >> ed[i].first >> ed[i].second;
ed[i].first--, ed[i].second--;
adj[ed[i].first].push_back(ed[i].second);
adj[ed[i].second].push_back(ed[i].first);
}
vi inv_p(n);
for (int i=0; i<n; i++) inv_p[p[i]] = i;
sort(all(ed), [&](ii a, ii b) {
return max(p[a.first], p[a.second]) < max(p[b.first], p[b.second]);
});
int idx_n = find(all(p), n - 1) - p.begin();
vi depth(n), idx(n);
vii euler;
function<void(int, int)> f = [&](int u, int p) {
idx[u] = euler.size();
euler.push_back({depth[u], u});
for (int &v : adj[u]) if (v != p) {
depth[v] = depth[u] + 1;
f(v, u);
euler.push_back({depth[u], u});
}
}; f(idx_n, -1);
ST st(euler.size(), euler);
DSU dsu(n);
auto dist = [&](int u, int v) {
int lca = st.query(idx[u], idx[v]).second;
return depth[u] + depth[v] - 2 * depth[lca];
};
vvi nadj(n);
for (auto &[ui, vi] : ed) {
int u = p[ui], v = p[vi];
int u_max = dsu.mx[dsu.find(u)], v_max = dsu.mx[dsu.find(v)];
nadj[u_max].push_back(v_max);
nadj[v_max].push_back(u_max);
dsu.merge(u, v);
}
int ans = 0;
function<void(int, int, int)> g = [&](int u, int p, int tot) {
if (p != -1)
tot += dist(inv_p[u], inv_p[p]);
ans = max(ans, tot);
for (int &v : nadj[u]) if (v != p)
g(v, u, tot);
}; g(n - 1, -1, 0);
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
clock_t start = clock();
int test_cases = 1;
// cin >> test_cases;
while (test_cases--)
solve();
#ifdef DEBUG
cerr << fixed << setprecision(10)
<< "\nTime Taken: " << (double)(clock() - start) / CLOCKS_PER_SEC
<< "s\n";
#endif
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:213:11: warning: unused variable 'start' [-Wunused-variable]
213 | clock_t start = clock();
| ^~~~~
# | 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... |