Submission #397927

#TimeUsernameProblemLanguageResultExecution timeMemory
397927Jarif_RahmanMeetings 2 (JOI21_meetings2)C++17
20 / 100
4064 ms53676 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; int n; vector<vector<int>> v; vector<int> ans; vector<set<pair<int, int>>> s; vector<int> sz; vector<multiset<int>> ms; void dfs_sz(int nd, int ss){ for(int x: v[nd]) if(x != ss) dfs_sz(x, nd); for(int x: v[nd]) if(x != ss) sz[nd]+=sz[x]; } void dfs(int nd, int ss, int dis, bool keep){ int mx = 0, in = -1; for(int x: v[nd]) if(x != ss && sz[x] > mx) mx = sz[x], in = x; for(int x: v[nd]) if(x != ss && x != in) dfs(x, nd, dis+1, 0); if(in != -1){ dfs(in, nd, dis+1, 1); swap(s[nd], s[in]); for(auto [szz0, d0]: s[nd]){ int szz = szz0, d = d0; if(n-sz[in] >= szz) ans[szz] = max(ans[szz], d-dis+1); } } for(int x: v[nd]) if(x != ss){ for(auto [szz0, d0]: s[x]){ int d = d0, szz = szz0; if(n-sz[x] >= szz) ans[szz] = max(ans[szz], d-dis+1); if(!ms[szz].empty()){ int cur = *ms[szz].rbegin(); cur+=d; cur-=2*dis; cur++; ans[szz] = max(ans[szz], cur); } } for(auto [szz0, d0]: s[x]){ int d = d0, szz = szz0; auto it = s[nd].lower_bound(make_pair(szz, 0)); if(it == s[nd].end() || it->f != szz){ s[nd].insert({szz, d}); ms[szz].insert(d); } else if(it->sc < d){ ms[szz].erase(ms[szz].find(it->sc)); s[nd].erase(it); s[nd].insert({szz, d}); ms[szz].insert(d); } } s[x].clear(); } int szz = n-sz[nd]+1; if(!ms[szz].empty()) ans[szz] = max(ans[szz], (*ms[szz].rbegin())-dis+1); for(int i = mx+1; i <= sz[nd]; i++){ ms[i].insert(dis); s[nd].insert({i, dis}); } if(!keep){ for(auto [szz0, d0]: s[nd]){ int d = d0, szz = szz0; ms[szz].erase(ms[szz].find(d)); } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; v.resize(n); ans.resize(n+1, 1); sz.resize(n, 1); s.resize(n); ms.resize(n+1); for(int i = 0; i < n-1; i++){ int a, b; cin >> a >> b; a--, b--; v[a].pb(b); v[b].pb(a); } dfs_sz(0, -1); dfs(0, -1, 0, 1); for(int i = n-1; i >= 0; i--) ans[i] = max(ans[i], ans[i+1]); for(int i = 1; i <= n; i++){ if(i%2 == 0) cout << ans[i/2] << "\n"; else cout << "1\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...