Submission #790845

# Submission time Handle Problem Language Result Execution time Memory
790845 2023-07-23T08:57:56 Z willychan Cat Exercise (JOI23_ho_t4) C++14
100 / 100
314 ms 57324 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds


const int N = 200005;
int w[N];
vector<int> side[N];
vector<int> from[N];
int head[N];
int p[N][19];
int dep[N];
ll dp[N];
void dfs(int cur){
	for(auto v : side[cur]){
		if(v==p[cur][0]) continue;
		p[v][0]=cur;
		dep[v]=dep[cur]+1;
		dfs(v);
	}
}

int dis(int a,int b){
	int oga= a;
	int ogb = b;
	if(dep[a]>dep[b]) swap(a,b);
	for(int l = 18;l>=0;l--){
		if(dep[p[b][l]]>dep[a])	b = p[b][l];
	}
	if(dep[b]!=dep[a]) b = p[b][0];
	if(a==b){
		return abs(dep[oga]-dep[ogb]);
	}
	for(int l=18;l>=0;l--){
		if(p[b][l]!=p[a][l]){
			a = p[a][l];
			b = p[b][l];
		}
	}
	if(a!=b) a=p[a][0];
	return abs(dep[a]-dep[oga])+abs(dep[a]-dep[ogb]);
}

int query(int a){
	if(head[a]==a) return a;
	head[a] = query(head[a]);
	return head[a];
}

void join(int a,int b){
	a = query(a);b = query(b);
	if(a==b) return;
	if(w[a]>w[b]) swap(a,b);
	head[a] =b;
}

int main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++) cin>>w[i];
	for(int i=0;i<n-1;i++){
		int a,b;cin>>a>>b;
		side[a].push_back(b);
		side[b].push_back(a);
	}
	p[1][0]=1;
	dep[1]=1;
	dfs(1);
	for(int l=1;l<19;l++){
		for(int i=1;i<=n;i++){
			p[i][l] = p[p[i][l-1]][l-1];
		}
	}
	int order[n];
	for(int i=1;i<=n;i++) {order[i-1]=i;head[i]=i;}
	sort(order,order+n,[&](const int a,const int b){return w[a]<w[b];});
	for(int i=0;i<n;i++){
		int cur = order[i];
		for(auto v : side[cur]){
			if(w[v]>w[cur]) continue;
			int g = query(v);
			from[g].push_back(cur);
			join(g,cur);
		}
	}
	sort(order,order+n,[&](const int a,const int b){return w[a]>w[b];});
	ll maxn = 0;
	for(int i=1;i<n;i++){
		int cur = order[i];
		for(auto v : from[cur]){
			dp[cur] = max(dp[v]+dis(v,cur),dp[cur]);
		}
		maxn = max(dp[cur],maxn);
	}
	cout<<maxn<<"\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9732 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9732 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9736 KB Output is correct
12 Correct 4 ms 9728 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 4 ms 9728 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9732 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9736 KB Output is correct
12 Correct 4 ms 9728 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 4 ms 9728 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 10 ms 10836 KB Output is correct
19 Correct 7 ms 10796 KB Output is correct
20 Correct 7 ms 10836 KB Output is correct
21 Correct 10 ms 10896 KB Output is correct
22 Correct 8 ms 10896 KB Output is correct
23 Correct 7 ms 10836 KB Output is correct
24 Correct 10 ms 10836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9732 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9736 KB Output is correct
12 Correct 4 ms 9728 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 4 ms 9728 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 10 ms 10836 KB Output is correct
19 Correct 7 ms 10796 KB Output is correct
20 Correct 7 ms 10836 KB Output is correct
21 Correct 10 ms 10896 KB Output is correct
22 Correct 8 ms 10896 KB Output is correct
23 Correct 7 ms 10836 KB Output is correct
24 Correct 10 ms 10836 KB Output is correct
25 Correct 4 ms 9684 KB Output is correct
26 Correct 7 ms 10708 KB Output is correct
27 Correct 7 ms 10764 KB Output is correct
28 Correct 7 ms 10728 KB Output is correct
29 Correct 7 ms 10768 KB Output is correct
30 Correct 7 ms 10580 KB Output is correct
31 Correct 11 ms 10548 KB Output is correct
32 Correct 8 ms 10512 KB Output is correct
33 Correct 10 ms 10584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9732 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9736 KB Output is correct
12 Correct 4 ms 9728 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 4 ms 9728 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 10 ms 10836 KB Output is correct
19 Correct 7 ms 10796 KB Output is correct
20 Correct 7 ms 10836 KB Output is correct
21 Correct 10 ms 10896 KB Output is correct
22 Correct 8 ms 10896 KB Output is correct
23 Correct 7 ms 10836 KB Output is correct
24 Correct 10 ms 10836 KB Output is correct
25 Correct 133 ms 57244 KB Output is correct
26 Correct 161 ms 57236 KB Output is correct
27 Correct 132 ms 57312 KB Output is correct
28 Correct 248 ms 57324 KB Output is correct
29 Correct 288 ms 57324 KB Output is correct
30 Correct 252 ms 57320 KB Output is correct
31 Correct 254 ms 57324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 225 ms 45432 KB Output is correct
4 Correct 283 ms 45456 KB Output is correct
5 Correct 240 ms 45464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9680 KB Output is correct
2 Correct 5 ms 9732 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 6 ms 9728 KB Output is correct
5 Correct 6 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9732 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9736 KB Output is correct
12 Correct 4 ms 9728 KB Output is correct
13 Correct 5 ms 9732 KB Output is correct
14 Correct 4 ms 9728 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9732 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 10 ms 10836 KB Output is correct
19 Correct 7 ms 10796 KB Output is correct
20 Correct 7 ms 10836 KB Output is correct
21 Correct 10 ms 10896 KB Output is correct
22 Correct 8 ms 10896 KB Output is correct
23 Correct 7 ms 10836 KB Output is correct
24 Correct 10 ms 10836 KB Output is correct
25 Correct 4 ms 9684 KB Output is correct
26 Correct 7 ms 10708 KB Output is correct
27 Correct 7 ms 10764 KB Output is correct
28 Correct 7 ms 10728 KB Output is correct
29 Correct 7 ms 10768 KB Output is correct
30 Correct 7 ms 10580 KB Output is correct
31 Correct 11 ms 10548 KB Output is correct
32 Correct 8 ms 10512 KB Output is correct
33 Correct 10 ms 10584 KB Output is correct
34 Correct 133 ms 57244 KB Output is correct
35 Correct 161 ms 57236 KB Output is correct
36 Correct 132 ms 57312 KB Output is correct
37 Correct 248 ms 57324 KB Output is correct
38 Correct 288 ms 57324 KB Output is correct
39 Correct 252 ms 57320 KB Output is correct
40 Correct 254 ms 57324 KB Output is correct
41 Correct 4 ms 9684 KB Output is correct
42 Correct 5 ms 9684 KB Output is correct
43 Correct 225 ms 45432 KB Output is correct
44 Correct 283 ms 45456 KB Output is correct
45 Correct 240 ms 45464 KB Output is correct
46 Correct 314 ms 51716 KB Output is correct
47 Correct 308 ms 50604 KB Output is correct
48 Correct 299 ms 52256 KB Output is correct
49 Correct 308 ms 50760 KB Output is correct
50 Correct 282 ms 45676 KB Output is correct
51 Correct 284 ms 45704 KB Output is correct
52 Correct 271 ms 45644 KB Output is correct
53 Correct 301 ms 45552 KB Output is correct