This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |