#include <bits/stdc++.h>
using ll = long long;
#define int ll
using namespace std;
#define sz(x) (int)(x).size()
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define mod 998244353
#define db(x) cerr << __LINE__ << " " << #x << " " << x << "\n"
using vi = vector<int>;
using pi = pair<int, int>;
const ll N = 200005;
const ll inf = 1e18;
int n, dep[N], par[25][N], sz[N], mz[N], res[N];
vi adj[N], v[N];
void dfs(int u, int p){
sz[u] = 1;
mz[u] = 0;
for(auto &i : adj[u]){
if(i == p) continue;
par[0][i] = u;
dep[i] = dep[u] + 1;
dfs(i, u);
sz[u] += sz[i];
mz[u] = max(mz[u], sz[i]);
}
}
int center(){
dfs(1, 0);
pi res(inf, -inf);
foru(i, 1, n){
int ryz = max(n - sz[i], mz[i]);
res = min(res, pi(ryz, i));
}
return res.se;
}
int lca(int x, int y){
if(dep[x] > dep[y]) swap(x, y);
int dif = dep[y] - dep[x];
for(int i = 0; dif; i++, dif >>= 1){
if(dif & 1) y = par[i][y];
}
if(x == y) return x;
ford(i, 16, 0){
if(par[i][x] != par[i][y]){
x = par[i][x];
y = par[i][y];
}
}
return par[0][x];
}
int dis(int x, int y){
return dep[x] - dep[lca(x, y)] + dep[y] - dep[lca(x, y)];
}
void solve(){
cin >> n;
foru(i, 1, n - 1){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int cen = center();
dfs(cen, -1);
foru(i, 1, 20){
foru(j, 1, n){
par[i][j] = par[i - 1][par[i - 1][j]];
}
}
foru(i, 1, n){
if(i == cen) continue;
v[sz[i]].push_back(i);
}
pi cur(cen, cen);
ford(i, n >> 1, 0){
for(auto &i : v[i]){
int f = cur.fi;
int s = cur.se;
cur = max(cur, pi(f, i), [&](pi a, pi b){
return dis(a.fi, a.se) < dis(b.fi, b.se);
});
cur = max(cur, pi(s, i), [&](pi a, pi b){
return dis(a.fi, a.se) < dis(b.fi, b.se);
});
}
res[i << 1] = dis(cur.fi, cur.se);
}
foru(i, 1, n) cout << res[i] + 1 << "\n";
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1; // cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9824 KB |
Output is correct |
3 |
Correct |
5 ms |
9824 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9856 KB |
Output is correct |
11 |
Correct |
5 ms |
9800 KB |
Output is correct |
12 |
Correct |
5 ms |
9840 KB |
Output is correct |
13 |
Correct |
5 ms |
9804 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
6 ms |
9812 KB |
Output is correct |
16 |
Correct |
5 ms |
9896 KB |
Output is correct |
17 |
Correct |
5 ms |
9812 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9824 KB |
Output is correct |
3 |
Correct |
5 ms |
9824 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9856 KB |
Output is correct |
11 |
Correct |
5 ms |
9800 KB |
Output is correct |
12 |
Correct |
5 ms |
9840 KB |
Output is correct |
13 |
Correct |
5 ms |
9804 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
6 ms |
9812 KB |
Output is correct |
16 |
Correct |
5 ms |
9896 KB |
Output is correct |
17 |
Correct |
5 ms |
9812 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
8 ms |
10836 KB |
Output is correct |
23 |
Correct |
9 ms |
10836 KB |
Output is correct |
24 |
Correct |
8 ms |
10836 KB |
Output is correct |
25 |
Correct |
8 ms |
10836 KB |
Output is correct |
26 |
Correct |
11 ms |
10836 KB |
Output is correct |
27 |
Correct |
8 ms |
10864 KB |
Output is correct |
28 |
Correct |
8 ms |
10836 KB |
Output is correct |
29 |
Correct |
8 ms |
10836 KB |
Output is correct |
30 |
Correct |
8 ms |
10816 KB |
Output is correct |
31 |
Correct |
8 ms |
10812 KB |
Output is correct |
32 |
Correct |
9 ms |
10964 KB |
Output is correct |
33 |
Correct |
8 ms |
10964 KB |
Output is correct |
34 |
Correct |
7 ms |
10880 KB |
Output is correct |
35 |
Correct |
8 ms |
10836 KB |
Output is correct |
36 |
Correct |
8 ms |
10880 KB |
Output is correct |
37 |
Correct |
8 ms |
10836 KB |
Output is correct |
38 |
Correct |
8 ms |
10836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9824 KB |
Output is correct |
3 |
Correct |
5 ms |
9824 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
5 ms |
9856 KB |
Output is correct |
11 |
Correct |
5 ms |
9800 KB |
Output is correct |
12 |
Correct |
5 ms |
9840 KB |
Output is correct |
13 |
Correct |
5 ms |
9804 KB |
Output is correct |
14 |
Correct |
5 ms |
9812 KB |
Output is correct |
15 |
Correct |
6 ms |
9812 KB |
Output is correct |
16 |
Correct |
5 ms |
9896 KB |
Output is correct |
17 |
Correct |
5 ms |
9812 KB |
Output is correct |
18 |
Correct |
5 ms |
9812 KB |
Output is correct |
19 |
Correct |
5 ms |
9812 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
5 ms |
9812 KB |
Output is correct |
22 |
Correct |
8 ms |
10836 KB |
Output is correct |
23 |
Correct |
9 ms |
10836 KB |
Output is correct |
24 |
Correct |
8 ms |
10836 KB |
Output is correct |
25 |
Correct |
8 ms |
10836 KB |
Output is correct |
26 |
Correct |
11 ms |
10836 KB |
Output is correct |
27 |
Correct |
8 ms |
10864 KB |
Output is correct |
28 |
Correct |
8 ms |
10836 KB |
Output is correct |
29 |
Correct |
8 ms |
10836 KB |
Output is correct |
30 |
Correct |
8 ms |
10816 KB |
Output is correct |
31 |
Correct |
8 ms |
10812 KB |
Output is correct |
32 |
Correct |
9 ms |
10964 KB |
Output is correct |
33 |
Correct |
8 ms |
10964 KB |
Output is correct |
34 |
Correct |
7 ms |
10880 KB |
Output is correct |
35 |
Correct |
8 ms |
10836 KB |
Output is correct |
36 |
Correct |
8 ms |
10880 KB |
Output is correct |
37 |
Correct |
8 ms |
10836 KB |
Output is correct |
38 |
Correct |
8 ms |
10836 KB |
Output is correct |
39 |
Correct |
327 ms |
57844 KB |
Output is correct |
40 |
Correct |
325 ms |
56656 KB |
Output is correct |
41 |
Correct |
317 ms |
58100 KB |
Output is correct |
42 |
Correct |
348 ms |
58780 KB |
Output is correct |
43 |
Correct |
315 ms |
58748 KB |
Output is correct |
44 |
Correct |
330 ms |
58996 KB |
Output is correct |
45 |
Correct |
602 ms |
64640 KB |
Output is correct |
46 |
Correct |
323 ms |
67880 KB |
Output is correct |
47 |
Correct |
219 ms |
58764 KB |
Output is correct |
48 |
Correct |
136 ms |
58760 KB |
Output is correct |
49 |
Correct |
459 ms |
59068 KB |
Output is correct |
50 |
Correct |
185 ms |
58780 KB |
Output is correct |
51 |
Correct |
239 ms |
66788 KB |
Output is correct |