#include <bits/stdc++.h>
using namespace std;
int par[200005][20], sz[200005], dep[200005], ans[200005], n, c, x, y, ind, res=1;
vector<pair<int, int> > vec;
vector<int> adj[200005];
void dfs(int u, int p)
{
sz[u]=1;
for (int v:adj[u])
{
if (v!=p)
{
dfs(v, u);
sz[u]+=sz[v];
}
}
bool ok=1;
for (int v:adj[u])
{
if (sz[v]>n/2)
{
ok=0;
break;
}
}
if (ok && n-sz[u]<=n/2)
c=x=y=u;
}
void dfs2(int u, int p)
{
par[u][0]=p;
for (int i=1; i<20; i++)
par[u][i]=par[par[u][i-1]][i-1];
sz[u]=1;
for (int v:adj[u])
{
if (v!=p)
{
dep[v]=dep[u]+1;
dfs2(v, u);
sz[u]+=sz[v];
}
}
}
int lca(int u, int v)
{
if (dep[u]<dep[v])
swap(u, v);
if (dep[u]>dep[v])
{
int diff=dep[u]-dep[v];
for (int i=0; i<20; i++)
if (diff&(1<<i))
u=par[u][i];
}
if (u==v)
return u;
for (int i=19; i>=0; i--)
if (par[u][i]!=par[v][i])
u=par[u][i], v=par[v][i];
return par[u][0];
}
int len(int u, int v)
{
return dep[u]+dep[v]-dep[lca(u, v)]*2;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1);
dfs2(c, c);
for (int i=1; i<=n; i++)
if (i!=c)
vec.push_back({min(sz[i], n-sz[i]+1), i});
sort(vec.begin(), vec.end(), greater<pair<int, int> >());
for (int i=n; i>=1; i--)
{
if (i&1)
{
ans[i]=1;
continue;
}
while (ind<vec.size() && vec[ind].first>=i/2)
{
int u=vec[ind].second;
int xy=len(x, y);
int ux=len(u, x);
int uy=len(u, y);
if (ux>=uy && ux>xy)
{
y=u;
res=ux+1;
}
else if (uy>ux && uy>xy)
{
x=u;
res=uy+1;
}
ind++;
}
ans[i]=res;
}
for (int i=1; i<=n; i++)
cout << ans[i] << '\n';
}
Compilation message
meetings2.cpp: In function 'int main()':
meetings2.cpp:93:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | while (ind<vec.size() && vec[ind].first>=i/2)
| ~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8628 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8628 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
4 ms |
9052 KB |
Output is correct |
23 |
Correct |
4 ms |
9052 KB |
Output is correct |
24 |
Correct |
4 ms |
9048 KB |
Output is correct |
25 |
Correct |
4 ms |
9052 KB |
Output is correct |
26 |
Correct |
4 ms |
8932 KB |
Output is correct |
27 |
Correct |
4 ms |
9052 KB |
Output is correct |
28 |
Correct |
4 ms |
9008 KB |
Output is correct |
29 |
Correct |
4 ms |
9052 KB |
Output is correct |
30 |
Correct |
4 ms |
9052 KB |
Output is correct |
31 |
Correct |
4 ms |
9052 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
4 ms |
9052 KB |
Output is correct |
34 |
Correct |
3 ms |
9052 KB |
Output is correct |
35 |
Correct |
3 ms |
9052 KB |
Output is correct |
36 |
Correct |
4 ms |
9052 KB |
Output is correct |
37 |
Correct |
3 ms |
8856 KB |
Output is correct |
38 |
Correct |
5 ms |
9308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8628 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8792 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
1 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
4 ms |
9052 KB |
Output is correct |
23 |
Correct |
4 ms |
9052 KB |
Output is correct |
24 |
Correct |
4 ms |
9048 KB |
Output is correct |
25 |
Correct |
4 ms |
9052 KB |
Output is correct |
26 |
Correct |
4 ms |
8932 KB |
Output is correct |
27 |
Correct |
4 ms |
9052 KB |
Output is correct |
28 |
Correct |
4 ms |
9008 KB |
Output is correct |
29 |
Correct |
4 ms |
9052 KB |
Output is correct |
30 |
Correct |
4 ms |
9052 KB |
Output is correct |
31 |
Correct |
4 ms |
9052 KB |
Output is correct |
32 |
Correct |
4 ms |
9052 KB |
Output is correct |
33 |
Correct |
4 ms |
9052 KB |
Output is correct |
34 |
Correct |
3 ms |
9052 KB |
Output is correct |
35 |
Correct |
3 ms |
9052 KB |
Output is correct |
36 |
Correct |
4 ms |
9052 KB |
Output is correct |
37 |
Correct |
3 ms |
8856 KB |
Output is correct |
38 |
Correct |
5 ms |
9308 KB |
Output is correct |
39 |
Correct |
186 ms |
33744 KB |
Output is correct |
40 |
Correct |
164 ms |
33108 KB |
Output is correct |
41 |
Correct |
166 ms |
33744 KB |
Output is correct |
42 |
Correct |
178 ms |
34256 KB |
Output is correct |
43 |
Correct |
203 ms |
34232 KB |
Output is correct |
44 |
Correct |
200 ms |
34128 KB |
Output is correct |
45 |
Correct |
266 ms |
39104 KB |
Output is correct |
46 |
Correct |
200 ms |
43160 KB |
Output is correct |
47 |
Correct |
159 ms |
34696 KB |
Output is correct |
48 |
Correct |
112 ms |
34812 KB |
Output is correct |
49 |
Correct |
180 ms |
34780 KB |
Output is correct |
50 |
Correct |
134 ms |
35076 KB |
Output is correct |
51 |
Correct |
165 ms |
42056 KB |
Output is correct |