Submission #595722

# Submission time Handle Problem Language Result Execution time Memory
595722 2022-07-14T03:18:27 Z penguinhacker Meetings 2 (JOI21_meetings2) C++17
20 / 100
4000 ms 30268 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ar array
 
const int mxN=2e5;
int n, d[mxN], s[mxN], anc[mxN][18], ans[mxN+1], tin[mxN], t;
vector<int> adj[mxN];
set<ar<int, 2>> active;
 
void dfs(int u=0) {
	s[u]=1;
	tin[u]=t++;
	for (int i=1; i<18; ++i)
		anc[u][i]=anc[anc[u][i-1]][i-1];
	for (int v : adj[u])
		if (v!=anc[u][0]) {
			d[v]=d[u]+1, anc[v][0]=u;
			dfs(v);
			s[u]+=s[v];
		}
}
 
int lca(int u, int v) {
	if (d[u]>d[v])
		swap(u, v);
	for (int i=17; ~i; --i)
		if (d[v]-(1<<i)>=d[u])
			v=anc[v][i];
	if (u==v)
		return u;
	for (int i=17; ~i; --i)
		if (anc[u][i]!=anc[v][i])
			u=anc[u][i], v=anc[v][i];
	return anc[u][0];
}
 
int dist(int u, int v) {
	return d[u]+d[v]-2*d[lca(u, v)];
}
 
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i=1; i<n; ++i) {
		int u, v;
		cin >> u >> v, --u, --v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs();
	vector<ar<int, 2>> upd;
	for (int i=1; i<n; ++i)
		upd.push_back({s[i], i});
	sort(upd.begin(), upd.end());
	fill(ans, ans+n+1, 1);
	int best=1;
	for (int i=n/2; i; --i) {
		while(upd.size()&&upd.back()[0]>=i) {
			int u=upd.back()[1];
			upd.pop_back();
			auto it=active.lower_bound({tin[u]});
			if (it!=active.begin()) {
				--it;
				int v=(*it)[1];
				if (tin[u]+s[u]<=tin[v]+s[v])
					active.erase(it);
			}
			for (ar<int, 2> x : active)
				best=max(best, dist(u, x[1])+1);
			active.insert({tin[u], u});
		}
		for (ar<int, 2> x : active) {
			int u=x[1];
			if (n-s[u]>=i) {
				int v=u;
				for (int j=17; ~j; --j)
					if (n-s[anc[v][j]]>=i)
						v=anc[v][j];
				best=max(best, d[u]-d[v]+2);
			}
		}
		ans[2*i]=best;
	}
	for (int i=1; i<=n; ++i)
		cout << ans[i] << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 5024 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 5024 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 319 ms 5692 KB Output is correct
23 Correct 312 ms 5588 KB Output is correct
24 Correct 317 ms 5572 KB Output is correct
25 Correct 307 ms 5580 KB Output is correct
26 Correct 313 ms 5712 KB Output is correct
27 Correct 317 ms 5748 KB Output is correct
28 Correct 327 ms 5588 KB Output is correct
29 Correct 307 ms 5736 KB Output is correct
30 Correct 316 ms 5588 KB Output is correct
31 Correct 322 ms 5588 KB Output is correct
32 Correct 154 ms 5716 KB Output is correct
33 Correct 5 ms 5716 KB Output is correct
34 Correct 415 ms 5716 KB Output is correct
35 Correct 573 ms 5708 KB Output is correct
36 Correct 174 ms 5588 KB Output is correct
37 Correct 541 ms 5784 KB Output is correct
38 Correct 156 ms 5708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4972 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 2 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 5024 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 2 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 319 ms 5692 KB Output is correct
23 Correct 312 ms 5588 KB Output is correct
24 Correct 317 ms 5572 KB Output is correct
25 Correct 307 ms 5580 KB Output is correct
26 Correct 313 ms 5712 KB Output is correct
27 Correct 317 ms 5748 KB Output is correct
28 Correct 327 ms 5588 KB Output is correct
29 Correct 307 ms 5736 KB Output is correct
30 Correct 316 ms 5588 KB Output is correct
31 Correct 322 ms 5588 KB Output is correct
32 Correct 154 ms 5716 KB Output is correct
33 Correct 5 ms 5716 KB Output is correct
34 Correct 415 ms 5716 KB Output is correct
35 Correct 573 ms 5708 KB Output is correct
36 Correct 174 ms 5588 KB Output is correct
37 Correct 541 ms 5784 KB Output is correct
38 Correct 156 ms 5708 KB Output is correct
39 Execution timed out 4065 ms 30268 KB Time limit exceeded
40 Halted 0 ms 0 KB -