Submission #1182646

#TimeUsernameProblemLanguageResultExecution timeMemory
1182646jerzykCat Exercise (JOI23_ho_t4)C++20
100 / 100
216 ms62124 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define st first #define nd second typedef long long ll; typedef long double ld; const ll I = 1000'000'000'000'000'000LL; const int II = 2'000'000'000; const ll M = 1000'000'007LL; const int N = 1000'007; int fau[N]; ll ans[N]; int per[N]; vector<int> ed[N]; int wys[N]; int jp[N][20]; int Find(int v) { if(fau[v] == v) return v; return (fau[v] = Find(fau[v])); } void Union(int a, int b) { fau[Find(b)] = Find(a); } void DFS(int v) { for(int i = 1; i <= 19; ++i) jp[v][i] = jp[jp[v][i - 1]][i - 1]; for(int i = 0; i < (int)ed[v].size(); ++i) { if(wys[ed[v][i]]) continue; wys[ed[v][i]] = wys[v] + 1; jp[ed[v][i]][0] = v; DFS(ed[v][i]); } } int Dis(int a, int b) { int ans = 0; if(wys[a] < wys[b]) swap(a, b); for(int i = 19; i >= 0; --i) if(wys[jp[a][i]] >= wys[b]) {a = jp[a][i]; ans += (1<<i);} for(int i = 19; i >= 0; --i) if(jp[a][i] != jp[b][i]) { a = jp[a][i]; b = jp[b][i]; ans += 2 * (1<<i); } if(a == b) return ans; return ans + 2; } void Solve() { int n, a, b; cin >> n; for(int i = 1; i <= n; ++i) cin >> per[i]; for(int i = 1; i < n; ++i) { cin >> a >> b; a = per[a]; b = per[b]; ed[a].pb(b); ed[b].pb(a); } wys[1] = 1; DFS(1); for(int v = 1; v <= n; ++v) { int ne; fau[v] = v; //cout << v << ":\n"; for(int j = 0; j < (int)ed[v].size(); ++j) { if(ed[v][j] > v) continue; ne = Find(ed[v][j]); //cout << "E: " << ed[v][j] << " " << ne << " " << Dis(ne, v) << "\n"; ans[v] = max(ans[v], ans[ne] + Dis(ne, v)); Union(v, ne); } } cout << ans[n] << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); //int t; cin >> t; //while(t--) Solve(); 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...