Submission #861487

# Submission time Handle Problem Language Result Execution time Memory
861487 2023-10-16T10:44:24 Z NotLinux Cat Exercise (JOI23_ho_t4) C++17
100 / 100
267 ms 77320 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 7;
const int M = 30;
int n,p[N],lift[N][M],depth[N];
vector < int > tree[N];
pair < int , int > ord[N];
inline void dfs(int node , int par , int dpt){
	lift[node][0] = par;
	depth[node] = dpt;
	for(int i = 0;i<(int)tree[node].size();i++){
		if(par != tree[node][i]){
			dfs(tree[node][i],node,dpt+1);
		}
	}
}
inline int lca(int a , int b){
	if(depth[a] > depth[b])swap(a,b);
	int diff = depth[b] - depth[a];
	for(int i = 0;i<M;i++){
		if((diff >> i) & 1){
			b = lift[b][i];
		}
	}
	if(a == b)return a;
	for(int i = M-1;i>=0;i--){
		if(lift[a][i] != lift[b][i]){
			a = lift[a][i];
			b = lift[b][i];
		}
	}
	return lift[a][0];
}
inline int dist(int a , int b){
	return depth[a] + depth[b] - 2 * depth[lca(a,b)];
}
struct DSU{
	vector < int > par,dp;
	DSU(int x){
		x += 3;
		par.resize(x);
		iota(par.begin() , par.end() , 0);
		dp.assign(x,0);
	}
	int find(int a){
		if(par[a] == a)return a;
		return par[a] = find(par[a]);
	}
	void merge(int a , int b){//a -> b
		a = find(a) , b = find(b);
		dp[b] = max(dp[b] , dp[a] + dist(a,b) );
		par[a] = b;
	}
} dsu(N);
void solve(){
	cin >> n;
	for(int i = 0;i<n;i++){
		cin >> p[i];
		ord[i] = {p[i],i};
	}
	sort(ord , ord + n);
	for(int i = 1;i<n;i++){
		int ai,bi;cin >> ai >> bi;
		ai--;bi--;
		tree[ai].push_back(bi);
		tree[bi].push_back(ai);
	}
	//calculating lca
	dfs(0,0,0);
	for(int i = 1;i<M;i++){
		for(int j = 0;j<n;j++){
			lift[j][i] = lift[lift[j][i-1]][i-1];
		}
	}
	int ans = 0;
	for(int i = 0;i<n;i++){
		for(auto itr : tree[ord[i].second]){
			if(p[itr] < ord[i].first){
				dsu.merge(itr , ord[i].second);
			}
		}
		ans = max(ans , dsu.dp[ord[i].second]);
	}
	cout << ans << endl;
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	int testcase = 1;//cin >> testcase;
	while(testcase--)solve();
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13872 KB Output is correct
17 Correct 3 ms 13912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13872 KB Output is correct
17 Correct 3 ms 13912 KB Output is correct
18 Correct 5 ms 16220 KB Output is correct
19 Correct 5 ms 16220 KB Output is correct
20 Correct 6 ms 16216 KB Output is correct
21 Correct 5 ms 16220 KB Output is correct
22 Correct 5 ms 16220 KB Output is correct
23 Correct 6 ms 16216 KB Output is correct
24 Correct 5 ms 16220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13872 KB Output is correct
17 Correct 3 ms 13912 KB Output is correct
18 Correct 5 ms 16220 KB Output is correct
19 Correct 5 ms 16220 KB Output is correct
20 Correct 6 ms 16216 KB Output is correct
21 Correct 5 ms 16220 KB Output is correct
22 Correct 5 ms 16220 KB Output is correct
23 Correct 6 ms 16216 KB Output is correct
24 Correct 5 ms 16220 KB Output is correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 5 ms 16220 KB Output is correct
27 Correct 6 ms 16228 KB Output is correct
28 Correct 7 ms 16216 KB Output is correct
29 Correct 5 ms 16220 KB Output is correct
30 Correct 6 ms 16036 KB Output is correct
31 Correct 6 ms 16200 KB Output is correct
32 Correct 7 ms 16220 KB Output is correct
33 Correct 6 ms 16216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13872 KB Output is correct
17 Correct 3 ms 13912 KB Output is correct
18 Correct 5 ms 16220 KB Output is correct
19 Correct 5 ms 16220 KB Output is correct
20 Correct 6 ms 16216 KB Output is correct
21 Correct 5 ms 16220 KB Output is correct
22 Correct 5 ms 16220 KB Output is correct
23 Correct 6 ms 16216 KB Output is correct
24 Correct 5 ms 16220 KB Output is correct
25 Correct 129 ms 73300 KB Output is correct
26 Correct 133 ms 77024 KB Output is correct
27 Correct 133 ms 76880 KB Output is correct
28 Correct 169 ms 77320 KB Output is correct
29 Correct 175 ms 77112 KB Output is correct
30 Correct 153 ms 76924 KB Output is correct
31 Correct 168 ms 77112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 196 ms 73012 KB Output is correct
4 Correct 210 ms 72780 KB Output is correct
5 Correct 237 ms 73000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13660 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 3 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 4 ms 13660 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13656 KB Output is correct
12 Correct 4 ms 13660 KB Output is correct
13 Correct 3 ms 13660 KB Output is correct
14 Correct 4 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13872 KB Output is correct
17 Correct 3 ms 13912 KB Output is correct
18 Correct 5 ms 16220 KB Output is correct
19 Correct 5 ms 16220 KB Output is correct
20 Correct 6 ms 16216 KB Output is correct
21 Correct 5 ms 16220 KB Output is correct
22 Correct 5 ms 16220 KB Output is correct
23 Correct 6 ms 16216 KB Output is correct
24 Correct 5 ms 16220 KB Output is correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 5 ms 16220 KB Output is correct
27 Correct 6 ms 16228 KB Output is correct
28 Correct 7 ms 16216 KB Output is correct
29 Correct 5 ms 16220 KB Output is correct
30 Correct 6 ms 16036 KB Output is correct
31 Correct 6 ms 16200 KB Output is correct
32 Correct 7 ms 16220 KB Output is correct
33 Correct 6 ms 16216 KB Output is correct
34 Correct 129 ms 73300 KB Output is correct
35 Correct 133 ms 77024 KB Output is correct
36 Correct 133 ms 76880 KB Output is correct
37 Correct 169 ms 77320 KB Output is correct
38 Correct 175 ms 77112 KB Output is correct
39 Correct 153 ms 76924 KB Output is correct
40 Correct 168 ms 77112 KB Output is correct
41 Correct 3 ms 13660 KB Output is correct
42 Correct 3 ms 13660 KB Output is correct
43 Correct 196 ms 73012 KB Output is correct
44 Correct 210 ms 72780 KB Output is correct
45 Correct 237 ms 73000 KB Output is correct
46 Correct 242 ms 75092 KB Output is correct
47 Correct 228 ms 74420 KB Output is correct
48 Correct 203 ms 75348 KB Output is correct
49 Correct 238 ms 74616 KB Output is correct
50 Correct 267 ms 72592 KB Output is correct
51 Correct 220 ms 72532 KB Output is correct
52 Correct 222 ms 72528 KB Output is correct
53 Correct 238 ms 72812 KB Output is correct