답안 #1011644

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1011644 2024-07-01T02:45:04 Z biximo Meetings 2 (JOI21_meetings2) C++17
100 / 100
3140 ms 43376 KB
#include <bits/stdc++.h>
#define N 200005
#define INF 1000000000
using namespace std;
int n, sz[N], dpt[N], tb[N][18];
vector<int> adj[N];
bool vs[N];
void size_DFS(int c, int p) {
	sz[c] = 1;
	for(int i = 1; i < 18; i ++) {
		tb[c][i] = tb[tb[c][i-1]][i-1];
	}
	for(int i: adj[c]) {
		if(i == p) continue;
		dpt[i] = dpt[c]+1;
		tb[i][0] = c;
		size_DFS(i, c);
		sz[c] += sz[i];
	}
}
int jump(int a, int ds) {
	for(int i = 0; i < 18; i ++) {
		if(ds&1<<i) a = tb[a][i];
	}
	return a;
}
int LCA(int a, int b) {
	if(dpt[a] != dpt[b]) {
		if(dpt[a] < dpt[b]) swap(a,b);
		a = jump(a, dpt[a]-dpt[b]);
	}
	if(a == b) return a;
	for(int i = 17; i >= 0; i --) {
		if(tb[a][i] != tb[b][i]) {
			a = tb[a][i];
			b = tb[b][i];
		}
	}
	return tb[a][0];
}
int getSize(int c, int rt) {
	int lca = LCA(c,rt);
	if(lca != c) {
		return sz[c];
	} else {
		assert(dpt[rt] > dpt[c]);
		return sz[1]-sz[jump(rt,dpt[rt]-dpt[c]-1)];
	}
}
int sub_sz[N];
void size_2_DFS(int c, int p) {
	sub_sz[c] = 1;
	for(int i: adj[c]) {
		if(vs[i] || i == p) continue;
		size_2_DFS(i, c);
		sub_sz[c] += sub_sz[i];
	}
}
int find_cent_DFS(int c, int p, int tree_sz) {
	for(int i: adj[c]) {
		if(i == p || vs[i] || sub_sz[i]*2<tree_sz) continue;
		return find_cent_DFS(i, c, tree_sz);
	}
	return c;
}
struct Segtree {
	vector<int> tree;
	int len;
	void init(int n) {
		len = 1<<18;
		tree = vector<int>(len*2,-INF);
	}
	void update(int a, int v) {
		tree[a+len] = max(tree[a+len],v);
		for(a = (a+len)>>1; a >= 1; a >>= 1) {
			tree[a] = max(tree[a*2],tree[a*2+1]);
		}
	}
	void erase(int a) {
		tree[a+len] = -INF;
		for(a = (a+len)>>1; a >= 1; a >>= 1) {
			tree[a] = max(tree[a*2],tree[a*2+1]);
		}
	}
	int query(int a, int b) {
		int res = -INF;
		for(a += len, b += len; a <= b; a >>= 1, b >>= 1) {
			if(a&1) res = max(res, tree[a++]);
			if(~b&1) res = max(res, tree[b--]);
		}
		return res;
	}
	Segtree() {}
} tree;
int ans[N];
void ans_DFS(int c, int dt, int p, int rt) {
	for(int i: adj[c]) {
		if(vs[i] || i == p) continue;
		ans_DFS(i, dt+1, c, rt);
	}
	if(c == rt) return;
	int csz = getSize(c, rt);
	ans[csz] = max(ans[csz], dt+tree.query(csz,tree.len-1)+1);
	int rsz = getSize(rt, c);
	ans[min(rsz,csz)] = max(ans[min(rsz,csz)], dt+1);
}
void upd_DFS(int c, int dt, int p, int rt) {
	for(int i: adj[c]) {
		if(vs[i] || i == p) continue;
		upd_DFS(i, dt+1, c, rt);
	}
	if(c == rt) return;
	int csz = getSize(c, rt);
	tree.update(csz, dt);
}
void ers_DFS(int c, int dt, int p, int rt) {
	for(int i: adj[c]) {
		if(vs[i] || i == p) continue;
		ers_DFS(i, dt+1, c, rt);
	}
	if(c == rt) return;
	int csz = getSize(c, rt);
	tree.erase(csz);
}
void centroid_DFS(int c) {
	size_2_DFS(c, -1);
	int cent = find_cent_DFS(c, -1, sub_sz[c]);
	vs[cent] = 1;
	for(int j = 0; j < 2; j ++) {
		for(int i: adj[cent]) {
			if(vs[i]) continue;
			ans_DFS(i, 1, cent, cent);
			upd_DFS(i, 1, cent, cent);
		}
		for(int i: adj[cent]) {
			if(vs[i]) continue;
			ers_DFS(i, 1, cent, cent);
		}
		reverse(adj[cent].begin(),adj[cent].end());
	}
	for(int i: adj[cent]) {
		if(vs[i]) continue;
		centroid_DFS(i);
	}
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    tree.init(n);
    for(int i = 1, u, v; i < n; i ++) {
    	cin >> u >> v;
    	adj[u].push_back(v);
    	adj[v].push_back(u);
    }
    size_DFS(1,-1);
    centroid_DFS(1);
    for(int i = n/2; i >= 1; i --) {
    	ans[i] = max({ans[i], ans[i+1], 1});
    }
    for(int i = 1; i <= n; i ++) {
    	if(i%2) cout << "1\n";
    	else cout << ans[i/2] << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10684 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10684 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 2 ms 10584 KB Output is correct
17 Correct 2 ms 10584 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10684 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10684 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 2 ms 10584 KB Output is correct
17 Correct 2 ms 10584 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 17 ms 10932 KB Output is correct
23 Correct 16 ms 10844 KB Output is correct
24 Correct 18 ms 10924 KB Output is correct
25 Correct 17 ms 10844 KB Output is correct
26 Correct 15 ms 10840 KB Output is correct
27 Correct 14 ms 10776 KB Output is correct
28 Correct 16 ms 10844 KB Output is correct
29 Correct 16 ms 10844 KB Output is correct
30 Correct 15 ms 10924 KB Output is correct
31 Correct 15 ms 10844 KB Output is correct
32 Correct 31 ms 10936 KB Output is correct
33 Correct 32 ms 11100 KB Output is correct
34 Correct 6 ms 10840 KB Output is correct
35 Correct 6 ms 10844 KB Output is correct
36 Correct 19 ms 10844 KB Output is correct
37 Correct 7 ms 10844 KB Output is correct
38 Correct 18 ms 11028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10584 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 2 ms 10684 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10684 KB Output is correct
6 Correct 2 ms 10588 KB Output is correct
7 Correct 2 ms 10588 KB Output is correct
8 Correct 2 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 2 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 2 ms 10584 KB Output is correct
17 Correct 2 ms 10584 KB Output is correct
18 Correct 2 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 2 ms 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 17 ms 10932 KB Output is correct
23 Correct 16 ms 10844 KB Output is correct
24 Correct 18 ms 10924 KB Output is correct
25 Correct 17 ms 10844 KB Output is correct
26 Correct 15 ms 10840 KB Output is correct
27 Correct 14 ms 10776 KB Output is correct
28 Correct 16 ms 10844 KB Output is correct
29 Correct 16 ms 10844 KB Output is correct
30 Correct 15 ms 10924 KB Output is correct
31 Correct 15 ms 10844 KB Output is correct
32 Correct 31 ms 10936 KB Output is correct
33 Correct 32 ms 11100 KB Output is correct
34 Correct 6 ms 10840 KB Output is correct
35 Correct 6 ms 10844 KB Output is correct
36 Correct 19 ms 10844 KB Output is correct
37 Correct 7 ms 10844 KB Output is correct
38 Correct 18 ms 11028 KB Output is correct
39 Correct 1323 ms 31024 KB Output is correct
40 Correct 1250 ms 30388 KB Output is correct
41 Correct 1287 ms 30900 KB Output is correct
42 Correct 1229 ms 31312 KB Output is correct
43 Correct 1428 ms 31436 KB Output is correct
44 Correct 1160 ms 31268 KB Output is correct
45 Correct 2953 ms 37276 KB Output is correct
46 Correct 3140 ms 42072 KB Output is correct
47 Correct 202 ms 31688 KB Output is correct
48 Correct 185 ms 34336 KB Output is correct
49 Correct 1728 ms 34620 KB Output is correct
50 Correct 423 ms 34500 KB Output is correct
51 Correct 1778 ms 43376 KB Output is correct