Submission #781779

#TimeUsernameProblemLanguageResultExecution timeMemory
781779AndreyCat Exercise (JOI23_ho_t4)C++14
21 / 100
150 ms44796 KiB
#include <bits/stdc++.h> using namespace std; vector<int> h(200001); vector<int> haha[200001]; vector<int> dsu(200001); vector<int> dep(200001); int bruh[200001][19]; void dfs(int a, int t, int d) { bruh[a][0] = t; dep[a] = d; for(int v: haha[a]) { if(v != t) { dfs(v,a,d+1); } } } int check(int a) { int c = a,d,e; while(dsu[c] != c) { c = dsu[c]; } d = a; while(d != dsu[d]) { e = dsu[d]; dsu[d] = c; d = e; } return c; } int dist(int a, int b) { if(dep[a] < dep[b]) { swap(a,b); } int c = dep[a]-dep[b],e = a; for(int i = 0; i < 19; i++) { if((1 << i)&c) { e = bruh[e][i]; } } if(b != e) { for(int i = 18; i >= 0; i--) { if(bruh[e][i] != -1 && bruh[e][i] != bruh[b][i]) { e = bruh[e][i]; b = bruh[b][i]; } } e = bruh[e][0]; } return dep[a]-dep[e]+dep[b]-dep[e]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for(int i = 1; i <= 200000; i++) { dsu[i] = i; } int n,a,b,y = 0; cin >> n; for(int i = 1; i <= n; i++) { cin >> h[i]; } vector<pair<int,pair<int,int>>> edge(0); for(int i = 0; i < n-1; i++) { cin >> a >> b; haha[a].push_back(b); haha[b].push_back(a); edge.push_back({max(h[a],h[b]),{a,b}}); } sort(edge.begin(),edge.end()); dfs(1,-1,0); for(int i = 1; i < 19; i++) { for(int j = 1; j <= n; j++) { if(bruh[j][i-1] != -1) { bruh[j][i] = bruh[bruh[j][i-1]][i-1]; } else { bruh[j][i] = -1; } } } vector<int> ans(n+1); for(int i = 1; i <= n; i++) { while(y < n-1 && edge[y].first == i) { a = edge[y].second.first; b = edge[y].second.second; if(h[a] < h[b]) { swap(a,b); } b = check(b); dsu[b] = a; ans[a] = max(ans[a],dist(a,b)+ans[b]); y++; } } for(int i = 1; i <= n; i++) { if(h[i] == n) { cout << ans[i]; } } 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...