Submission #869287

# Submission time Handle Problem Language Result Execution time Memory
869287 2023-11-04T01:18:37 Z NK_ Meetings 2 (JOI21_meetings2) C++17
100 / 100
393 ms 51828 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front
 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
 
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
 
using db = long double;
 
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
 
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 19;

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int N; cin >> N;

	V<vi> adj(N); 
	for(int i = 0; i < N-1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	vi sub(N), X(N, MOD);
	function<void(int, int)> prep = [&](int u, int p) {
		sub[u] = 1;
		for(auto& v : adj[u]) if (v != p) {
			prep(v, u);
			X[u] = min(X[u], N - sub[v]);
			sub[u] += sub[v];
		}

		X[u] = min(X[u], sub[u]);
		// cout << "X " << X[u] << " " << u << endl;
	};

	prep(0, -1);

	vi dep(N); V<vi> up(N, vi(LG));
	function<void(int, int)> gen = [&](int u, int p) {
		up[u][0] = p; dep[u] = (u == p ? 0 : dep[p] + 1);
		for(int i = 1; i < LG; i++) up[u][i] = up[up[u][i-1]][i-1];	

		for(auto& v : adj[u]) if (v != p) gen(v, u);
	};

	auto jmp = [&](int u, int d) {
		for(int i = 0; i < LG; i++) if ((d >> i) & 1) u = up[u][i];
		return u;
	};

	auto lca = [&](int a, int b) {
		if (dep[a] < dep[b]) swap(a, b);
		a = jmp(a, dep[a] - dep[b]);
		if (a == b) return a;
		for(int i = LG - 1; i >= 0; i--) {
			if (up[a][i] != up[b][i]) {
				a = up[a][i], b = up[b][i];
			}
		}
		return up[a][0];
	};	

	auto dist = [&](int a, int b) { return dep[a] + dep[b] - 2 * dep[lca(a, b)]; };

	gen(0, 0);

	vi U(N); iota(begin(U), end(U), 0);
	sort(begin(U), end(U), [&](int a, int b) {
		return X[a] > X[b];
	});	

	int a = U[0], b = U[0], d = 0;
	vi ans(N, 1);
	for(int i = N / 2, cur = 0; i >= 1; i--) {
		while(cur < N && X[U[cur]] >= i) {
			// add U[cur];
			int x = U[cur]; 
			// cerr << "ADD " << x << endl;
			int xa = dist(x, a), xb = dist(x, b);
			if (d < xa) b = x, d = xa; 
			else if (d < xb) a = x, d = xb;
			cur++;
		}
		// cerr << "=> " << d << endl;
		ans[2 * i - 1] = d + 1;
	}	

	for(int i = 0; i < N; i++) cout << ans[i] << nl;


	exit(0-0);
} 	 
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 460 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 4 ms 1232 KB Output is correct
24 Correct 4 ms 1268 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 3 ms 1116 KB Output is correct
27 Correct 2 ms 1116 KB Output is correct
28 Correct 3 ms 1116 KB Output is correct
29 Correct 3 ms 1252 KB Output is correct
30 Correct 3 ms 1116 KB Output is correct
31 Correct 3 ms 1116 KB Output is correct
32 Correct 3 ms 1372 KB Output is correct
33 Correct 3 ms 1372 KB Output is correct
34 Correct 2 ms 1116 KB Output is correct
35 Correct 2 ms 1116 KB Output is correct
36 Correct 3 ms 1368 KB Output is correct
37 Correct 3 ms 1116 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 456 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 460 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 4 ms 1232 KB Output is correct
24 Correct 4 ms 1268 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 3 ms 1116 KB Output is correct
27 Correct 2 ms 1116 KB Output is correct
28 Correct 3 ms 1116 KB Output is correct
29 Correct 3 ms 1252 KB Output is correct
30 Correct 3 ms 1116 KB Output is correct
31 Correct 3 ms 1116 KB Output is correct
32 Correct 3 ms 1372 KB Output is correct
33 Correct 3 ms 1372 KB Output is correct
34 Correct 2 ms 1116 KB Output is correct
35 Correct 2 ms 1116 KB Output is correct
36 Correct 3 ms 1368 KB Output is correct
37 Correct 3 ms 1116 KB Output is correct
38 Correct 3 ms 1372 KB Output is correct
39 Correct 218 ms 40056 KB Output is correct
40 Correct 227 ms 39424 KB Output is correct
41 Correct 223 ms 40152 KB Output is correct
42 Correct 233 ms 40788 KB Output is correct
43 Correct 217 ms 40792 KB Output is correct
44 Correct 260 ms 40796 KB Output is correct
45 Correct 393 ms 46532 KB Output is correct
46 Correct 316 ms 51828 KB Output is correct
47 Correct 163 ms 41084 KB Output is correct
48 Correct 148 ms 41416 KB Output is correct
49 Correct 244 ms 41664 KB Output is correct
50 Correct 146 ms 41412 KB Output is correct
51 Correct 205 ms 50376 KB Output is correct