#include <bits/stdc++.h>
#define MAXN 200007
#define MAXL 20
using namespace std;
vector<int> g[MAXN],v[MAXN];
int p[MAXL][MAXN],d[MAXN],n,ans[MAXN],c,in[MAXN],out[MAXN],t;
int dfsc(int s,int f,int dub)
{
p[0][s]=f;
in[s]=t++;
d[s]=dub;
bool centroid=true;
int sum=1;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f)
{
int a=dfsc(g[s][i],s,dub+1);
if(a>n/2) centroid=false;
sum+=a;
}
if(n-sum>n/2) centroid=false;
if(centroid) c=s;
out[s]=t++;
return sum;
}
bool insub(int u,int v) {return in[u]>=in[v] && out[u]<=out[v];}
int lca(int u,int v)
{
if(insub(u,v)) return v;
if(insub(v,u)) return u;
for(int i=MAXL-1;i>=0;i--) if(!insub(v,p[i][u])) u=p[i][u];
return p[0][u];
}
int dist(int u,int v) {return d[u]+d[v]-2*d[lca(u,v)];}
int dfs(int s,int f)
{
int sum=1;
for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) sum+=dfs(g[s][i],s);
v[sum].push_back(s);
return sum;
}
int main()
{
cin>>n;
for(int i=0;i<n-1;i++)
{
int t1,t2;
cin>>t1>>t2;
g[t1].push_back(t2);
g[t2].push_back(t1);
}
dfsc(1,1,0);
for(int i=1;i<MAXL;i++) for(int j=1;j<=n;j++) p[i][j]=p[i-1][p[i-1][j]];
dfs(c,c);
int d1=c,d2=c;
for(int i=n/2;i>=1;i--)
{
for(int j=0;j<v[i].size();j++)
{
int ds1=dist(v[i][j],d1),ds2=dist(v[i][j],d2),dsp=dist(d1,d2);
if(ds1>=ds2 && ds1>dsp) d2=v[i][j];
if(ds2>ds1 && ds2>dsp) d1=v[i][j];
}
ans[2*i]=dist(d1,d2);
}
for(int i=1;i<=n;i++) cout<<ans[i]+1<<endl;
}
Compilation message
meetings2.cpp: In function 'int dfsc(int, int, int)':
meetings2.cpp:14:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int i=0;i<g[s].size();i++) if(g[s][i]!=f)
| ~^~~~~~~~~~~~
meetings2.cpp: In function 'int dfs(int, int)':
meetings2.cpp:37:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i=0;i<g[s].size();i++) if(g[s][i]!=f) sum+=dfs(g[s][i],s);
| ~^~~~~~~~~~~~
meetings2.cpp: In function 'int main()':
meetings2.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int j=0;j<v[i].size();j++)
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
7 ms |
9696 KB |
Output is correct |
3 |
Correct |
6 ms |
9688 KB |
Output is correct |
4 |
Correct |
6 ms |
9824 KB |
Output is correct |
5 |
Correct |
6 ms |
9716 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9824 KB |
Output is correct |
8 |
Correct |
6 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9804 KB |
Output is correct |
11 |
Correct |
6 ms |
9804 KB |
Output is correct |
12 |
Correct |
7 ms |
9804 KB |
Output is correct |
13 |
Correct |
7 ms |
9804 KB |
Output is correct |
14 |
Correct |
7 ms |
9804 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
6 ms |
9792 KB |
Output is correct |
17 |
Correct |
6 ms |
9804 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
6 ms |
9804 KB |
Output is correct |
20 |
Correct |
7 ms |
9804 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
7 ms |
9696 KB |
Output is correct |
3 |
Correct |
6 ms |
9688 KB |
Output is correct |
4 |
Correct |
6 ms |
9824 KB |
Output is correct |
5 |
Correct |
6 ms |
9716 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9824 KB |
Output is correct |
8 |
Correct |
6 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9804 KB |
Output is correct |
11 |
Correct |
6 ms |
9804 KB |
Output is correct |
12 |
Correct |
7 ms |
9804 KB |
Output is correct |
13 |
Correct |
7 ms |
9804 KB |
Output is correct |
14 |
Correct |
7 ms |
9804 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
6 ms |
9792 KB |
Output is correct |
17 |
Correct |
6 ms |
9804 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
6 ms |
9804 KB |
Output is correct |
20 |
Correct |
7 ms |
9804 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
22 |
Correct |
17 ms |
10316 KB |
Output is correct |
23 |
Correct |
19 ms |
10396 KB |
Output is correct |
24 |
Correct |
18 ms |
10444 KB |
Output is correct |
25 |
Correct |
17 ms |
10296 KB |
Output is correct |
26 |
Correct |
17 ms |
10316 KB |
Output is correct |
27 |
Correct |
18 ms |
10348 KB |
Output is correct |
28 |
Correct |
17 ms |
10316 KB |
Output is correct |
29 |
Correct |
17 ms |
10352 KB |
Output is correct |
30 |
Correct |
17 ms |
10392 KB |
Output is correct |
31 |
Correct |
22 ms |
10340 KB |
Output is correct |
32 |
Correct |
21 ms |
10552 KB |
Output is correct |
33 |
Correct |
17 ms |
10572 KB |
Output is correct |
34 |
Correct |
19 ms |
10360 KB |
Output is correct |
35 |
Correct |
17 ms |
10352 KB |
Output is correct |
36 |
Correct |
18 ms |
10416 KB |
Output is correct |
37 |
Correct |
18 ms |
10316 KB |
Output is correct |
38 |
Correct |
17 ms |
10444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9804 KB |
Output is correct |
2 |
Correct |
7 ms |
9696 KB |
Output is correct |
3 |
Correct |
6 ms |
9688 KB |
Output is correct |
4 |
Correct |
6 ms |
9824 KB |
Output is correct |
5 |
Correct |
6 ms |
9716 KB |
Output is correct |
6 |
Correct |
6 ms |
9804 KB |
Output is correct |
7 |
Correct |
6 ms |
9824 KB |
Output is correct |
8 |
Correct |
6 ms |
9804 KB |
Output is correct |
9 |
Correct |
6 ms |
9804 KB |
Output is correct |
10 |
Correct |
6 ms |
9804 KB |
Output is correct |
11 |
Correct |
6 ms |
9804 KB |
Output is correct |
12 |
Correct |
7 ms |
9804 KB |
Output is correct |
13 |
Correct |
7 ms |
9804 KB |
Output is correct |
14 |
Correct |
7 ms |
9804 KB |
Output is correct |
15 |
Correct |
6 ms |
9804 KB |
Output is correct |
16 |
Correct |
6 ms |
9792 KB |
Output is correct |
17 |
Correct |
6 ms |
9804 KB |
Output is correct |
18 |
Correct |
6 ms |
9804 KB |
Output is correct |
19 |
Correct |
6 ms |
9804 KB |
Output is correct |
20 |
Correct |
7 ms |
9804 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
22 |
Correct |
17 ms |
10316 KB |
Output is correct |
23 |
Correct |
19 ms |
10396 KB |
Output is correct |
24 |
Correct |
18 ms |
10444 KB |
Output is correct |
25 |
Correct |
17 ms |
10296 KB |
Output is correct |
26 |
Correct |
17 ms |
10316 KB |
Output is correct |
27 |
Correct |
18 ms |
10348 KB |
Output is correct |
28 |
Correct |
17 ms |
10316 KB |
Output is correct |
29 |
Correct |
17 ms |
10352 KB |
Output is correct |
30 |
Correct |
17 ms |
10392 KB |
Output is correct |
31 |
Correct |
22 ms |
10340 KB |
Output is correct |
32 |
Correct |
21 ms |
10552 KB |
Output is correct |
33 |
Correct |
17 ms |
10572 KB |
Output is correct |
34 |
Correct |
19 ms |
10360 KB |
Output is correct |
35 |
Correct |
17 ms |
10352 KB |
Output is correct |
36 |
Correct |
18 ms |
10416 KB |
Output is correct |
37 |
Correct |
18 ms |
10316 KB |
Output is correct |
38 |
Correct |
17 ms |
10444 KB |
Output is correct |
39 |
Correct |
1014 ms |
38524 KB |
Output is correct |
40 |
Correct |
969 ms |
37812 KB |
Output is correct |
41 |
Correct |
1054 ms |
38592 KB |
Output is correct |
42 |
Correct |
1012 ms |
38872 KB |
Output is correct |
43 |
Correct |
1042 ms |
38872 KB |
Output is correct |
44 |
Correct |
1093 ms |
38972 KB |
Output is correct |
45 |
Correct |
1110 ms |
45444 KB |
Output is correct |
46 |
Correct |
840 ms |
49876 KB |
Output is correct |
47 |
Correct |
1042 ms |
39412 KB |
Output is correct |
48 |
Correct |
994 ms |
39216 KB |
Output is correct |
49 |
Correct |
889 ms |
39500 KB |
Output is correct |
50 |
Correct |
993 ms |
39436 KB |
Output is correct |
51 |
Correct |
828 ms |
47744 KB |
Output is correct |