#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
const int N = 2e5+5;
const int mod = 998277353;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
int n;
vector<int> adj[N];
int sz[N], ans[N], dist[N], mx[N], lst[N];
bool check[N];
void predfs(int u,int p)
{
sz[u] = 1;
for (auto v : adj[u])
{
if (v==p || check[v]) continue;
predfs(v,u);
sz[u] += sz[v];
}
}
int centroid(int u, int p, int n)
{
for (auto v : adj[u])
{
if (check[v] || v==p) continue;
if (sz[v] > n/2) return centroid(v,u,n);
}
return u;
}
void dfs1(int u,int p)
{
dist[u] = dist[p]+1;
for (auto v : adj[u])
{
if (v==p || check[v]) continue;
dfs1(v,u);
}
mx[sz[u]] = max(mx[sz[u]],dist[u]);
}
void dfs(int u)
{
predfs(u,0);
int ct = centroid(u,0,sz[u]);
predfs(ct,0);
dist[ct] = 1;
int tot = 0;
//cout<<ct<<endl;
for (auto v : adj[ct])
{
if (check[v]) continue;
dfs1(v,ct);
//cout<<v<<endl;
tot = max(tot,sz[v]);
for (int i=sz[v]-1;i>=0;i--) mx[i] = max(mx[i],mx[i+1]);
// for (int i=0;i<=sz[v];i++) cout<<mx[i]<<" ";
// cout<<endl;
for (int i=sz[v];i>=0;i--)
{
ans[i*2] = max(ans[i*2], mx[i] + max(lst[i],1ll) - 1);
lst[i] = max(lst[i],mx[i]), mx[i] = 0;
}
}
//cout<<endl;
for (int i=0;i<=tot;i++) mx[i] = lst[i] = 0;
check[ct] = true;
for (auto v : adj[ct])
{
if (check[v]) continue;
dfs(v);
}
}
void solve()
{
cin>>n;
for (int i=1;i<n;i++)
{
int u,v,w; cin>>u>>v;
adj[u].pb(v); adj[v].pb(u);
}
dfs(1);
for (int i=1;i<=n;i++) cout<<(ans[i] ? ans[i] : 1)<<endl;
}
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
//cin>>t;
while (t--)
{
solve();
cout<<"\n";
}
}
Compilation message
meetings2.cpp: In function 'void solve()':
meetings2.cpp:102:11: warning: unused variable 'w' [-Wunused-variable]
102 | int u,v,w; cin>>u>>v;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5168 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5168 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
10 ms |
5468 KB |
Output is correct |
23 |
Correct |
8 ms |
5468 KB |
Output is correct |
24 |
Correct |
7 ms |
5512 KB |
Output is correct |
25 |
Correct |
7 ms |
5468 KB |
Output is correct |
26 |
Correct |
8 ms |
5504 KB |
Output is correct |
27 |
Correct |
8 ms |
5468 KB |
Output is correct |
28 |
Correct |
10 ms |
5316 KB |
Output is correct |
29 |
Correct |
8 ms |
5468 KB |
Output is correct |
30 |
Correct |
8 ms |
5468 KB |
Output is correct |
31 |
Correct |
7 ms |
5492 KB |
Output is correct |
32 |
Correct |
10 ms |
5468 KB |
Output is correct |
33 |
Correct |
9 ms |
5468 KB |
Output is correct |
34 |
Correct |
9 ms |
5428 KB |
Output is correct |
35 |
Correct |
10 ms |
5468 KB |
Output is correct |
36 |
Correct |
9 ms |
5528 KB |
Output is correct |
37 |
Correct |
9 ms |
5468 KB |
Output is correct |
38 |
Correct |
8 ms |
5356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
3 ms |
5168 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
3 ms |
4956 KB |
Output is correct |
19 |
Correct |
3 ms |
4952 KB |
Output is correct |
20 |
Correct |
3 ms |
4952 KB |
Output is correct |
21 |
Correct |
2 ms |
4956 KB |
Output is correct |
22 |
Correct |
10 ms |
5468 KB |
Output is correct |
23 |
Correct |
8 ms |
5468 KB |
Output is correct |
24 |
Correct |
7 ms |
5512 KB |
Output is correct |
25 |
Correct |
7 ms |
5468 KB |
Output is correct |
26 |
Correct |
8 ms |
5504 KB |
Output is correct |
27 |
Correct |
8 ms |
5468 KB |
Output is correct |
28 |
Correct |
10 ms |
5316 KB |
Output is correct |
29 |
Correct |
8 ms |
5468 KB |
Output is correct |
30 |
Correct |
8 ms |
5468 KB |
Output is correct |
31 |
Correct |
7 ms |
5492 KB |
Output is correct |
32 |
Correct |
10 ms |
5468 KB |
Output is correct |
33 |
Correct |
9 ms |
5468 KB |
Output is correct |
34 |
Correct |
9 ms |
5428 KB |
Output is correct |
35 |
Correct |
10 ms |
5468 KB |
Output is correct |
36 |
Correct |
9 ms |
5528 KB |
Output is correct |
37 |
Correct |
9 ms |
5468 KB |
Output is correct |
38 |
Correct |
8 ms |
5356 KB |
Output is correct |
39 |
Correct |
410 ms |
21332 KB |
Output is correct |
40 |
Correct |
411 ms |
20868 KB |
Output is correct |
41 |
Correct |
431 ms |
21228 KB |
Output is correct |
42 |
Correct |
464 ms |
21324 KB |
Output is correct |
43 |
Correct |
421 ms |
21444 KB |
Output is correct |
44 |
Correct |
396 ms |
21208 KB |
Output is correct |
45 |
Correct |
570 ms |
23888 KB |
Output is correct |
46 |
Correct |
471 ms |
24656 KB |
Output is correct |
47 |
Correct |
347 ms |
18888 KB |
Output is correct |
48 |
Correct |
260 ms |
19072 KB |
Output is correct |
49 |
Correct |
392 ms |
22108 KB |
Output is correct |
50 |
Correct |
274 ms |
22212 KB |
Output is correct |
51 |
Correct |
350 ms |
24516 KB |
Output is correct |