Submission #763839

# Submission time Handle Problem Language Result Execution time Memory
763839 2023-06-22T23:57:03 Z NK_ Cat Exercise (JOI23_ho_t4) C++17
100 / 100
367 ms 70580 KB
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'

struct DSU {
	vector<int> e, mx; void init(int N, const vector<int>& A) { e = vector<int>(N, -1); mx = A; }
	int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
	int getmx(int x) { return mx[get(x)]; }
	bool unite(int x, int y) {
		x = get(x), y = get(y);
		if (x == y) return 0;
		if (e[x] > e[y]) swap(x, y);
		e[x] += e[y]; mx[x] = max(mx[x], mx[y]); e[y] = x; return 1;
	}
};

using ll = long long;

const int LG = 20;

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

	int N; cin >> N;
	vector<int> A(N); for(auto& x : A) { cin >> x; --x; }
	vector<int> P(N); for(int i = 0; i < N; i++) P[A[i]] = i;

	vector<vector<int>> adj(N), ADJ(N);
	for(int i = 0; i < N-1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		if (A[u] < A[v]) swap(u, v);
		adj[u].push_back(v);
		adj[v].push_back(u);
		ADJ[u].push_back(v);
	}

	vector<vector<int>> up(N, vector<int>(LG));
	vector<int> dep(N);
	function<void(int, int)> dfs = [&](int u, int p) {
		up[u][0] = (p == -1 ? u : p);
		dep[u] = (p == -1 ? 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) dfs(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 u, int v) {
		if (dep[u] < dep[v]) swap(u, v);
		u = jmp(u, dep[u] - dep[v]);
		if (u == v) return u;
		for(int i = LG - 1; i >= 0; i--) if (up[u][i] != up[v][i]) {
			u = up[u][i], v = up[v][i];
		}
		return up[u][0];
	};

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

	vector<ll> dp(N);

	dfs(0, -1);

	DSU d; d.init(N, A);

	for(int u = 0; u < N; u++) {
		for(auto v : ADJ[P[u]]) {
			int x = d.getmx(v); 
			// cout << u << " - " << P[u] << nl;
			// cout << v << " <> " << x << nl;
			dp[u] = max(dp[u], dp[x] + dist(P[u], P[x]));
		}
		for(auto v : ADJ[P[u]]) d.unite(P[u], v);
	}

	cout << dp[N - 1] << nl;


	return 0;
}

/*

4 
3 4 1 2  
1 2
2 3
3 4

7 
3 2 7 1 5 4 6  
1 2
1 3
2 4
2 5
3 6
3 7

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1972 KB Output is correct
19 Correct 3 ms 2004 KB Output is correct
20 Correct 4 ms 2004 KB Output is correct
21 Correct 4 ms 2000 KB Output is correct
22 Correct 3 ms 1896 KB Output is correct
23 Correct 3 ms 1876 KB Output is correct
24 Correct 3 ms 1948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1972 KB Output is correct
19 Correct 3 ms 2004 KB Output is correct
20 Correct 4 ms 2004 KB Output is correct
21 Correct 4 ms 2000 KB Output is correct
22 Correct 3 ms 1896 KB Output is correct
23 Correct 3 ms 1876 KB Output is correct
24 Correct 3 ms 1948 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 4 ms 1824 KB Output is correct
27 Correct 4 ms 1876 KB Output is correct
28 Correct 6 ms 1748 KB Output is correct
29 Correct 4 ms 1876 KB Output is correct
30 Correct 4 ms 1612 KB Output is correct
31 Correct 4 ms 1564 KB Output is correct
32 Correct 4 ms 1608 KB Output is correct
33 Correct 4 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1972 KB Output is correct
19 Correct 3 ms 2004 KB Output is correct
20 Correct 4 ms 2004 KB Output is correct
21 Correct 4 ms 2000 KB Output is correct
22 Correct 3 ms 1896 KB Output is correct
23 Correct 3 ms 1876 KB Output is correct
24 Correct 3 ms 1948 KB Output is correct
25 Correct 155 ms 70580 KB Output is correct
26 Correct 133 ms 70036 KB Output is correct
27 Correct 134 ms 70024 KB Output is correct
28 Correct 181 ms 68512 KB Output is correct
29 Correct 207 ms 68276 KB Output is correct
30 Correct 174 ms 68432 KB Output is correct
31 Correct 163 ms 68560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 186 ms 52540 KB Output is correct
4 Correct 181 ms 52488 KB Output is correct
5 Correct 230 ms 52416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 0 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 1972 KB Output is correct
19 Correct 3 ms 2004 KB Output is correct
20 Correct 4 ms 2004 KB Output is correct
21 Correct 4 ms 2000 KB Output is correct
22 Correct 3 ms 1896 KB Output is correct
23 Correct 3 ms 1876 KB Output is correct
24 Correct 3 ms 1948 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 4 ms 1824 KB Output is correct
27 Correct 4 ms 1876 KB Output is correct
28 Correct 6 ms 1748 KB Output is correct
29 Correct 4 ms 1876 KB Output is correct
30 Correct 4 ms 1612 KB Output is correct
31 Correct 4 ms 1564 KB Output is correct
32 Correct 4 ms 1608 KB Output is correct
33 Correct 4 ms 1492 KB Output is correct
34 Correct 155 ms 70580 KB Output is correct
35 Correct 133 ms 70036 KB Output is correct
36 Correct 134 ms 70024 KB Output is correct
37 Correct 181 ms 68512 KB Output is correct
38 Correct 207 ms 68276 KB Output is correct
39 Correct 174 ms 68432 KB Output is correct
40 Correct 163 ms 68560 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 186 ms 52540 KB Output is correct
44 Correct 181 ms 52488 KB Output is correct
45 Correct 230 ms 52416 KB Output is correct
46 Correct 358 ms 62660 KB Output is correct
47 Correct 360 ms 61308 KB Output is correct
48 Correct 367 ms 63272 KB Output is correct
49 Correct 351 ms 61320 KB Output is correct
50 Correct 334 ms 52712 KB Output is correct
51 Correct 357 ms 52680 KB Output is correct
52 Correct 323 ms 52824 KB Output is correct
53 Correct 338 ms 52808 KB Output is correct