제출 #836832

#제출 시각아이디문제언어결과실행 시간메모리
836832jasminCat Exercise (JOI23_ho_t4)C++17
7 / 100
1 ms512 KiB
//JOI 2023 Final Round #include<bits/stdc++.h> using namespace std; struct ufind{ vector<int> chef; ufind(int n){ chef.assign(n, 0); iota(chef.begin(), chef.end(), 0); } int find(int a){ if(chef[a]==a) return a; return chef[a] = find(chef[a]); } void unite(int a, int b, vector<int>& h){ if(h[a] < h[b]){ chef[a] = b; } else{ chef[b] = a; } } }; struct tree{ int n; vector<vector<int> > adi; vector<int> tin; vector<int> tout; vector<int> h; int t=0; const int L=25; vector<vector<int> > p; tree(int ninput){ n=ninput; adi.assign(n, {}); tin.assign(n, 0); tout.assign(n, 0); h.assign(n, 0); p.assign(L, vector<int> (n, -1)); } void dfs(int v, int par){ tin[v]=t; t++; p[0][v]= par; for(int i=1; i<L; i++){ if(p[i-1][v] == -1) break; p[i][v] = p[p[i-1][v]][i-1]; } for(auto u: adi[v]){ if(u==par) continue; h[u] = h[v]+1; dfs(u, v); } tout[v]=t; t++; } bool ancestor(int a, int b){ return tin[a]<=tin[b] && tout[b]<=tout[a]; } int lca(int a, int b){ if(ancestor(a, b)) return a; if(ancestor(b, a)) return b; for(int i=L-1; i>=0; i--){ if(p[i][a]!=-1 && !ancestor(p[i][a], b)){ a = p[i][a]; } } return p[0][a]; } int dist(int a, int b){ int x=lca(a, b); int dist = h[a]-h[x] + h[b]-h[x]; return dist; } }; signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> h(n); for(int i=0; i<n; i++){ cin >> h[i]; } tree g(n); for(int i=0; i<n-1; i++){ int a, b; cin >> a >> b; a--; b--; g.adi[a].push_back(b); g.adi[b].push_back(a); } g.dfs(0, -1); vector<int> sorted(n); iota(sorted.begin(), sorted.end(), 0); sort(sorted.begin(), sorted.end(), [&](int i, int j){ return h[i] < h[j]; }); vector<int> ans(n, 0); ufind uf(n); int maxi=0; for(auto v: sorted){ for(auto u: g.adi[v]){ if(h[u] > h[v]) continue; u=uf.find(u); ans[v] = max(ans[v], ans[u] + g.dist(v, u)); uf.unite(v, u, h); } maxi = max(maxi, ans[v]); } cout << maxi << "\n"; }
#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...