답안 #963138

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963138 2024-04-14T15:02:02 Z dorulean Cat Exercise (JOI23_ho_t4) C++17
100 / 100
465 ms 53076 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//#define int ll
using vi = vector<int>;
using pii = pair<int,int>;
using vpii = vector<pii>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
#define eb emplace_back

const int N = 2e5 + 5;
int a[N], ord[N], n, t[N], up[20][N], depth[N];
ll dp[N];
vi g[N];
pii best[N];
bool active[N];

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

 	cin >> n;
 	for (int i = 1; i <= n; ++i)
 		cin >> a[i], ord[i] = i;
 	for (int u, v, i = 1; i < n; ++i) {
 		cin >> u >> v;
 		g[u].eb(v);
 		g[v].eb(u);
 	}
 	sort(ord + 1, ord + n + 1, [&](const int& i, const int& j) {return a[i] < a[j];});
 	
 	function<void(int,int)> dfs = [&](int u, int parent) {
 		up[0][u] = parent;
 		depth[u] = depth[parent] + 1;
 		for (auto v : g[u])
 			if (v != parent)
 				dfs(v, u);
 	};
	function<int(int,int)> lca = [&](int u, int v) {	
		if (depth[u] < depth[v])
			swap(u, v);
 		for (int p = 19; p >= 0; --p)
 			if (up[p][u] && depth[up[p][u]] >= depth[v])
 				u = up[p][u];
 		if (u == v) return u;
 		for (int p = 19; p >= 0; --p)
 			if (up[p][u] != up[p][v])
 				u = up[p][u], v = up[p][v];
 		return up[0][u];
 	};
 	function<int(int,int)> dist = [&](int u, int v) {
 		return depth[u] + depth[v] - 2 * depth[lca(u, v)];
 	};
 	function<int(int)> find = [&](int u) {
 		return (u == t[u] ? u : (t[u] = find(t[u])));
 	};
 	function<void(int,int)> join = [&](int u, int v) {
 		int ru = find(u), rv = find(v);
 		t[ru] = rv;
 		best[rv] = max(best[rv], best[ru]);
 	};
 	
 	dfs(1, 0);
 	for (int p = 1; p < 20; ++p)
 		for (int i = 1; i <= n; ++i)
 			up[p][i] = up[p - 1][up[p - 1][i]];
 	
 	for (int i = 1; i <= n; ++i) {
 		int u = ord[i];
 		t[u] = u, best[u] = {a[u], u};
 		active[u] = true;
 		for (auto v : g[u]) {
 			if (active[v]) {
 				int mx = best[find(v)].second;
 				dp[u] = max(dp[u], dp[mx] + dist(u, mx));
 				join(u, v);
 			}
 		}
 	}

 	cout << dp[ord[n]] << '\n';
} 

# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 5 ms 24924 KB Output is correct
7 Correct 4 ms 24924 KB Output is correct
8 Correct 5 ms 25076 KB Output is correct
9 Correct 4 ms 24920 KB Output is correct
10 Correct 4 ms 24924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 5 ms 24924 KB Output is correct
7 Correct 4 ms 24924 KB Output is correct
8 Correct 5 ms 25076 KB Output is correct
9 Correct 4 ms 24920 KB Output is correct
10 Correct 4 ms 24924 KB Output is correct
11 Correct 4 ms 25124 KB Output is correct
12 Correct 4 ms 24924 KB Output is correct
13 Correct 4 ms 24920 KB Output is correct
14 Correct 4 ms 24924 KB Output is correct
15 Correct 4 ms 25104 KB Output is correct
16 Correct 4 ms 24924 KB Output is correct
17 Correct 4 ms 24924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 5 ms 24924 KB Output is correct
7 Correct 4 ms 24924 KB Output is correct
8 Correct 5 ms 25076 KB Output is correct
9 Correct 4 ms 24920 KB Output is correct
10 Correct 4 ms 24924 KB Output is correct
11 Correct 4 ms 25124 KB Output is correct
12 Correct 4 ms 24924 KB Output is correct
13 Correct 4 ms 24920 KB Output is correct
14 Correct 4 ms 24924 KB Output is correct
15 Correct 4 ms 25104 KB Output is correct
16 Correct 4 ms 24924 KB Output is correct
17 Correct 4 ms 24924 KB Output is correct
18 Correct 8 ms 25436 KB Output is correct
19 Correct 6 ms 25436 KB Output is correct
20 Correct 6 ms 25436 KB Output is correct
21 Correct 7 ms 25436 KB Output is correct
22 Correct 6 ms 25436 KB Output is correct
23 Correct 7 ms 25436 KB Output is correct
24 Correct 10 ms 25436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 5 ms 24924 KB Output is correct
7 Correct 4 ms 24924 KB Output is correct
8 Correct 5 ms 25076 KB Output is correct
9 Correct 4 ms 24920 KB Output is correct
10 Correct 4 ms 24924 KB Output is correct
11 Correct 4 ms 25124 KB Output is correct
12 Correct 4 ms 24924 KB Output is correct
13 Correct 4 ms 24920 KB Output is correct
14 Correct 4 ms 24924 KB Output is correct
15 Correct 4 ms 25104 KB Output is correct
16 Correct 4 ms 24924 KB Output is correct
17 Correct 4 ms 24924 KB Output is correct
18 Correct 8 ms 25436 KB Output is correct
19 Correct 6 ms 25436 KB Output is correct
20 Correct 6 ms 25436 KB Output is correct
21 Correct 7 ms 25436 KB Output is correct
22 Correct 6 ms 25436 KB Output is correct
23 Correct 7 ms 25436 KB Output is correct
24 Correct 10 ms 25436 KB Output is correct
25 Correct 5 ms 24924 KB Output is correct
26 Correct 6 ms 25436 KB Output is correct
27 Correct 7 ms 25436 KB Output is correct
28 Correct 8 ms 25436 KB Output is correct
29 Correct 7 ms 25572 KB Output is correct
30 Correct 9 ms 25180 KB Output is correct
31 Correct 6 ms 25180 KB Output is correct
32 Correct 8 ms 25184 KB Output is correct
33 Correct 6 ms 25180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 5 ms 24924 KB Output is correct
7 Correct 4 ms 24924 KB Output is correct
8 Correct 5 ms 25076 KB Output is correct
9 Correct 4 ms 24920 KB Output is correct
10 Correct 4 ms 24924 KB Output is correct
11 Correct 4 ms 25124 KB Output is correct
12 Correct 4 ms 24924 KB Output is correct
13 Correct 4 ms 24920 KB Output is correct
14 Correct 4 ms 24924 KB Output is correct
15 Correct 4 ms 25104 KB Output is correct
16 Correct 4 ms 24924 KB Output is correct
17 Correct 4 ms 24924 KB Output is correct
18 Correct 8 ms 25436 KB Output is correct
19 Correct 6 ms 25436 KB Output is correct
20 Correct 6 ms 25436 KB Output is correct
21 Correct 7 ms 25436 KB Output is correct
22 Correct 6 ms 25436 KB Output is correct
23 Correct 7 ms 25436 KB Output is correct
24 Correct 10 ms 25436 KB Output is correct
25 Correct 114 ms 48980 KB Output is correct
26 Correct 130 ms 52948 KB Output is correct
27 Correct 127 ms 52948 KB Output is correct
28 Correct 329 ms 52948 KB Output is correct
29 Correct 372 ms 52944 KB Output is correct
30 Correct 349 ms 53076 KB Output is correct
31 Correct 313 ms 52812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 24920 KB Output is correct
2 Correct 5 ms 25176 KB Output is correct
3 Correct 221 ms 33512 KB Output is correct
4 Correct 261 ms 33504 KB Output is correct
5 Correct 252 ms 33616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 24924 KB Output is correct
2 Correct 5 ms 24924 KB Output is correct
3 Correct 4 ms 24924 KB Output is correct
4 Correct 5 ms 24924 KB Output is correct
5 Correct 4 ms 24924 KB Output is correct
6 Correct 5 ms 24924 KB Output is correct
7 Correct 4 ms 24924 KB Output is correct
8 Correct 5 ms 25076 KB Output is correct
9 Correct 4 ms 24920 KB Output is correct
10 Correct 4 ms 24924 KB Output is correct
11 Correct 4 ms 25124 KB Output is correct
12 Correct 4 ms 24924 KB Output is correct
13 Correct 4 ms 24920 KB Output is correct
14 Correct 4 ms 24924 KB Output is correct
15 Correct 4 ms 25104 KB Output is correct
16 Correct 4 ms 24924 KB Output is correct
17 Correct 4 ms 24924 KB Output is correct
18 Correct 8 ms 25436 KB Output is correct
19 Correct 6 ms 25436 KB Output is correct
20 Correct 6 ms 25436 KB Output is correct
21 Correct 7 ms 25436 KB Output is correct
22 Correct 6 ms 25436 KB Output is correct
23 Correct 7 ms 25436 KB Output is correct
24 Correct 10 ms 25436 KB Output is correct
25 Correct 5 ms 24924 KB Output is correct
26 Correct 6 ms 25436 KB Output is correct
27 Correct 7 ms 25436 KB Output is correct
28 Correct 8 ms 25436 KB Output is correct
29 Correct 7 ms 25572 KB Output is correct
30 Correct 9 ms 25180 KB Output is correct
31 Correct 6 ms 25180 KB Output is correct
32 Correct 8 ms 25184 KB Output is correct
33 Correct 6 ms 25180 KB Output is correct
34 Correct 114 ms 48980 KB Output is correct
35 Correct 130 ms 52948 KB Output is correct
36 Correct 127 ms 52948 KB Output is correct
37 Correct 329 ms 52948 KB Output is correct
38 Correct 372 ms 52944 KB Output is correct
39 Correct 349 ms 53076 KB Output is correct
40 Correct 313 ms 52812 KB Output is correct
41 Correct 4 ms 24920 KB Output is correct
42 Correct 5 ms 25176 KB Output is correct
43 Correct 221 ms 33512 KB Output is correct
44 Correct 261 ms 33504 KB Output is correct
45 Correct 252 ms 33616 KB Output is correct
46 Correct 434 ms 45868 KB Output is correct
47 Correct 440 ms 44544 KB Output is correct
48 Correct 459 ms 46672 KB Output is correct
49 Correct 465 ms 44324 KB Output is correct
50 Correct 415 ms 37408 KB Output is correct
51 Correct 324 ms 37204 KB Output is correct
52 Correct 322 ms 37412 KB Output is correct
53 Correct 338 ms 37408 KB Output is correct