Submission #1098253

#TimeUsernameProblemLanguageResultExecution timeMemory
1098253onbertMeetings 2 (JOI21_meetings2)C++17
20 / 100
1188 ms1628 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 4005, lg = 20; int n; vector<int> adj[maxn]; int lift[maxn][lg]; int d[maxn], sz[maxn]; void dfs(int u) { sz[u] = 1; for (int i=1;i<lg;i++) { if (lift[u][i-1]==-1 || lift[lift[u][i-1]][i-1]==-1) break; lift[u][i] = lift[lift[u][i-1]][i-1]; } for (int v:adj[u]) { adj[v].erase(find(adj[v].begin(), adj[v].end(), u)); lift[v][0] = u; d[v] = d[u] + 1; dfs(v); sz[u] += sz[v]; } } int lca(int u, int v) { if (d[u] > d[v]) swap(u, v); for (int i=lg-1;i>=0;i--) { if (d[v] - (1<<i) >= d[u]) v = lift[v][i]; } if (u==v) return u; for (int i=lg-1;i>=0;i--) if (lift[u][i]!=lift[v][i]) { u = lift[u][i], v = lift[v][i]; } return lift[u][0]; } int lift2(int u, int D) { for (int i=lg-1;i>=0;i--) { if (d[u] - (1<<i) >= D) u = lift[u][i]; } return u; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i=1;i<=n-1;i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i=1;i<=n;i++) for (int j=0;j<lg;j++) lift[i][j] = -1; d[1] = 0; dfs(1); int ans[n/2+1]; for (int i=1;i<=n/2;i++) ans[i] = 1; for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) { int m = lca(i, j); int a = sz[i], b = sz[j]; if (m==i) { int thing = lift2(j, d[i]+1); a = n - sz[thing]; } if (m==j) { int thing = lift2(i, d[j]+1); b = n - sz[thing]; } int dist = d[i] + d[j] - d[m] * 2; // cout << i << " " << j << " " << dist << endl; // cout << a << " " << b << endl; ans[min(a, b)] = max(dist + 1, ans[min(a, b)]); } for (int i=n/2-1;i>=1;i--) ans[i] = max(ans[i], ans[i+1]); for (int i=1;i<=n;i++) { if (i%2==0) cout << ans[i/2] << endl; else cout << "1\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...