Submission #729486

#TimeUsernameProblemLanguageResultExecution timeMemory
729486MohammadAghilCat Exercise (JOI23_ho_t4)C++17
100 / 100
384 ms40044 KiB
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize ("Ofast,unroll-loops") // #pragma GCC target ("avx2") #define rep(i,l,r) for(int i = (l); i < (r); i++) #define per(i,r,l) for(int i = (r); i >= (l); i--) #define all(x) begin(x), end(x) #define sz(x) (int)size(x) #define pb push_back #define ff first #define ss second typedef long long ll; typedef pair<int, int> pp; void dbg(){ cerr << endl; } template<typename H, typename... T> void dbg(H h, T... t){ cerr << h << ", "; dbg(t...); } void IOS(){ cin.tie(0) -> sync_with_stdio(0); #ifndef ONLINE_JUDGE // freopen("inp.txt", "r", stdin); // freopen("out.txt", "w", stdout); #define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">: ", dbg(__VA_ARGS__) #else #define er(...) 0 #endif } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll mod = 1e9 + 7, maxn = 2e5 + 5, maxk = 31, lg = 19, inf = ll(1e18) + 5; int par[maxn][lg], h[maxn]; vector<int> adj[maxn]; void dfs(int r, int p){ par[r][0] = p; rep(j,1,lg){ par[r][j] = par[par[r][j-1]][j-1]; } for(int c: adj[r]) if(c - p){ h[c] = h[r] + 1; dfs(c, r); } } int lca(int u, int v){ if(h[u] < h[v]) swap(u, v); per(i,lg-1,0) if(h[par[u][i]] >= h[v]) u = par[u][i]; if(u == v) return u; per(i,lg-1,0) if(par[u][i] - par[v][i]) u = par[u][i], v = par[v][i]; return par[u][0]; } int dist(int u, int v){ return h[u] + h[v] - 2*h[lca(u, v)]; } int pr[maxn]; int get(int x){ return pr[x] == -1? x: pr[x] = get(pr[x]); } ll dp[maxn]; int main(){ IOS(); int n; cin >> n; vector<int> h(n); rep(i,0,n){ cin >> h[i]; pr[i] = -1; } rep(i,1,n){ int u, v; cin >> u >> v; u--, v--; adj[u].pb(v), adj[v].pb(u); } dfs(0, 0); vector<int> srt(n); iota(all(srt), 0); sort(all(srt), [&](int u, int v){ return h[u] < h[v]; }); for(int r: srt){ for(int c: adj[r]) if(h[c] < h[r]){ c = get(c); dp[r] = max(dp[r], dp[c] + dist(c, r)); pr[c] = r; } } cout << dp[srt.back()] << '\n'; return 0; }
#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...