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 "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 recur(int node) {
if(adj[node].empty()) return 0;
int cat = dsu[node];
int boss = *nums[cat].rbegin();
nums[cat].erase(boss);
node = pos[boss];
vector<int> visited; // Remember to turn all of them to not visited at the end
vis[node] = 1;
visited.push_back(node);
vector<int> start;
start.push_back(0);
for(int x: adj[node]) start.push_back(x);
int len = start.size() - 1;
int nxt[len+1], prv[len+1];
queue<int> q[len+1];
for(int i=1; i<=len; i++) {
nxt[i] = i+1;
prv[i] = i-1;
q[i].push(start[i]);
}
nxt[0] = 1;
vector<int> bruh[len+1]; // respective lists
vector<int>ranks; // rank according ascending of size
while(1) {
int id=0;
while(nxt[id]!=len+1){
if(ranks.size() + 1 == len) break;
id=nxt[id];
int f = q[id].front();
q[id].pop();
vis[f]=1;
bruh[id].push_back(f);
visited.push_back(f);
for(int x:adj[f]) {
if(!vis[x]) {
q[id].push(x);
}
}
if(q[id].empty()) {
for(int x:bruh[id]){
dsu[x]=nxtset;
nums[cat].erase(p[x]);
nums[nxtset].insert(p[x]);
}
ranks.push_back(id);
nxtset++;
nxt[prv[id]]=nxt[id];
prv[nxt[id]]=prv[id];
}
}
if(ranks.size() + 1 == len) break;
}
for(int x:visited)vis[x]=0;
vector<pair<int,int>>vt;
for(int i=1; i<=len; i++) {
vt.push_back({*nums[dsu[start[i]]].rbegin(),pos[*nums[dsu[start[i]]].rbegin()]});
}
for(int x: adj[node]) {
adj[x].erase(node);
}
int res = 0;
for(pair<int, int> x: vt) {
res = max(res, dist(node, x.second) + recur(x.second));
}
return res;
}
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;
for(int i=1; i<n; i++) {
int a, b;
cin >> 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]=1;
for(int i=1; i<=n; i++) nums[1].insert(i);
nxtset=2;
cout<<recur(1)<<'\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 'long long int recur(long long int)':
Main.cpp:78:27: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
78 | if(ranks.size() + 1 == len) break;
| ~~~~~~~~~~~~~~~~~^~~~~~
Main.cpp:102:25: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
102 | if(ranks.size() + 1 == len) break;
| ~~~~~~~~~~~~~~~~~^~~~~~
Main.cpp: In function 'void solve(long long int)':
Main.cpp:129: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]
129 | for(int i=0; i<euler.size(); i++) r[euler[i]] = i;
| ~^~~~~~~~~~~~~
Main.cpp:131: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]
131 | for(int i=0; i<euler.size(); i++) st[0][i] = dep[euler[i]];
| ~^~~~~~~~~~~~~
Main.cpp:133: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]
133 | 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... |