#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1e9+10;
const int inf = 1e9;
const ll mod = 1e9+7 ;
void GG(){cout<<"0\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b,mo) % mo;
}
const int maxn = 2e5+5;
vector<int> g[maxn];
int sz[maxn], L[maxn], R[maxn], par[maxn], dep[maxn];
vector<int> children[maxn]; // including self
int IT = 0;
int lca[4005][4005];
void dfsqua(int v, int p) {
par[v] = p;
sz[v] = 1;
L[v] = R[v] = IT++;
children[v].pb(v);
for (int u : g[v]) {
if (u != p) {
dep[u] = dep[v] + 1;
dfsqua(u,v);
sz[v] += sz[u];
R[v] = R[u];
for (int c : children[u]) {
children[v].pb(c);
}
}
}
}
void dfs(int v, int p) {
par[v] = p;
sz[v] = 1;
L[v] = R[v] = IT++;
for (int u : g[v]) {
if (u != p) {
dep[u] = dep[v] + 1;
dfs(u,v);
sz[v] += sz[u];
R[v] = R[u];
}
}
}
vector<int> ord;
bool alive[maxn];
struct S{
int a, b, ab, bc, abc;
}s[400005 * 4];
S pull(S x, S y){
S r;
r.a=max(x.a, y.a);
r.b=max(x.b, y.b);
r.ab=max(x.ab,y.ab);
r.bc=max(x.bc,y.bc);
r.abc=max(x.abc,y.abc);
MX(r.ab, x.a+y.b);
MX(r.bc, x.b+y.a);
MX(r.abc, x.ab+y.a);
MX(r.abc, x.a+y.bc);
return r;
}
void MO(int p, bool on, int o=1, int l=0, int r=SZ(ord)-1) {
if (l > p || r < p) return;
if (l == r) {
int a = ord[l];
if (on) {
s[o] = {
dep[a],
dep[a]*-2,
-dep[a],
-dep[a],
0
};
}else{
s[o] = {
-inf,
dep[a]*-2,
-inf,
-inf,
-inf
};
}
return;
}
int mid = l+r>>1;
MO(p,on, o*2,l,mid);
MO(p,on, o*2+1,mid+1,r);
s[o] = pull(s[o*2], s[o*2+1]);
}
void dfs2(int v, int p) {
L[v] = R[v] = SZ(ord);
ord.pb(v);
par[v] = p;
sz[v] = 1;
for (int u : g[v]) {
if (u != p) {
dep[u] = dep[v] + 1;
dfs2(u,v);
R[v] = SZ(ord);
ord.pb(v);
sz[v] += sz[u];
}
}
}
bool isAnc(int a, int b) {
// is a b's ancestor?
return L[a] <= L[b] && R[a] >= R[b];
}
int n;
vector<int> quadratic() {
dfsqua(0, -1);
REP(i,n) {
for (int c : children[i]) {
lca[i][c] = lca[c][i] = i;
}
for (int c : g[i]) {
if (c != par[i]) {
for (int c2 : g[i]) {
if (c2 > c && c2 != par[i]) {
for (int v1 : children[c]) for (int v2 : children[c2]) {
lca[v1][v2] = lca[v2][v1] = i;
}
}
}
}
}
}
vector<int> dst(n); // max accommodation for this length
auto go = [&](int a, int b, int s1, int s2) {
bug(a,b,s1,s2);
bug(dep[a] + dep[b] - dep[lca[a][b]]*2);
MX(dst[dep[a] + dep[b] - dep[lca[a][b]]*2], min(s1, s2) * 2);
};
REP(a,n) for (int j : g[a]) if (j != par[a]) {
for (int b : children[j]) {
go(a,b,n-sz[j],sz[b]);
}
}
REP(i,n) REP(j,i) {
int a = i, b = j;
if (dep[a] > dep[b]) swap(a,b);
int s1, s2;
if (isAnc(a,b)) {
continue;
}
s1 = sz[a];
s2 = sz[b];
go(a,b,s1,s2);
// bug(a,b,lca[a][b], s1, s2);
// bug(dep[a] + dep[b] - dep[lca[a][b]] * 2);
}
for (int i = n-2; i>=0; --i) {
MX(dst[i], dst[i+1]);
}
vector<int> ret(n+1);
int j = 0;
for (int i = n; i>=1; --i) {
if (i % 2 == 1) ret[i] = 1;
else{
while (j+1 < n && dst[j+1] >= i) {
++j;
}
ret[i] = j+1;
bug(i, ret[i]);
}
}
return ret;
}
int cen(int v) {
for (int u : g[v]) {
if (u!=par[v] && sz[u] >= (n+1)/2) {
return cen(u);
}
}
return v;
}
//int csz[maxn]; // centroid based sizes
vector<int> who[maxn];
vector<int> fast(){
dfs(0,-1);
int rt = cen(0), rt2 = -1;
if (sz[rt] == n-sz[rt]) {
rt2 = par[rt];
}
bug(rt, rt2);
dep[rt] = 0;
dfs2(rt, rt); // now everything is relative to the new root
REP(i,n) {
if (i != rt)
who[sz[i]].pb(i);
}
vector<int> ret(n+1);
#ifdef BALBIT
for (int x : ord) cout<<x<<' ';
cout<<endl;
#endif
REP(i, SZ(ord)) {
MO(i, 0);
}
REP(i,n) {
MO(L[i], 1);
}
for (int i = 1; i<=n; ++i ) {
if (i & 1) {
ret[i] = 1;
}else{
bug(i, s[1].abc+1);
ret[i] = s[1].abc+1;
for (int x : who[i/2]) {
MO(L[x], 0);
}
}
}
for (int t : g[rt]) {
bug(t, sz[t]);
MX(ret[sz[t] * 2], 2);
}
return ret;
}
signed main(){
IOS();
cin>>n;
REP(i,n-1) {
int a,b; cin>>a>>b; --a; --b;
g[a].pb(b); g[b].pb(a);
}
vector<int> yay = fast();
REP1(i,n) {
cout<<yay[i]<<endl;
}
}
Compilation message
meetings2.cpp: In function 'void MO(int, bool, int, int, int)':
meetings2.cpp:138:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
138 | int mid = l+r>>1;
| ~^~
meetings2.cpp: In function 'std::vector<int> fast()':
meetings2.cpp:250:22: warning: variable 'rt2' set but not used [-Wunused-but-set-variable]
250 | int rt = cen(0), rt2 = -1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14340 KB |
Output is correct |
3 |
Correct |
8 ms |
14340 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
9 ms |
14432 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14448 KB |
Output is correct |
13 |
Correct |
9 ms |
14328 KB |
Output is correct |
14 |
Correct |
9 ms |
14412 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
9 ms |
14448 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14340 KB |
Output is correct |
3 |
Correct |
8 ms |
14340 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
9 ms |
14432 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14448 KB |
Output is correct |
13 |
Correct |
9 ms |
14328 KB |
Output is correct |
14 |
Correct |
9 ms |
14412 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
9 ms |
14448 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14412 KB |
Output is correct |
22 |
Correct |
16 ms |
15052 KB |
Output is correct |
23 |
Correct |
17 ms |
15036 KB |
Output is correct |
24 |
Correct |
17 ms |
15052 KB |
Output is correct |
25 |
Correct |
16 ms |
15052 KB |
Output is correct |
26 |
Correct |
17 ms |
15052 KB |
Output is correct |
27 |
Correct |
17 ms |
15064 KB |
Output is correct |
28 |
Correct |
16 ms |
15040 KB |
Output is correct |
29 |
Correct |
17 ms |
14948 KB |
Output is correct |
30 |
Correct |
17 ms |
15000 KB |
Output is correct |
31 |
Correct |
16 ms |
14984 KB |
Output is correct |
32 |
Correct |
17 ms |
15180 KB |
Output is correct |
33 |
Correct |
18 ms |
15232 KB |
Output is correct |
34 |
Correct |
18 ms |
15056 KB |
Output is correct |
35 |
Correct |
16 ms |
15064 KB |
Output is correct |
36 |
Correct |
16 ms |
14976 KB |
Output is correct |
37 |
Correct |
16 ms |
14960 KB |
Output is correct |
38 |
Correct |
17 ms |
15052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14340 KB |
Output is correct |
3 |
Correct |
8 ms |
14340 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
9 ms |
14432 KB |
Output is correct |
6 |
Correct |
9 ms |
14412 KB |
Output is correct |
7 |
Correct |
10 ms |
14412 KB |
Output is correct |
8 |
Correct |
9 ms |
14412 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14412 KB |
Output is correct |
11 |
Correct |
8 ms |
14412 KB |
Output is correct |
12 |
Correct |
9 ms |
14448 KB |
Output is correct |
13 |
Correct |
9 ms |
14328 KB |
Output is correct |
14 |
Correct |
9 ms |
14412 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
9 ms |
14412 KB |
Output is correct |
17 |
Correct |
9 ms |
14448 KB |
Output is correct |
18 |
Correct |
9 ms |
14412 KB |
Output is correct |
19 |
Correct |
9 ms |
14412 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14412 KB |
Output is correct |
22 |
Correct |
16 ms |
15052 KB |
Output is correct |
23 |
Correct |
17 ms |
15036 KB |
Output is correct |
24 |
Correct |
17 ms |
15052 KB |
Output is correct |
25 |
Correct |
16 ms |
15052 KB |
Output is correct |
26 |
Correct |
17 ms |
15052 KB |
Output is correct |
27 |
Correct |
17 ms |
15064 KB |
Output is correct |
28 |
Correct |
16 ms |
15040 KB |
Output is correct |
29 |
Correct |
17 ms |
14948 KB |
Output is correct |
30 |
Correct |
17 ms |
15000 KB |
Output is correct |
31 |
Correct |
16 ms |
14984 KB |
Output is correct |
32 |
Correct |
17 ms |
15180 KB |
Output is correct |
33 |
Correct |
18 ms |
15232 KB |
Output is correct |
34 |
Correct |
18 ms |
15056 KB |
Output is correct |
35 |
Correct |
16 ms |
15064 KB |
Output is correct |
36 |
Correct |
16 ms |
14976 KB |
Output is correct |
37 |
Correct |
16 ms |
14960 KB |
Output is correct |
38 |
Correct |
17 ms |
15052 KB |
Output is correct |
39 |
Correct |
709 ms |
49192 KB |
Output is correct |
40 |
Correct |
692 ms |
51144 KB |
Output is correct |
41 |
Correct |
708 ms |
51792 KB |
Output is correct |
42 |
Correct |
709 ms |
51836 KB |
Output is correct |
43 |
Correct |
733 ms |
51896 KB |
Output is correct |
44 |
Correct |
722 ms |
51896 KB |
Output is correct |
45 |
Correct |
757 ms |
58264 KB |
Output is correct |
46 |
Correct |
642 ms |
62628 KB |
Output is correct |
47 |
Correct |
686 ms |
52612 KB |
Output is correct |
48 |
Correct |
677 ms |
52100 KB |
Output is correct |
49 |
Correct |
683 ms |
52348 KB |
Output is correct |
50 |
Correct |
665 ms |
52240 KB |
Output is correct |
51 |
Correct |
647 ms |
60552 KB |
Output is correct |