Submission #1303656

#TimeUsernameProblemLanguageResultExecution timeMemory
1303656nekolieMeetings 2 (JOI21_meetings2)C++20
100 / 100
411 ms31984 KiB
#include <bits/stdc++.h> using namespace std; const int M = 200001; int rozm[M], cntr, odl[M], p[M], jp[M], odp[M]; vector<int> tree[M], vert[M]; int centroid(int v, int n) { rozm[v] = 1; bool uwu = true; for (int u : tree[v]) if (u != p[v]) p[u] = v, centroid(u,n), uwu = (uwu && (rozm[u] <= n/2)), rozm[v] += rozm[u]; if (uwu && rozm[v] >= (n+1)/2) cntr = v; return cntr; } void dfs(int v) { rozm[v] = 1; if (v != cntr) jp[v] = ((odl[p[v]]+odl[jp[jp[p[v]]]] == 2*odl[jp[p[v]]]) ? jp[jp[p[v]]] : p[v]); for (int u : tree[v]) if (u != p[v]) p[u] = v, odl[u] = odl[v]+1, dfs(u), rozm[v] += rozm[u]; if (v != cntr) vert[rozm[v]].push_back(v); } int jump(int v, int depth) { while (odl[v] > depth) v = ((odl[jp[v]] < depth) ? p[v] : jp[v]); return v; } int lca(int a, int b) { if (odl[a] < odl[b]) swap(a,b); a = jump(a,odl[b]); while (a != b) { if (jp[a] == jp[b]) a = p[a], b = p[b]; else a = jp[a], b = jp[b]; } return a; } int dist(int a, int b) { return odl[a]+odl[b]-2*odl[lca(a,b)]; } void update(int &a, int &b, int c) { if (dist(a,c) >= max(dist(a,b),dist(b,c))) b = c; else if (dist(b,c) >= max(dist(a,b),dist(a,c))) a = c; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,a,b; cin >> n; for (int i = 1; i < n; i++) cin >> a >> b, tree[a].push_back(b), tree[b].push_back(a); centroid(1,n), p[cntr] = 0, dfs(cntr), a = b = cntr; for (int i = n; i > 0; i--) { if (i&1) odp[i] = 1; else { for (int v : vert[i/2]) update(a,b,v); odp[i] = dist(a,b)+1; } } for (int i = 1; i <= n; i++) cout << odp[i] << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...