#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
const int INF = 1e9+7;
int N;
vector<int> adj[MAXN+10], VV[MAXN+10];
int sz[MAXN+10], dep[MAXN+10], par[MAXN+10];
int ans[MAXN+10];
vector<int> dfn;
void dfs(int now, int bef, int d)
{
sz[now]=1;
dep[now]=d;
par[now]=bef;
VV[now].push_back(dfn.size());
dfn.push_back(now);
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now, d+1);
VV[now].push_back(dfn.size());
dfn.push_back(now);
sz[now]+=sz[nxt];
}
}
struct UF
{
int par[MAXN+10], minv[MAXN+10], maxv[MAXN+10];
void init()
{
for(int i=1; i<=N; i++)
{
par[i]=i;
maxv[i]=minv[i]=dep[i];
}
}
int Find(int x)
{
if(x==par[x]) return x;
return par[x]=Find(par[x]);
}
void Union(int x, int y)
{
x=Find(x); y=Find(y);
minv[x]=min(minv[x], minv[y]);
maxv[x]=max(maxv[x], maxv[y]);
par[y]=x;
}
}uf;
struct Node
{
int a, ab, aba, b, ba;
Node() : a(-INF), ab(-INF), aba(-INF), b(-INF), ba(-INF) {}
};
Node operator + (const Node &p, const Node &q)
{
Node ret;
ret.a=max(p.a, q.a);
ret.b=max(p.b, q.b);
ret.ab=max({p.ab, q.ab, p.a+q.b});
ret.ba=max({p.ba, q.ba, p.b+q.a});
ret.aba=max({p.aba, q.aba, p.a+q.ba, p.ab+q.a});
return ret;
}
struct SEG
{
Node tree[MAXN*8+10];
void update(int node, int tl, int tr, int p, int q)
{
if(tl==tr)
{
if(q==1) tree[node].a=dep[dfn[tl]];
else if(q==2) tree[node].a=-INF;
else tree[node].b=-2*dep[dfn[tl]];
return;
}
int mid=tl+tr>>1;
if(p<=mid) update(node*2, tl, mid, p, q);
else update(node*2+1, mid+1, tr, p, q);
tree[node]=tree[node*2]+tree[node*2+1];
}
int val() { return tree[1].aba; }
}seg;
int main()
{
scanf("%d", &N);
for(int i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1, 1);
vector<pii> V;
int t=0;
for(int i=2; i<=N; i++)
{
int p=sz[i], q=sz[par[i]];
V.push_back({min({p, N-p, q, N-q}), i});
t=max(t, min(p, N-p));
}
sort(V.begin(), V.end(), greater<pii>());
uf.init();
int val=1;
for(int i=N/2, j=0; i>=1; i--)
{
if(t>=i) val=max(val, 2);
for(; j<V.size() && V[j].first>=i; j++)
{
int u=V[j].second;
uf.Union(u, par[u]);
u=uf.Find(u);
val=max(val, uf.maxv[u]-uf.minv[u]+2);
}
ans[i]=val;
}
V.clear();
for(int i=1; i<=N; i++) V.push_back({sz[i], i});
sort(V.begin(), V.end(), greater<pii>());
for(int i=N/2, j=0; i>=1; i--)
{
for(; j<V.size() && V[j].first>=i; j++)
{
if(V[j].second!=1) seg.update(1, 0, dfn.size()-1, VV[par[V[j].second]].front(), 2);
for(auto it : VV[V[j].second]) seg.update(1, 0, dfn.size()-1, it, 0);
seg.update(1, 0, dfn.size()-1, VV[V[j].second].front(), 1);
}
ans[i]=max(ans[i], seg.val()+1);
//printf("!%d\n", seg.val());
}
for(int i=1; i<=N; i++)
{
if(i%2) printf("1\n");
else printf("%d\n", ans[i/2]);
}
}
Compilation message
meetings2.cpp: In member function 'void SEG::update(int, int, int, int, int)':
meetings2.cpp:90:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
90 | int mid=tl+tr>>1;
| ~~^~~
meetings2.cpp: In function 'int main()':
meetings2.cpp:128:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for(; j<V.size() && V[j].first>=i; j++)
| ~^~~~~~~~~
meetings2.cpp:144:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for(; j<V.size() && V[j].first>=i; j++)
| ~^~~~~~~~~
meetings2.cpp:100:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
meetings2.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
40916 KB |
Output is correct |
2 |
Correct |
20 ms |
40932 KB |
Output is correct |
3 |
Correct |
19 ms |
40968 KB |
Output is correct |
4 |
Correct |
22 ms |
40996 KB |
Output is correct |
5 |
Correct |
19 ms |
40996 KB |
Output is correct |
6 |
Correct |
21 ms |
41012 KB |
Output is correct |
7 |
Correct |
17 ms |
40996 KB |
Output is correct |
8 |
Correct |
21 ms |
41044 KB |
Output is correct |
9 |
Correct |
17 ms |
41044 KB |
Output is correct |
10 |
Correct |
18 ms |
41032 KB |
Output is correct |
11 |
Correct |
20 ms |
41024 KB |
Output is correct |
12 |
Correct |
18 ms |
40984 KB |
Output is correct |
13 |
Correct |
19 ms |
41044 KB |
Output is correct |
14 |
Correct |
18 ms |
40968 KB |
Output is correct |
15 |
Correct |
19 ms |
41044 KB |
Output is correct |
16 |
Correct |
22 ms |
41044 KB |
Output is correct |
17 |
Correct |
19 ms |
41040 KB |
Output is correct |
18 |
Correct |
20 ms |
40944 KB |
Output is correct |
19 |
Correct |
18 ms |
41044 KB |
Output is correct |
20 |
Correct |
19 ms |
41032 KB |
Output is correct |
21 |
Correct |
18 ms |
41044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
40916 KB |
Output is correct |
2 |
Correct |
20 ms |
40932 KB |
Output is correct |
3 |
Correct |
19 ms |
40968 KB |
Output is correct |
4 |
Correct |
22 ms |
40996 KB |
Output is correct |
5 |
Correct |
19 ms |
40996 KB |
Output is correct |
6 |
Correct |
21 ms |
41012 KB |
Output is correct |
7 |
Correct |
17 ms |
40996 KB |
Output is correct |
8 |
Correct |
21 ms |
41044 KB |
Output is correct |
9 |
Correct |
17 ms |
41044 KB |
Output is correct |
10 |
Correct |
18 ms |
41032 KB |
Output is correct |
11 |
Correct |
20 ms |
41024 KB |
Output is correct |
12 |
Correct |
18 ms |
40984 KB |
Output is correct |
13 |
Correct |
19 ms |
41044 KB |
Output is correct |
14 |
Correct |
18 ms |
40968 KB |
Output is correct |
15 |
Correct |
19 ms |
41044 KB |
Output is correct |
16 |
Correct |
22 ms |
41044 KB |
Output is correct |
17 |
Correct |
19 ms |
41040 KB |
Output is correct |
18 |
Correct |
20 ms |
40944 KB |
Output is correct |
19 |
Correct |
18 ms |
41044 KB |
Output is correct |
20 |
Correct |
19 ms |
41032 KB |
Output is correct |
21 |
Correct |
18 ms |
41044 KB |
Output is correct |
22 |
Correct |
26 ms |
41420 KB |
Output is correct |
23 |
Correct |
25 ms |
41428 KB |
Output is correct |
24 |
Correct |
25 ms |
41528 KB |
Output is correct |
25 |
Correct |
29 ms |
41420 KB |
Output is correct |
26 |
Correct |
30 ms |
41556 KB |
Output is correct |
27 |
Correct |
26 ms |
41472 KB |
Output is correct |
28 |
Correct |
25 ms |
41420 KB |
Output is correct |
29 |
Correct |
26 ms |
41428 KB |
Output is correct |
30 |
Correct |
25 ms |
41584 KB |
Output is correct |
31 |
Correct |
27 ms |
41456 KB |
Output is correct |
32 |
Correct |
25 ms |
41556 KB |
Output is correct |
33 |
Correct |
24 ms |
41712 KB |
Output is correct |
34 |
Correct |
25 ms |
41476 KB |
Output is correct |
35 |
Correct |
24 ms |
41468 KB |
Output is correct |
36 |
Correct |
27 ms |
41472 KB |
Output is correct |
37 |
Correct |
25 ms |
41484 KB |
Output is correct |
38 |
Correct |
24 ms |
41632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
40916 KB |
Output is correct |
2 |
Correct |
20 ms |
40932 KB |
Output is correct |
3 |
Correct |
19 ms |
40968 KB |
Output is correct |
4 |
Correct |
22 ms |
40996 KB |
Output is correct |
5 |
Correct |
19 ms |
40996 KB |
Output is correct |
6 |
Correct |
21 ms |
41012 KB |
Output is correct |
7 |
Correct |
17 ms |
40996 KB |
Output is correct |
8 |
Correct |
21 ms |
41044 KB |
Output is correct |
9 |
Correct |
17 ms |
41044 KB |
Output is correct |
10 |
Correct |
18 ms |
41032 KB |
Output is correct |
11 |
Correct |
20 ms |
41024 KB |
Output is correct |
12 |
Correct |
18 ms |
40984 KB |
Output is correct |
13 |
Correct |
19 ms |
41044 KB |
Output is correct |
14 |
Correct |
18 ms |
40968 KB |
Output is correct |
15 |
Correct |
19 ms |
41044 KB |
Output is correct |
16 |
Correct |
22 ms |
41044 KB |
Output is correct |
17 |
Correct |
19 ms |
41040 KB |
Output is correct |
18 |
Correct |
20 ms |
40944 KB |
Output is correct |
19 |
Correct |
18 ms |
41044 KB |
Output is correct |
20 |
Correct |
19 ms |
41032 KB |
Output is correct |
21 |
Correct |
18 ms |
41044 KB |
Output is correct |
22 |
Correct |
26 ms |
41420 KB |
Output is correct |
23 |
Correct |
25 ms |
41428 KB |
Output is correct |
24 |
Correct |
25 ms |
41528 KB |
Output is correct |
25 |
Correct |
29 ms |
41420 KB |
Output is correct |
26 |
Correct |
30 ms |
41556 KB |
Output is correct |
27 |
Correct |
26 ms |
41472 KB |
Output is correct |
28 |
Correct |
25 ms |
41420 KB |
Output is correct |
29 |
Correct |
26 ms |
41428 KB |
Output is correct |
30 |
Correct |
25 ms |
41584 KB |
Output is correct |
31 |
Correct |
27 ms |
41456 KB |
Output is correct |
32 |
Correct |
25 ms |
41556 KB |
Output is correct |
33 |
Correct |
24 ms |
41712 KB |
Output is correct |
34 |
Correct |
25 ms |
41476 KB |
Output is correct |
35 |
Correct |
24 ms |
41468 KB |
Output is correct |
36 |
Correct |
27 ms |
41472 KB |
Output is correct |
37 |
Correct |
25 ms |
41484 KB |
Output is correct |
38 |
Correct |
24 ms |
41632 KB |
Output is correct |
39 |
Correct |
690 ms |
64788 KB |
Output is correct |
40 |
Correct |
644 ms |
64312 KB |
Output is correct |
41 |
Correct |
655 ms |
64848 KB |
Output is correct |
42 |
Correct |
675 ms |
65328 KB |
Output is correct |
43 |
Correct |
669 ms |
65208 KB |
Output is correct |
44 |
Correct |
682 ms |
65208 KB |
Output is correct |
45 |
Correct |
582 ms |
70892 KB |
Output is correct |
46 |
Correct |
488 ms |
75640 KB |
Output is correct |
47 |
Correct |
552 ms |
65892 KB |
Output is correct |
48 |
Correct |
467 ms |
66276 KB |
Output is correct |
49 |
Correct |
568 ms |
66196 KB |
Output is correct |
50 |
Correct |
509 ms |
66392 KB |
Output is correct |
51 |
Correct |
478 ms |
74760 KB |
Output is correct |