답안 #927277

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927277 2024-02-14T15:11:22 Z belgianbot Cat Exercise (JOI23_ho_t4) C++14
100 / 100
458 ms 67412 KB
#include <bits/stdc++.h>

using namespace std;

vector <vector<int> > connections, up, child;
vector <int> p, parent, depth;
int N, LOG;
	

void dfs(int a, int pa) {
	for (int b : connections[a]) {
		if(b == pa) continue;
		depth[b] = depth[a] + 1;
		up[b][0] = a;
		for (int i(1); i < LOG; i++) 
			up[b][i] = up[ up[b][i - 1] ][i - 1];
		dfs(b, a);
	}
}
int lca(int a, int b) {
	if (depth[a] < depth[b])
		swap(a, b);
		
	int k = depth[a] - depth[b];
	for (int i(LOG); i >= 0; i--) {
		if (k & (1 << i))
			a = up[a][i];
	}
	
	if (a == b)
		return a;
	
	for (int i(LOG); i >= 0; i--) {
		if (up[a][i] != up[b][i]) {
			a = up[a][i];
			b = up[b][i];
		}
	}
	
	return up[a][0];
}
int Find(int x) {
	if (x == parent[x]) return x;
	return Find(parent[x]);
}

void Merge(int a, int b) {
	int pb = Find(b);
	
	child[a].push_back(pb);
	parent[pb] = a;
}

long long score(int x) {
	long long ans(0);
	for (int i : child[x]) {
		int root = lca(x, i);
		ans = max(ans, score(i) + (long long)(depth[x] + depth[i] - 2 * depth[root]));
	}
	return ans;
}
int main() {
	cin >> N;
	LOG = ceil(log2(N));
	connections.resize(N); p.resize(N), parent.resize(N); depth.resize(N); up.resize(N, vector<int> (LOG + 1, N - 1)); child.resize(N);
	for (int i(0); i < N; i++) {
		cin >> p[i];
		p[i]--;
		parent[i] = i;
	}
	

	for (int i(0); i < N - 1; i++) {
		int a, b; cin >> a >> b;
		a--; b--;
		connections[p[a]].push_back(p[b]);
		connections[p[b]].push_back(p[a]);
	}
	
	depth[N - 1] = 0;
	dfs(N - 1, -1);
	for (int i(1); i < N; i++) {
		for (int j : connections[i]) {
			if (j < i) Merge(i, j);
		}
	}
	
	cout << score(N - 1) << '\n';
		
	return 0;
}
	
	
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 440 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 440 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 440 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 5 ms 1884 KB Output is correct
19 Correct 5 ms 1628 KB Output is correct
20 Correct 5 ms 1624 KB Output is correct
21 Correct 5 ms 1624 KB Output is correct
22 Correct 5 ms 1372 KB Output is correct
23 Correct 5 ms 1372 KB Output is correct
24 Correct 5 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 440 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 5 ms 1884 KB Output is correct
19 Correct 5 ms 1628 KB Output is correct
20 Correct 5 ms 1624 KB Output is correct
21 Correct 5 ms 1624 KB Output is correct
22 Correct 5 ms 1372 KB Output is correct
23 Correct 5 ms 1372 KB Output is correct
24 Correct 5 ms 1372 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 7 ms 1628 KB Output is correct
27 Correct 6 ms 1628 KB Output is correct
28 Correct 5 ms 1740 KB Output is correct
29 Correct 5 ms 1628 KB Output is correct
30 Correct 5 ms 1468 KB Output is correct
31 Correct 8 ms 1480 KB Output is correct
32 Correct 5 ms 1372 KB Output is correct
33 Correct 5 ms 1372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 440 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 5 ms 1884 KB Output is correct
19 Correct 5 ms 1628 KB Output is correct
20 Correct 5 ms 1624 KB Output is correct
21 Correct 5 ms 1624 KB Output is correct
22 Correct 5 ms 1372 KB Output is correct
23 Correct 5 ms 1372 KB Output is correct
24 Correct 5 ms 1372 KB Output is correct
25 Correct 229 ms 67412 KB Output is correct
26 Correct 223 ms 65360 KB Output is correct
27 Correct 278 ms 65620 KB Output is correct
28 Correct 348 ms 56672 KB Output is correct
29 Correct 331 ms 57972 KB Output is correct
30 Correct 365 ms 57900 KB Output is correct
31 Correct 337 ms 58196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 436 ms 50468 KB Output is correct
4 Correct 458 ms 50744 KB Output is correct
5 Correct 407 ms 50516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 440 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 5 ms 1884 KB Output is correct
19 Correct 5 ms 1628 KB Output is correct
20 Correct 5 ms 1624 KB Output is correct
21 Correct 5 ms 1624 KB Output is correct
22 Correct 5 ms 1372 KB Output is correct
23 Correct 5 ms 1372 KB Output is correct
24 Correct 5 ms 1372 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 7 ms 1628 KB Output is correct
27 Correct 6 ms 1628 KB Output is correct
28 Correct 5 ms 1740 KB Output is correct
29 Correct 5 ms 1628 KB Output is correct
30 Correct 5 ms 1468 KB Output is correct
31 Correct 8 ms 1480 KB Output is correct
32 Correct 5 ms 1372 KB Output is correct
33 Correct 5 ms 1372 KB Output is correct
34 Correct 229 ms 67412 KB Output is correct
35 Correct 223 ms 65360 KB Output is correct
36 Correct 278 ms 65620 KB Output is correct
37 Correct 348 ms 56672 KB Output is correct
38 Correct 331 ms 57972 KB Output is correct
39 Correct 365 ms 57900 KB Output is correct
40 Correct 337 ms 58196 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 436 ms 50468 KB Output is correct
44 Correct 458 ms 50744 KB Output is correct
45 Correct 407 ms 50516 KB Output is correct
46 Correct 354 ms 62104 KB Output is correct
47 Correct 342 ms 61964 KB Output is correct
48 Correct 359 ms 61824 KB Output is correct
49 Correct 343 ms 61776 KB Output is correct
50 Correct 350 ms 49900 KB Output is correct
51 Correct 373 ms 49848 KB Output is correct
52 Correct 366 ms 49888 KB Output is correct
53 Correct 357 ms 49796 KB Output is correct