#include<bits/stdc++.h>
#define int long long
#define ll pair<int,int>
using namespace std;
const int N=400022;
set<int> ke[N];
int maxx[N];
int h[N];
int kq[N];
int oo=1e9;
int kq2[N];
int sz[N];
void dfs(int u,int pa)
{
sz[u]=1;
for (int v : ke[u])
{
if (v==pa) continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
int dfs2(int u,int pa,int kk)
{
for (int v : ke[u])
{
if (v==pa) continue;
if (sz[v]>kk/2) return dfs2(v,u,kk);
}
return u;
}
void dfs3(int u,int pa,int kk)
{
sz[u]=1;
h[u]=kk;
for (int v : ke[u])
{
if (v==pa) continue;
dfs3(v,u,kk+1);
sz[u]+=sz[v];
}
kq[sz[u]]=max(kq[sz[u]],h[u]);
}
void gg(int u)
{
dfs(u,0);
int dinh=dfs2(u,0,sz[u]);
h[dinh]=0;
for (int i=1;i<=sz[u];i++) kq[i]=-oo,maxx[i]=0;
maxx[dinh]=0;
int cur=1;
for (int v : ke[dinh])
{
dfs3(v,dinh,1);
kq[sz[v]+1]=0;
for (int i2=sz[v]+1;i2>=2;i2--) kq[i2-1]=max(kq[i2-1],kq[i2]);
for (int i2=1;i2<=sz[v]+1;i2++)
{
kq2[i2*2]=max(kq2[i2*2],kq[i2]+maxx[i2]);
}
for (int i2=1;i2<=sz[v]+1;i2++)
{
maxx[i2]=max(maxx[i2],kq[i2]);
kq[i2]=-oo;
}
for (int i2=cur;i2<=cur+sz[v];i2++) maxx[i2]=max(maxx[i2],0ll);
cur+=sz[v];
}
set<int> lol=ke[dinh];
set<int> :: iterator itr;
for (itr=lol.begin();itr!=lol.end();itr++)
{
int v=*itr;
ke[v].erase(dinh);
ke[dinh].erase(v);
gg(v);
}
}
signed main()
{
// freopen("kk.inp","r",stdin);
// freopen("kk.ans","w",stdout);
// freopen("City.inp","r",stdin);
// freopen("City.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i=1;i<n;i++)
{
int u,v;
cin >> u >> v;
ke[u].insert(v);
ke[v].insert(u);
}
gg(1);
for (int i=1;i<=n;i++)
{
cout << kq2[i]+1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19196 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
8 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
8 ms |
19036 KB |
Output is correct |
7 |
Correct |
8 ms |
19036 KB |
Output is correct |
8 |
Correct |
9 ms |
19088 KB |
Output is correct |
9 |
Correct |
8 ms |
19036 KB |
Output is correct |
10 |
Correct |
8 ms |
19036 KB |
Output is correct |
11 |
Correct |
8 ms |
19272 KB |
Output is correct |
12 |
Correct |
8 ms |
19292 KB |
Output is correct |
13 |
Correct |
8 ms |
19036 KB |
Output is correct |
14 |
Correct |
8 ms |
19060 KB |
Output is correct |
15 |
Correct |
7 ms |
19036 KB |
Output is correct |
16 |
Correct |
8 ms |
19252 KB |
Output is correct |
17 |
Correct |
8 ms |
19292 KB |
Output is correct |
18 |
Correct |
8 ms |
19032 KB |
Output is correct |
19 |
Correct |
8 ms |
19032 KB |
Output is correct |
20 |
Correct |
9 ms |
19032 KB |
Output is correct |
21 |
Correct |
8 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19196 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
8 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
8 ms |
19036 KB |
Output is correct |
7 |
Correct |
8 ms |
19036 KB |
Output is correct |
8 |
Correct |
9 ms |
19088 KB |
Output is correct |
9 |
Correct |
8 ms |
19036 KB |
Output is correct |
10 |
Correct |
8 ms |
19036 KB |
Output is correct |
11 |
Correct |
8 ms |
19272 KB |
Output is correct |
12 |
Correct |
8 ms |
19292 KB |
Output is correct |
13 |
Correct |
8 ms |
19036 KB |
Output is correct |
14 |
Correct |
8 ms |
19060 KB |
Output is correct |
15 |
Correct |
7 ms |
19036 KB |
Output is correct |
16 |
Correct |
8 ms |
19252 KB |
Output is correct |
17 |
Correct |
8 ms |
19292 KB |
Output is correct |
18 |
Correct |
8 ms |
19032 KB |
Output is correct |
19 |
Correct |
8 ms |
19032 KB |
Output is correct |
20 |
Correct |
9 ms |
19032 KB |
Output is correct |
21 |
Correct |
8 ms |
19036 KB |
Output is correct |
22 |
Correct |
10 ms |
19800 KB |
Output is correct |
23 |
Correct |
10 ms |
19636 KB |
Output is correct |
24 |
Correct |
10 ms |
19804 KB |
Output is correct |
25 |
Correct |
14 ms |
19852 KB |
Output is correct |
26 |
Correct |
13 ms |
19804 KB |
Output is correct |
27 |
Correct |
11 ms |
19800 KB |
Output is correct |
28 |
Correct |
10 ms |
19804 KB |
Output is correct |
29 |
Correct |
10 ms |
19812 KB |
Output is correct |
30 |
Correct |
10 ms |
19648 KB |
Output is correct |
31 |
Correct |
10 ms |
19632 KB |
Output is correct |
32 |
Correct |
11 ms |
19828 KB |
Output is correct |
33 |
Correct |
11 ms |
20056 KB |
Output is correct |
34 |
Correct |
10 ms |
19800 KB |
Output is correct |
35 |
Correct |
10 ms |
19804 KB |
Output is correct |
36 |
Correct |
11 ms |
19712 KB |
Output is correct |
37 |
Correct |
10 ms |
19780 KB |
Output is correct |
38 |
Correct |
10 ms |
20032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
19036 KB |
Output is correct |
2 |
Correct |
9 ms |
19196 KB |
Output is correct |
3 |
Correct |
8 ms |
19036 KB |
Output is correct |
4 |
Correct |
8 ms |
19036 KB |
Output is correct |
5 |
Correct |
9 ms |
19036 KB |
Output is correct |
6 |
Correct |
8 ms |
19036 KB |
Output is correct |
7 |
Correct |
8 ms |
19036 KB |
Output is correct |
8 |
Correct |
9 ms |
19088 KB |
Output is correct |
9 |
Correct |
8 ms |
19036 KB |
Output is correct |
10 |
Correct |
8 ms |
19036 KB |
Output is correct |
11 |
Correct |
8 ms |
19272 KB |
Output is correct |
12 |
Correct |
8 ms |
19292 KB |
Output is correct |
13 |
Correct |
8 ms |
19036 KB |
Output is correct |
14 |
Correct |
8 ms |
19060 KB |
Output is correct |
15 |
Correct |
7 ms |
19036 KB |
Output is correct |
16 |
Correct |
8 ms |
19252 KB |
Output is correct |
17 |
Correct |
8 ms |
19292 KB |
Output is correct |
18 |
Correct |
8 ms |
19032 KB |
Output is correct |
19 |
Correct |
8 ms |
19032 KB |
Output is correct |
20 |
Correct |
9 ms |
19032 KB |
Output is correct |
21 |
Correct |
8 ms |
19036 KB |
Output is correct |
22 |
Correct |
10 ms |
19800 KB |
Output is correct |
23 |
Correct |
10 ms |
19636 KB |
Output is correct |
24 |
Correct |
10 ms |
19804 KB |
Output is correct |
25 |
Correct |
14 ms |
19852 KB |
Output is correct |
26 |
Correct |
13 ms |
19804 KB |
Output is correct |
27 |
Correct |
11 ms |
19800 KB |
Output is correct |
28 |
Correct |
10 ms |
19804 KB |
Output is correct |
29 |
Correct |
10 ms |
19812 KB |
Output is correct |
30 |
Correct |
10 ms |
19648 KB |
Output is correct |
31 |
Correct |
10 ms |
19632 KB |
Output is correct |
32 |
Correct |
11 ms |
19828 KB |
Output is correct |
33 |
Correct |
11 ms |
20056 KB |
Output is correct |
34 |
Correct |
10 ms |
19800 KB |
Output is correct |
35 |
Correct |
10 ms |
19804 KB |
Output is correct |
36 |
Correct |
11 ms |
19712 KB |
Output is correct |
37 |
Correct |
10 ms |
19780 KB |
Output is correct |
38 |
Correct |
10 ms |
20032 KB |
Output is correct |
39 |
Correct |
322 ms |
48024 KB |
Output is correct |
40 |
Correct |
280 ms |
47440 KB |
Output is correct |
41 |
Correct |
279 ms |
47928 KB |
Output is correct |
42 |
Correct |
288 ms |
48576 KB |
Output is correct |
43 |
Correct |
275 ms |
48468 KB |
Output is correct |
44 |
Correct |
266 ms |
48320 KB |
Output is correct |
45 |
Correct |
399 ms |
52908 KB |
Output is correct |
46 |
Correct |
339 ms |
56992 KB |
Output is correct |
47 |
Correct |
220 ms |
53328 KB |
Output is correct |
48 |
Correct |
182 ms |
56404 KB |
Output is correct |
49 |
Correct |
300 ms |
48968 KB |
Output is correct |
50 |
Correct |
190 ms |
49228 KB |
Output is correct |
51 |
Correct |
274 ms |
59476 KB |
Output is correct |