제출 #670525

#제출 시각아이디문제언어결과실행 시간메모리
670525MilosMilutinovicMeetings 2 (JOI21_meetings2)C++14
100 / 100
624 ms63900 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<vector<int>> g(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; --u; --v; g[u].push_back(v); g[v].push_back(u); } vector<int> sz(n); function<void(int, int)> DfsSz = [&](int v, int pv) { sz[v] = 1; for (int u : g[v]) { if (u == pv) { continue; } DfsSz(u, v); sz[v] += sz[u]; } }; DfsSz(0, 0); function<int(int, int)> Find = [&](int v, int pv) { for (int u : g[v]) { if (u == pv) { continue; } if (sz[u] * 2 >= n) { return Find(u, v); } } return v; }; int c = Find(0, 0); const int L = 25; vector<vector<int>> jump(n, vector<int>(L)); vector<int> dep(n); vector<int> dfn(n); vector<int> dfo(n); int T = 0; function<void(int, int)> Dfs = [&](int v, int pv) { dfn[v] = ++T; sz[v] = 1; jump[v][0] = pv; for (int u : g[v]) { if (u == pv) { continue; } dep[u] = dep[v] + 1; Dfs(u, v); sz[v] += sz[u]; } dfo[v] = T; }; Dfs(c, c); for (int j = 1; j < L; j++) { for (int i = 0; i < n; i++) { jump[i][j] = jump[jump[i][j - 1]][j - 1]; } } auto LCA = [&](int x, int y) { if (dep[x] < dep[y]) { swap(x, y); } for (int i = L - 1; i >= 0; i--) { if (dep[jump[x][i]] >= dep[y]) { x = jump[x][i]; } } if (x == y) { return x; } for (int i = L - 1; i >= 0; i--) { if (jump[x][i] != jump[y][i]) { x = jump[x][i]; y = jump[y][i]; } } return jump[x][0]; }; auto GetDist = [&](int x, int y) { return dep[x] + dep[y] - 2 * dep[LCA(x, y)]; }; vector<vector<int>> ver(n + 1); for (int i = 0; i < n; i++) { ver[sz[i]].push_back(i); } int x = c; int y = c; int d = 0; vector<int> ans(n + 1); for (int i = n / 2; i >= 1; i--) { for (int z : ver[i]) { int d_xz = GetDist(x, z); int d_yz = GetDist(y, z); if (d_xz < d_yz) { swap(x, y); swap(d_xz, d_yz); } if (d_xz > d) { d = d_xz; y = z; } } ans[2 * i] = d; } for (int i = 1; i <= n; i++) { cout << ans[i] + 1 << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...