Submission #446637

#TimeUsernameProblemLanguageResultExecution timeMemory
446637ritul_kr_singhMeetings 2 (JOI21_meetings2)C++17
100 / 100
1347 ms23164 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
 
const int LIM = 2e5+2, INF = 1e9;

struct FenwickTree{
	vector<int> a; int n, s;
	FenwickTree(int N) : a((n=N)+1, -INF) {}
	void update(int i, int v){
		i = n - i;
		for(; i<=n; i+=i&-i) a[i] = max(a[i], v);
	}
	int operator()(int i){
		i = n - i, s = -INF;
		for(; i>=1; i-=i&-i) s = max(s, a[i]);
		return s;
	}
};
 
int n, ans[LIM], s[LIM], mxSz;
vector<int> g[LIM];
bool r[LIM], update;
FenwickTree f(0);
 
void init(int u){
	s[u] = 1;
	for(int v : g[u]) if(!s[v]) init(v), s[u] += s[v];
}
 
int find(int u){
	for(int v : g[u]) if(!r[v] && s[v] > s[u]/2){
		s[u] -= s[v], s[v] += s[u];
		return find(v);
	}
	return u;
}
 
void dfs(int u, int d){
	if(update) f.update(s[u], d);
	else ans[s[u]] = max(ans[s[u]], f(s[u]) + d);
	if(s[u] <= mxSz) ans[s[u]] = max(ans[s[u]], d);
 
	for(int v : g[u]) if(!r[v] && s[v] < s[u]) dfs(v, d+1);
}
 
void calc(int u){
	f = FenwickTree(s[u] + 1);

	for(int v : g[u]) if(!r[v]){
		ans[s[v]+1] = max(ans[s[v]+1], f(s[v]+1));
 
		mxSz = s[u] - s[v];
 
		update = 0;
		dfs(v, 1);
		update = 1;
		dfs(v, 1);
	}
}
 
void decompose(int u){
	r[u = find(u)] = 1;
	calc(u);
	reverse(g[u].begin(), g[u].end());
	calc(u);
 
	for(int v : g[u]) if(!r[v]) decompose(v);
}
 
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;
	for(int i=1; i<n; ++i){
		int u, v; cin >> u >> v;
		--u, --v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
 
	init(0);
	decompose(0);
 
	for(int i=n; --i; ) ans[i] = max(ans[i], ans[i+1]);
	for(int i=1; i<=n; ++i){
		if(i & 1) cout << 1 nl;
		else cout << ans[i/2]+1 nl;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...