답안 #800464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
800464 2023-08-01T15:08:50 Z acatmeowmeow Cat Exercise (JOI23_ho_t4) C++11
100 / 100
327 ms 126200 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long 

const int N = 2e5, KMAX = 20;
int n, h[N + 5], euler[2*N + 5], lg[2*N + 5], tin[N + 5], tout[N + 5], timer = 1, d[N + 5], table[2*N + 5][KMAX + 5];
vector<int> adj[N + 5];

void dfs1(int u, int e) {
	tin[u] = ++timer;
	euler[timer] = u;
	for (auto&v : adj[u]) {
		if (v == e) continue;
		d[v] = d[u] + 1;
		dfs1(v, u);
		euler[++timer] = u;
	}
	tout[u] = timer;
}

int combine(int x, int y) { return d[x] < d[y] ? x : y; }

void build(int n) {
	lg[1] = 0;
	for (int i = 2; i <= n; i++) lg[i] = lg[i/2] + 1;
	for (int i = 1; i <= n; i++) table[i][0] = euler[i];
	for (int j = 1; j <= KMAX; j++) {
		for (int i = 1; i + (1ll << j) - 1 <= n; i++) {
			table[i][j] = combine(table[i][j - 1], table[i + (1ll << (j - 1))][j - 1]);
		}
	}
}

int lca(int x, int y) {
	if (tin[x] > tin[y]) swap(x, y);
	int k = lg[tin[y] - tin[x] + 1], w = combine(table[tin[x]][k], table[tin[y] - (1ll << k) + 1][k]);
	return d[x] + d[y] - 2*d[w];
}

int par[N + 5], sz[N + 5], mx[N + 5], highest[N + 5], indx[N + 5];

int find(int x) { return x == par[x] ? x : find(par[x]); }

bool same(int x, int y) { return find(x) == find(y); }

int combine2(int x, int y) { 
	if (!x) return y;
	else if (!y) return x;
	else return h[x] > h[y] ? x : y; 
}

void unite(int x, int y) {
	x = find(x), y = find(y);
	if (sz[x] < sz[y]) swap(x, y);
	sz[x] += sz[y], par[y] = x;
   	mx[x] = max(mx[x], mx[y]), highest[x] = combine2(highest[x], highest[y]);
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> h[i];
	for (int i = 1; i < n; i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs1(1, 0);
	build(2*n);
	fill(sz + 1, sz + n + 1, 1ll);
	iota(par + 1, par + n + 1, 1ll);
	iota(highest + 1, highest + n + 1, 1ll);
	iota(indx + 1, indx + n + 1, 1ll);
	sort(indx + 1, indx + n + 1, [&](int a, int b) { return h[a] < h[b]; });
	for (int i = 1;i <= n; i++) {
		int index = indx[i];
		for (auto&v : adj[index]) {
			int root = find(v);
			if (h[highest[root]] > h[index]) continue;
			mx[index] = max(mx[index], mx[root] + lca(index, highest[root]));
		}
		for (auto&v : adj[index]) {
			int root = find(v);
			if (h[highest[root]] > h[index]) continue;
			if (!same(index, v)) unite(index, v);
		}
	}
	cout << *max_element(mx + 1, mx + n + 1) << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5032 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5032 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 2 ms 5204 KB Output is correct
14 Correct 2 ms 5204 KB Output is correct
15 Correct 2 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5032 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 2 ms 5204 KB Output is correct
14 Correct 2 ms 5204 KB Output is correct
15 Correct 2 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 7 ms 7996 KB Output is correct
19 Correct 6 ms 7996 KB Output is correct
20 Correct 7 ms 8072 KB Output is correct
21 Correct 6 ms 8020 KB Output is correct
22 Correct 7 ms 8008 KB Output is correct
23 Correct 7 ms 8016 KB Output is correct
24 Correct 8 ms 8124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5032 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 2 ms 5204 KB Output is correct
14 Correct 2 ms 5204 KB Output is correct
15 Correct 2 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 7 ms 7996 KB Output is correct
19 Correct 6 ms 7996 KB Output is correct
20 Correct 7 ms 8072 KB Output is correct
21 Correct 6 ms 8020 KB Output is correct
22 Correct 7 ms 8008 KB Output is correct
23 Correct 7 ms 8016 KB Output is correct
24 Correct 8 ms 8124 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 6 ms 7892 KB Output is correct
27 Correct 6 ms 7892 KB Output is correct
28 Correct 7 ms 7868 KB Output is correct
29 Correct 5 ms 8020 KB Output is correct
30 Correct 6 ms 7764 KB Output is correct
31 Correct 6 ms 7764 KB Output is correct
32 Correct 5 ms 7732 KB Output is correct
33 Correct 6 ms 7764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5032 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 2 ms 5204 KB Output is correct
14 Correct 2 ms 5204 KB Output is correct
15 Correct 2 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 7 ms 7996 KB Output is correct
19 Correct 6 ms 7996 KB Output is correct
20 Correct 7 ms 8072 KB Output is correct
21 Correct 6 ms 8020 KB Output is correct
22 Correct 7 ms 8008 KB Output is correct
23 Correct 7 ms 8016 KB Output is correct
24 Correct 8 ms 8124 KB Output is correct
25 Correct 193 ms 126180 KB Output is correct
26 Correct 215 ms 126160 KB Output is correct
27 Correct 206 ms 126132 KB Output is correct
28 Correct 240 ms 126180 KB Output is correct
29 Correct 237 ms 126196 KB Output is correct
30 Correct 255 ms 126176 KB Output is correct
31 Correct 242 ms 126200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5168 KB Output is correct
3 Correct 243 ms 115124 KB Output is correct
4 Correct 242 ms 115124 KB Output is correct
5 Correct 261 ms 115148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 2 ms 5076 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5076 KB Output is correct
6 Correct 3 ms 5032 KB Output is correct
7 Correct 2 ms 5076 KB Output is correct
8 Correct 2 ms 5032 KB Output is correct
9 Correct 2 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 2 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 2 ms 5204 KB Output is correct
14 Correct 2 ms 5204 KB Output is correct
15 Correct 2 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 7 ms 7996 KB Output is correct
19 Correct 6 ms 7996 KB Output is correct
20 Correct 7 ms 8072 KB Output is correct
21 Correct 6 ms 8020 KB Output is correct
22 Correct 7 ms 8008 KB Output is correct
23 Correct 7 ms 8016 KB Output is correct
24 Correct 8 ms 8124 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 6 ms 7892 KB Output is correct
27 Correct 6 ms 7892 KB Output is correct
28 Correct 7 ms 7868 KB Output is correct
29 Correct 5 ms 8020 KB Output is correct
30 Correct 6 ms 7764 KB Output is correct
31 Correct 6 ms 7764 KB Output is correct
32 Correct 5 ms 7732 KB Output is correct
33 Correct 6 ms 7764 KB Output is correct
34 Correct 193 ms 126180 KB Output is correct
35 Correct 215 ms 126160 KB Output is correct
36 Correct 206 ms 126132 KB Output is correct
37 Correct 240 ms 126180 KB Output is correct
38 Correct 237 ms 126196 KB Output is correct
39 Correct 255 ms 126176 KB Output is correct
40 Correct 242 ms 126200 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 3 ms 5168 KB Output is correct
43 Correct 243 ms 115124 KB Output is correct
44 Correct 242 ms 115124 KB Output is correct
45 Correct 261 ms 115148 KB Output is correct
46 Correct 313 ms 121072 KB Output is correct
47 Correct 281 ms 119964 KB Output is correct
48 Correct 298 ms 121612 KB Output is correct
49 Correct 264 ms 119964 KB Output is correct
50 Correct 327 ms 114724 KB Output is correct
51 Correct 306 ms 114704 KB Output is correct
52 Correct 284 ms 114636 KB Output is correct
53 Correct 291 ms 114752 KB Output is correct