Submission #1246796

#TimeUsernameProblemLanguageResultExecution timeMemory
1246796M_SH_OCat Exercise (JOI23_ho_t4)C++20
100 / 100
235 ms77548 KiB
/*#pragma GCC optimize("O3") #pragma GCC optimization("Ofast,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")*/ #include <bits/stdc++.h> /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp>*/ #define ll long long #define ll1 long long #define ull unsigned long long #define dou long double #define str string #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define vpll vector<pll> #define vbool vector<bool> #define vstr vector<str> #define vvll vector<vll> #define pb push_back #define pf push_front #define endl "\n" #define fr first #define se second // #define sortcmp(a) sort(a.begin(), a.end(), cmp) #define sort(a) sort(a.begin(), a.end()) #define reverse(a) reverse(a.begin(), a.end()) #define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define INF 1000000000000000007 #define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> using namespace std; //using namespace __gnu_pbds; mt19937 rng(time(0)); ll randll(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); } vvll g; vll p, a; ll find(ll v){ if(p[v] == v) return v; return p[v] = find(p[v]); } void unite(ll a1, ll b){ a1 = find(a1); b = find(b); if(a1 == b) return; if(a[a1] > a[b]) p[b] = a1; else p[a1] = b; } vll tin, tout, pref; vll c; vvll pr; ll t = 0; void dfs(ll v, ll d = 0, ll p = -1){ c.pb(v); for(int i = 0; (1 << i) < c.size(); i ++){ pr[v][i] = c[c.size()-(1 << i)-1]; } tin[v] = t++; pref[v] = d; for(int i : g[v]){ if(i == p) continue; dfs(i, d+1, v); } tout[v] = t ++; c.pop_back(); } bool f(ll a, ll b){ if(tin[a] <= tin[b] && tout[a] >= tout[b]) return 1; return 0; } ll lca(ll a, ll b){ if(f(a, b)) return a; if(f(b, a)) return b; ll x = a; for(int i = 19; i >= 0; i --){ if(pr[x][i] == -1 || f(pr[x][i], b)) continue; x = pr[x][i]; //cout << x << endl; } return pr[x][0]; } int main(){ speed; srand(time(0)); ll n; cin >> n; a.resize(n+7); g.resize(n+7); p.resize(n+7); tin.resize(n+7); tout.resize(n+7); pr.resize(n+7, vll(20, -1)); pref.resize(n+7, 0); vpll v; for(int i = 1; i <= n; i ++){ cin >> a[i]; p[i] = i; v.pb({a[i], i}); } for(int i = 0; i < n-1; i ++){ ll a, b; cin >> a >> b; g[a].pb(b); g[b].pb(a); } dfs(1); vll dp(n+7, 0); //cout << lca(5, 7) << endl; sort(v); for(auto i : v){ for(int j : g[i.se]){ if(a[j] > i.fr) continue; ll c = find(j); //cout << i.se << ' ' << j << ' ' << c << ' ' << lca(i.se, c) << ' ' << pref[i.se] << ' ' << pref[c] << endl; dp[i.se] = max(dp[i.se], dp[c] + pref[i.se] + pref[c] - 2*pref[lca(i.se, c)]); unite(j, i.se); } //cout << i.se << ' ' << dp[i.se] << endl; } cout << dp[v.back().se] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...