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>
#include<fstream>
using namespace std;
ifstream fin("WINTER.inp");
ofstream fout("WINTER.out");
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
const int x[4] = {1, -1, 0, 0};
const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 2e5, mxm = 1e5, sq = 400;
const int base = (1 << 18);
const ll inf = 1e9;
const ld pre = 1e-6;
int n;
int a[mxn + 1], dep[mxn + 1], up[mxn + 1][18], p[mxn + 1], to[mxn + 1];
ll dp[mxn + 1];
vt<int>adj[mxn + 1];
vt<pii>g[mxn + 1];
int lca(int u, int v){
if(dep[u] < dep[v])swap(u, v);
int k = dep[u] - dep[v];
for(int i = 0; i < 18; i++){
if(k & (1 << i)){
u = up[u][i];
}
}
if(u == v)return(u);
for(int i = 17; i >= 0; i--){
if(up[u][i] != up[v][i]){
u = up[u][i]; v = up[v][i];
}
}
return(up[u][0]);
}
void dfs2(int s){
for(auto [i, w]: g[s]){
dfs2(i);
dp[s] = max(dp[s], dp[i] + w);
}
}
void dfs(int s, int pre){
for(auto i: adj[s]){
if(i != pre){
dep[i] = dep[s] + 1; up[i][0] = s;
dfs(i, s);
}
}
}
int fp(int u){
if(p[u] == u)return(u);
return(p[u] = fp(p[u]));
}
void unon(int u, int v){
u = fp(u); v = fp(v);
if(u == v)return;
if(a[u] < a[v])swap(u, v);
p[v] = u;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
forr(i, 1, n + 1){
cin >> a[i]; to[a[i]] = i;
}
forr(i, 1, n){
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
dfs(1, -1);
forr(j, 1, 18){
forr(i, 1, n + 1){
up[i][j] = up[up[i][j - 1]][j - 1];
}
}
forr(i, 1, n + 1)p[i] = i;
for(int i = 1; i <= n; i++){
int id = to[i];
for(auto j: adj[id]){
if(a[j] < a[id]){
int id2 = fp(j);
//cout << id << " " << id2 << " " << dep[id] + dep[id2] - 2 * dep[lca(id, id2)] << "\n";
unon(id, id2); g[id].pb(make_pair(id2, dep[id] + dep[id2] - 2 * dep[lca(id, id2)]));
}
}
}
dfs2(to[n]);
cout << dp[to[n]];
return(0);
}
Compilation message (stderr)
Main.cpp: In function 'void dfs2(int)':
Main.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for(auto [i, w]: g[s]){
| ^
# | 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... |