Submission #595719

#TimeUsernameProblemLanguageResultExecution timeMemory
595719penguinhackerMeetings 2 (JOI21_meetings2)C++17
0 / 100
3 ms4948 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ar array const int mxN=2e5; int n, d[mxN], s[mxN], anc[mxN][18], ans[mxN+1], tin[mxN], tout[mxN], t; vector<int> adj[mxN]; set<ar<int, 2>> active; void dfs(int u=0) { s[u]=1; tin[u]=t++; for (int i=1; i<18; ++i) anc[u][i]=anc[anc[u][i-1]][i-1]; for (int v : adj[u]) if (v!=anc[u][0]) { d[v]=d[u]+1, anc[v][0]=u; dfs(v); s[u]+=s[v]; } tout[u]=t-1; } bool ia(int u, int v) { return tin[u]<=tin[v]&&tout[v]<=tout[u]; } int lca(int u, int v) { if (ia(u, v)||ia(v, u)) return ia(u, v)?u:v; for (int i=17; ~i; --i) if (!ia(anc[u][i], v)) u=anc[u][i]; return anc[u][0]; } int dist(int u, int v) { return d[u]+d[v]-2*d[lca(u, v)]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i=1; i<n; ++i) { int u, v; cin >> u >> v, --u, --v; adj[u].push_back(v); adj[v].push_back(u); } dfs(); vector<ar<int, 2>> upd; for (int i=1; i<n; ++i) upd.push_back({s[i], i}); sort(upd.begin(), upd.end()); fill(ans, ans+n+1, 1); int best=1; for (int i=n/2; i; --i) { while(upd.size()&&upd.back()[0]>=i) { int u=upd.back()[1]; upd.pop_back(); /*auto it=active.lower_bound({tin[u]}); if (it!=active.begin()) { --it; int v=(*it)[1]; if (tin[u]+s[u]<=tin[v]+s[v]) active.erase(it); }*/ for (auto it=active.begin(); it!=active.end();) { if (ia((*it)[1], u)) it=active.erase(it); else ++it; } for (ar<int, 2> x : active) best=max(best, dist(u, x[1])+1); active.insert({tin[u], u}); for (ar<int, 2> x : active) { u=x[1]; if (n-s[u]>=i) { int v=u; for (int j=17; ~j; --j) if (n-s[anc[v][j]]>=i) v=anc[v][j]; best=max(best, d[u]-d[v]+2); } } } ans[2*i]=best; } for (int i=1; i<=n; ++i) cout << ans[i] << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...