Submission #1162724

#TimeUsernameProblemLanguageResultExecution timeMemory
1162724fryingducMeetings 2 (JOI21_meetings2)C++20
100 / 100
225 ms39700 KiB
// centroid, not center #include "bits/stdc++.h" using namespace std; #ifdef duc_debug #include "bits/debug.h" #else #define debug(...) #endif const int maxn = 2e5 + 5; const int LG = 19; int n, h[maxn]; vector<int> g[maxn]; int up[maxn][LG + 1]; int sz[maxn]; int ord[maxn]; int res[maxn]; int find_centroid(int u, int prev, int n) { for (auto v:g[u]) { if (v == prev) continue; if (sz[v] * 2 > n) return find_centroid(v, u, n); } return u; } void dfs(int u, int prev) { sz[u] = 1; for (auto v:g[u]) { if (v == prev) continue; h[v] = h[u] + 1; up[v][0] = u; dfs(v, u); sz[u] += sz[v]; } } int lca(int u, int v) { if (h[u] < h[v]) swap(u, v); for (int i = LG; i >= 0; --i) { if ((h[u] - h[v]) >> i & 1) { u = up[u][i]; } } if (u == v) return u; for (int i = LG; i >= 0; --i) { if (up[u][i] != up[v][i]) { u = up[u][i], v = up[v][i]; } } return up[v][0]; } int dist(int u, int v) { return h[u] + h[v] - 2 * h[lca(u, v)]; } void solve() { cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } dfs(1, 0); int c = find_centroid(1, 0, n); h[c] = 0; up[c][0] = 0; dfs(c, 0); for (int i = 1; i <= LG; ++i) { for (int u = 1; u <= n; ++u) { up[u][i] = up[up[u][i - 1]][i - 1]; } } iota(ord + 1, ord + n + 1, 1); sort(ord + 1, ord + n + 1, [](const int &x, const int &y) -> bool { return sz[x] > sz[y]; }); debug(c); int x = c, y = c; int diam = 0; for (int i = 2; i <= n; ++i) { int cur = 0; while (i + cur <= n and sz[ord[i]] == sz[ord[i + cur]]) { int u = ord[i + cur]; vector<pair<int, int>> vec; vec.emplace_back(u, x); vec.emplace_back(u, y); vec.emplace_back(x, y); for (auto [a, b] : vec) { if (diam < dist(a, b)) { diam = dist(a, b), x = a, y = b; } } ++cur; } res[sz[ord[i]]] = diam; i = i + cur - 1; } for (int i = n; i; --i) { res[i] = max(res[i], res[i + 1]); } for (int i = 1; i <= n; ++i) { cout << (i & 1 ? 1 : 1 + res[i / 2]) << '\n'; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...