답안 #396108

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396108 2021-04-29T13:04:01 Z Mlxa Meetings 2 (JOI21_meetings2) C++17
100 / 100
350 ms 128460 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define int ll

const int L = 19;
const int N = 1 << 19;
const int INF = 1e18;

int n;
vector<int> g[N];
int down[N], value[N];

int tl[N], tr[N], ti = 0;
int st[L][N];
int l2[N];
int hei[N];
int saved[N];

int argmin(int i, int j) {
	return tl[i] < tl[j] ? i : j;
}

void dfs(int v, int p) {
	down[v] = 1;
	tl[v] = ti;
	st[0][ti++] = v;
	vector<int> sons;
	for (int u : g[v]) {
		if (u == p) {
			continue;
		}
		hei[u] = hei[v] + 1;
		dfs(u, v);
		down[v] += down[u];
		sons.push_back(down[u]);
		st[0][ti++] = v;
	}
	sons.push_back(n - down[v]);
	value[v] = n - *max_element(all(sons));
	tr[v] = ti;
}

int lca(int v, int u) {
	int l = min(tl[v], tl[u]);
	int r = max(tl[v], tl[u]) + 1;
	int i = l2[r - l];
	return argmin(st[i][l], st[i][r - (1 << i)]);
}
int dist(int v, int u) {
	int l = lca(v, u);
	return hei[v] + hei[u] - 2 * hei[l];
}

int dv = -1, du = -1;

int add(int v) {
	// cout << "add " << v + 1 << endl;
	if (dv == -1) {
		dv = v;
		du = v;
		return 1;
	}
	if (dist(v, dv) < dist(v, du)) {
		swap(dv, du);
	}
	if (dist(v, dv) > dist(du, dv)) {
		du = v;
	}
	// cout << dv + 1 << " " << du + 1 << "\t" << dist(dv, du) + 1 << endl;
	return dist(dv, du) + 1;
}

signed main() {
#ifdef LC
	assert(freopen("input.txt", "r", stdin));
#endif
	ios::sync_with_stdio(0); cin.tie(0);
	
	cin >> n;
	for (int v, u, i = 0; i < n - 1; ++i) {
		cin >> v >> u;
		--v, --u;
		g[v].push_back(u);
		g[u].push_back(v);
	}
	dfs(0, 0);
	for (int i = 2; i < N; ++i) {
		l2[i] = l2[i / 2] + 1;
	}
	for (int i = 1; i < L; ++i) {
		for (int j = 0; j + (1 << i) <= N; ++j) {
			st[i][j] = argmin(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
		}
	}
	vector<pair<int, int>> order;
	for (int i = 0; i < n; ++i) {
		order.emplace_back(value[i], i);
	}
	sort(order.rbegin(), order.rend());
	for (auto e : order) {
		int cd = add(e.y);
		// cout << "upd " << e.x << " " << cd << endl;
		saved[e.x] = max(saved[e.x], cd);
	}
	for (int i = N - 2; i >= 0; --i) {
		saved[i] = max(saved[i], saved[i + 1]);
	}
	for (int i = 1; i <= n; ++i) {
		if (i % 2) {
			cout << "1\n";
		} else {
			cout << saved[i / 2] << "\n";
		}
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 90684 KB Output is correct
2 Correct 63 ms 90740 KB Output is correct
3 Correct 70 ms 90620 KB Output is correct
4 Correct 62 ms 90576 KB Output is correct
5 Correct 67 ms 90688 KB Output is correct
6 Correct 61 ms 90612 KB Output is correct
7 Correct 68 ms 90576 KB Output is correct
8 Correct 60 ms 90564 KB Output is correct
9 Correct 61 ms 90640 KB Output is correct
10 Correct 61 ms 90672 KB Output is correct
11 Correct 60 ms 90560 KB Output is correct
12 Correct 62 ms 90652 KB Output is correct
13 Correct 62 ms 90620 KB Output is correct
14 Correct 61 ms 90644 KB Output is correct
15 Correct 60 ms 90564 KB Output is correct
16 Correct 60 ms 90668 KB Output is correct
17 Correct 61 ms 90664 KB Output is correct
18 Correct 71 ms 90676 KB Output is correct
19 Correct 66 ms 90660 KB Output is correct
20 Correct 64 ms 90728 KB Output is correct
21 Correct 60 ms 90600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 90684 KB Output is correct
2 Correct 63 ms 90740 KB Output is correct
3 Correct 70 ms 90620 KB Output is correct
4 Correct 62 ms 90576 KB Output is correct
5 Correct 67 ms 90688 KB Output is correct
6 Correct 61 ms 90612 KB Output is correct
7 Correct 68 ms 90576 KB Output is correct
8 Correct 60 ms 90564 KB Output is correct
9 Correct 61 ms 90640 KB Output is correct
10 Correct 61 ms 90672 KB Output is correct
11 Correct 60 ms 90560 KB Output is correct
12 Correct 62 ms 90652 KB Output is correct
13 Correct 62 ms 90620 KB Output is correct
14 Correct 61 ms 90644 KB Output is correct
15 Correct 60 ms 90564 KB Output is correct
16 Correct 60 ms 90668 KB Output is correct
17 Correct 61 ms 90664 KB Output is correct
18 Correct 71 ms 90676 KB Output is correct
19 Correct 66 ms 90660 KB Output is correct
20 Correct 64 ms 90728 KB Output is correct
21 Correct 60 ms 90600 KB Output is correct
22 Correct 64 ms 91076 KB Output is correct
23 Correct 63 ms 91156 KB Output is correct
24 Correct 76 ms 91160 KB Output is correct
25 Correct 66 ms 91172 KB Output is correct
26 Correct 63 ms 91156 KB Output is correct
27 Correct 63 ms 91108 KB Output is correct
28 Correct 63 ms 91176 KB Output is correct
29 Correct 63 ms 91080 KB Output is correct
30 Correct 66 ms 91136 KB Output is correct
31 Correct 67 ms 91108 KB Output is correct
32 Correct 63 ms 91276 KB Output is correct
33 Correct 63 ms 91500 KB Output is correct
34 Correct 73 ms 91172 KB Output is correct
35 Correct 62 ms 91136 KB Output is correct
36 Correct 64 ms 91212 KB Output is correct
37 Correct 63 ms 91264 KB Output is correct
38 Correct 63 ms 91208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 90684 KB Output is correct
2 Correct 63 ms 90740 KB Output is correct
3 Correct 70 ms 90620 KB Output is correct
4 Correct 62 ms 90576 KB Output is correct
5 Correct 67 ms 90688 KB Output is correct
6 Correct 61 ms 90612 KB Output is correct
7 Correct 68 ms 90576 KB Output is correct
8 Correct 60 ms 90564 KB Output is correct
9 Correct 61 ms 90640 KB Output is correct
10 Correct 61 ms 90672 KB Output is correct
11 Correct 60 ms 90560 KB Output is correct
12 Correct 62 ms 90652 KB Output is correct
13 Correct 62 ms 90620 KB Output is correct
14 Correct 61 ms 90644 KB Output is correct
15 Correct 60 ms 90564 KB Output is correct
16 Correct 60 ms 90668 KB Output is correct
17 Correct 61 ms 90664 KB Output is correct
18 Correct 71 ms 90676 KB Output is correct
19 Correct 66 ms 90660 KB Output is correct
20 Correct 64 ms 90728 KB Output is correct
21 Correct 60 ms 90600 KB Output is correct
22 Correct 64 ms 91076 KB Output is correct
23 Correct 63 ms 91156 KB Output is correct
24 Correct 76 ms 91160 KB Output is correct
25 Correct 66 ms 91172 KB Output is correct
26 Correct 63 ms 91156 KB Output is correct
27 Correct 63 ms 91108 KB Output is correct
28 Correct 63 ms 91176 KB Output is correct
29 Correct 63 ms 91080 KB Output is correct
30 Correct 66 ms 91136 KB Output is correct
31 Correct 67 ms 91108 KB Output is correct
32 Correct 63 ms 91276 KB Output is correct
33 Correct 63 ms 91500 KB Output is correct
34 Correct 73 ms 91172 KB Output is correct
35 Correct 62 ms 91136 KB Output is correct
36 Correct 64 ms 91212 KB Output is correct
37 Correct 63 ms 91264 KB Output is correct
38 Correct 63 ms 91208 KB Output is correct
39 Correct 326 ms 112332 KB Output is correct
40 Correct 314 ms 114300 KB Output is correct
41 Correct 340 ms 114984 KB Output is correct
42 Correct 339 ms 115284 KB Output is correct
43 Correct 324 ms 115240 KB Output is correct
44 Correct 350 ms 115244 KB Output is correct
45 Correct 325 ms 122860 KB Output is correct
46 Correct 279 ms 128460 KB Output is correct
47 Correct 286 ms 115260 KB Output is correct
48 Correct 261 ms 115568 KB Output is correct
49 Correct 291 ms 115652 KB Output is correct
50 Correct 247 ms 115664 KB Output is correct
51 Correct 283 ms 127124 KB Output is correct