Submission #857733

# Submission time Handle Problem Language Result Execution time Memory
857733 2023-10-06T18:29:05 Z Hakiers Cat Exercise (JOI23_ho_t4) C++17
100 / 100
200 ms 45696 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5+7;
const int logx = 18;
vector<int> G[MAXN];
int jmp[MAXN][logx+1];
int t[MAXN];
int depth[MAXN];
int rep[MAXN];
int val[MAXN];
pair<ll, int> res[MAXN];
int n;

void connect(int a, int b){
	//cout << "connect: " << a << " " << b << "\n";
	G[a].push_back(b);
	G[b].push_back(a);
}

void dfs(int v, int p){
	depth[v] = depth[p] + 1;
	jmp[v][0] = p;
	
	for(int k = 1; k <=logx; k++)
		jmp[v][k] = jmp[jmp[v][k-1]][k-1];
		
	for(auto u : G[v])
		if(u != p) dfs(u, v);

}

int lca(int a, int b){
	if(depth[a] > depth[b]) swap(a, b);
	
	for(int k = logx; k >= 0; k--)
		if(depth[jmp[b][k]] >= depth[a])
			b = jmp[b][k];
			
	if(a == b)
		return a;
	
	for(int k = logx; k >= 0; k--){
		if(jmp[a][k] != jmp[b][k]){
			a = jmp[a][k];
			b = jmp[b][k];
		}
	}
	
	return jmp[a][0];
}

ll get_dist(int a, int b){
	return depth[a] + depth[b] - 2*depth[lca(a, b)];
}

int Find(int a){
	if(rep[a] == a)
		return a;
	return rep[a] = Find(rep[a]);
}

void Union(int a, int b){
	//cout << "a: " << a << " b: " << b << "\n";
	int tmpa = a;
	a = Find(a);
	b = Find(b);
	if(a == b) return;
	
	ll dist = get_dist(tmpa, res[b].second);
	pair<ll, int> sum = {res[b].first + dist, tmpa};
	
	
	if(val[a] < val[b]) swap(a, b);
	rep[b] = a;
	val[a] += val[b];
	res[a] = max({res[b], res[a], sum});
}


void merge(int v){

	for(auto u : G[v])
		 if(u < v) Union(v, u);
		 
	//cout << "res: " << v << " " << res[Find(v)].first << "\n";
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	
	for(int i = 1; i <= n; i++)
		cin >> t[i];
	
	for(int i = 1; i < n; i++){
		int a, b;
		cin >> a >> b;
		connect(t[a], t[b]);
	}
	
	dfs(n, n);
	
	for(int i = 1; i <= n; i++){
		rep[i] = i;
		val[i] = 1;
		res[i].second = i;
	}
	
	for(int i = 1; i <= n; i++) merge(i);
	
	cout << res[Find(n)].first << "\n";

}

/*
10
9 2 4 10 6 7 3 5 8 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10




*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9692 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9692 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9560 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9692 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9560 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 4 ms 12124 KB Output is correct
19 Correct 5 ms 12124 KB Output is correct
20 Correct 4 ms 12268 KB Output is correct
21 Correct 4 ms 12124 KB Output is correct
22 Correct 5 ms 12068 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9692 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9560 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 4 ms 12124 KB Output is correct
19 Correct 5 ms 12124 KB Output is correct
20 Correct 4 ms 12268 KB Output is correct
21 Correct 4 ms 12124 KB Output is correct
22 Correct 5 ms 12068 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12124 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 4 ms 12124 KB Output is correct
27 Correct 6 ms 12072 KB Output is correct
28 Correct 5 ms 12124 KB Output is correct
29 Correct 5 ms 12124 KB Output is correct
30 Correct 4 ms 12064 KB Output is correct
31 Correct 4 ms 11864 KB Output is correct
32 Correct 5 ms 11868 KB Output is correct
33 Correct 4 ms 11868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9692 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9560 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 4 ms 12124 KB Output is correct
19 Correct 5 ms 12124 KB Output is correct
20 Correct 4 ms 12268 KB Output is correct
21 Correct 4 ms 12124 KB Output is correct
22 Correct 5 ms 12068 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12124 KB Output is correct
25 Correct 101 ms 45696 KB Output is correct
26 Correct 102 ms 45508 KB Output is correct
27 Correct 104 ms 45640 KB Output is correct
28 Correct 193 ms 43348 KB Output is correct
29 Correct 200 ms 44648 KB Output is correct
30 Correct 173 ms 44112 KB Output is correct
31 Correct 199 ms 44880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 154 ms 32656 KB Output is correct
4 Correct 163 ms 36272 KB Output is correct
5 Correct 167 ms 36196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9564 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9692 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9560 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 4 ms 12124 KB Output is correct
19 Correct 5 ms 12124 KB Output is correct
20 Correct 4 ms 12268 KB Output is correct
21 Correct 4 ms 12124 KB Output is correct
22 Correct 5 ms 12068 KB Output is correct
23 Correct 5 ms 12124 KB Output is correct
24 Correct 4 ms 12124 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 4 ms 12124 KB Output is correct
27 Correct 6 ms 12072 KB Output is correct
28 Correct 5 ms 12124 KB Output is correct
29 Correct 5 ms 12124 KB Output is correct
30 Correct 4 ms 12064 KB Output is correct
31 Correct 4 ms 11864 KB Output is correct
32 Correct 5 ms 11868 KB Output is correct
33 Correct 4 ms 11868 KB Output is correct
34 Correct 101 ms 45696 KB Output is correct
35 Correct 102 ms 45508 KB Output is correct
36 Correct 104 ms 45640 KB Output is correct
37 Correct 193 ms 43348 KB Output is correct
38 Correct 200 ms 44648 KB Output is correct
39 Correct 173 ms 44112 KB Output is correct
40 Correct 199 ms 44880 KB Output is correct
41 Correct 2 ms 9564 KB Output is correct
42 Correct 2 ms 9564 KB Output is correct
43 Correct 154 ms 32656 KB Output is correct
44 Correct 163 ms 36272 KB Output is correct
45 Correct 167 ms 36196 KB Output is correct
46 Correct 124 ms 42688 KB Output is correct
47 Correct 119 ms 42836 KB Output is correct
48 Correct 122 ms 43600 KB Output is correct
49 Correct 129 ms 42876 KB Output is correct
50 Correct 180 ms 36420 KB Output is correct
51 Correct 200 ms 36196 KB Output is correct
52 Correct 176 ms 36396 KB Output is correct
53 Correct 176 ms 36180 KB Output is correct