답안 #946352

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
946352 2024-03-14T14:18:45 Z djs100201 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
235 ms 64772 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using PP = pair<ll, P>;
const ll n_ =2e5+100, inf = (ll)2e9 * (ll)1e9 + 7, mod = 998244353;
ll n, m, tc = 1, a, b, c, d, sum, x, y, z, base, ans, k;
ll gcd(ll x,ll y){
	if(!y)return x;
	return gcd(y,x%y);
}
vector<ll>v[n_];
ll arr[n_],parent[n_][21],dep[n_],par[n_],dp[n_];
ll find(ll x){
	if(par[x]<0)return x;
	return par[x]=find(par[x]);
}
void merge(ll x,ll y){
	x=find(x),y=find(y);
	if(x==y)return;
	par[x]=y;
}
void dfs(int x, int par) {
	dep[x] = dep[par] + 1;
	for (int nxt : v[x]) {
		if (nxt == par)continue;
		parent[nxt][0] = x;
		dfs(nxt, x);
	}
}
int lca(int x, int y) {
	if (dep[x] > dep[y])swap(x, y);//항상 y의 depth가 더 깊게 만들어주자!
	for (int i = 20; i >= 0; i--)
		if (dep[y] - dep[x] >= (1 << i))
			y = parent[y][i];
	if (x == y)return x;
	for (int i = 20; i >= 0; i--) {
		if (parent[x][i] != parent[y][i]) {
			x = parent[x][i];
			y = parent[y][i];
		}
	}
	return parent[x][0];
}
ll dist(ll x,ll y){
	ll z=lca(x,y);
	return dep[x]-dep[z]+dep[y]-dep[z];
}
void solve(){
	cin>>n;
	vector<P>A;
	for(int i=1;i<=n;i++)cin>>arr[i],A.push_back({arr[i],i});
	sort(all(A));
	for(int i=1;i<n;i++){
		cin>>a>>b;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	dfs(1,0);
	for (int i = 1; i < 21; i++)
		for (int j = 1; j <= n; j++)
			parent[j][i] = parent[parent[j][i - 1]][i - 1];
	memset(par,-1,sizeof(par));
	for(auto [a,b]:A){
		ll ret=0;
		for(auto nxt:v[b]){
			if(arr[nxt]>a)continue;
			x=find(nxt);
			ret=max(ret,dp[x]+dist(x,b));
			merge(x,b);
		}
		dp[b]=ret;
		//cout<<b<<' '<<dp[b]<<endl;
	}
	cout<<dp[A.back().second]<<endl;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);  
	//cin >> tc;
	while (tc--)solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12120 KB Output is correct
2 Correct 2 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 2 ms 12376 KB Output is correct
5 Correct 2 ms 12064 KB Output is correct
6 Correct 3 ms 12120 KB Output is correct
7 Correct 2 ms 12120 KB Output is correct
8 Correct 2 ms 12120 KB Output is correct
9 Correct 2 ms 12120 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12120 KB Output is correct
2 Correct 2 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 2 ms 12376 KB Output is correct
5 Correct 2 ms 12064 KB Output is correct
6 Correct 3 ms 12120 KB Output is correct
7 Correct 2 ms 12120 KB Output is correct
8 Correct 2 ms 12120 KB Output is correct
9 Correct 2 ms 12120 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 2 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12064 KB Output is correct
14 Correct 2 ms 12124 KB Output is correct
15 Correct 3 ms 12128 KB Output is correct
16 Correct 3 ms 12076 KB Output is correct
17 Correct 3 ms 12124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12120 KB Output is correct
2 Correct 2 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 2 ms 12376 KB Output is correct
5 Correct 2 ms 12064 KB Output is correct
6 Correct 3 ms 12120 KB Output is correct
7 Correct 2 ms 12120 KB Output is correct
8 Correct 2 ms 12120 KB Output is correct
9 Correct 2 ms 12120 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 2 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12064 KB Output is correct
14 Correct 2 ms 12124 KB Output is correct
15 Correct 3 ms 12128 KB Output is correct
16 Correct 3 ms 12076 KB Output is correct
17 Correct 3 ms 12124 KB Output is correct
18 Correct 5 ms 12636 KB Output is correct
19 Correct 4 ms 12636 KB Output is correct
20 Correct 4 ms 12636 KB Output is correct
21 Correct 5 ms 12636 KB Output is correct
22 Correct 5 ms 12636 KB Output is correct
23 Correct 5 ms 12636 KB Output is correct
24 Correct 5 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12120 KB Output is correct
2 Correct 2 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 2 ms 12376 KB Output is correct
5 Correct 2 ms 12064 KB Output is correct
6 Correct 3 ms 12120 KB Output is correct
7 Correct 2 ms 12120 KB Output is correct
8 Correct 2 ms 12120 KB Output is correct
9 Correct 2 ms 12120 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 2 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12064 KB Output is correct
14 Correct 2 ms 12124 KB Output is correct
15 Correct 3 ms 12128 KB Output is correct
16 Correct 3 ms 12076 KB Output is correct
17 Correct 3 ms 12124 KB Output is correct
18 Correct 5 ms 12636 KB Output is correct
19 Correct 4 ms 12636 KB Output is correct
20 Correct 4 ms 12636 KB Output is correct
21 Correct 5 ms 12636 KB Output is correct
22 Correct 5 ms 12636 KB Output is correct
23 Correct 5 ms 12636 KB Output is correct
24 Correct 5 ms 12636 KB Output is correct
25 Correct 3 ms 12112 KB Output is correct
26 Correct 5 ms 12632 KB Output is correct
27 Correct 5 ms 12636 KB Output is correct
28 Correct 5 ms 12636 KB Output is correct
29 Correct 5 ms 12636 KB Output is correct
30 Correct 6 ms 12632 KB Output is correct
31 Correct 5 ms 12636 KB Output is correct
32 Correct 5 ms 12632 KB Output is correct
33 Correct 5 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12120 KB Output is correct
2 Correct 2 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 2 ms 12376 KB Output is correct
5 Correct 2 ms 12064 KB Output is correct
6 Correct 3 ms 12120 KB Output is correct
7 Correct 2 ms 12120 KB Output is correct
8 Correct 2 ms 12120 KB Output is correct
9 Correct 2 ms 12120 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 2 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12064 KB Output is correct
14 Correct 2 ms 12124 KB Output is correct
15 Correct 3 ms 12128 KB Output is correct
16 Correct 3 ms 12076 KB Output is correct
17 Correct 3 ms 12124 KB Output is correct
18 Correct 5 ms 12636 KB Output is correct
19 Correct 4 ms 12636 KB Output is correct
20 Correct 4 ms 12636 KB Output is correct
21 Correct 5 ms 12636 KB Output is correct
22 Correct 5 ms 12636 KB Output is correct
23 Correct 5 ms 12636 KB Output is correct
24 Correct 5 ms 12636 KB Output is correct
25 Correct 124 ms 64480 KB Output is correct
26 Correct 112 ms 63936 KB Output is correct
27 Correct 113 ms 64772 KB Output is correct
28 Correct 150 ms 63936 KB Output is correct
29 Correct 161 ms 64036 KB Output is correct
30 Correct 152 ms 63932 KB Output is correct
31 Correct 149 ms 63852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12124 KB Output is correct
2 Correct 2 ms 12124 KB Output is correct
3 Correct 184 ms 58992 KB Output is correct
4 Correct 171 ms 59456 KB Output is correct
5 Correct 160 ms 58724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12120 KB Output is correct
2 Correct 2 ms 12124 KB Output is correct
3 Correct 2 ms 12124 KB Output is correct
4 Correct 2 ms 12376 KB Output is correct
5 Correct 2 ms 12064 KB Output is correct
6 Correct 3 ms 12120 KB Output is correct
7 Correct 2 ms 12120 KB Output is correct
8 Correct 2 ms 12120 KB Output is correct
9 Correct 2 ms 12120 KB Output is correct
10 Correct 3 ms 12124 KB Output is correct
11 Correct 2 ms 12120 KB Output is correct
12 Correct 3 ms 12124 KB Output is correct
13 Correct 3 ms 12064 KB Output is correct
14 Correct 2 ms 12124 KB Output is correct
15 Correct 3 ms 12128 KB Output is correct
16 Correct 3 ms 12076 KB Output is correct
17 Correct 3 ms 12124 KB Output is correct
18 Correct 5 ms 12636 KB Output is correct
19 Correct 4 ms 12636 KB Output is correct
20 Correct 4 ms 12636 KB Output is correct
21 Correct 5 ms 12636 KB Output is correct
22 Correct 5 ms 12636 KB Output is correct
23 Correct 5 ms 12636 KB Output is correct
24 Correct 5 ms 12636 KB Output is correct
25 Correct 3 ms 12112 KB Output is correct
26 Correct 5 ms 12632 KB Output is correct
27 Correct 5 ms 12636 KB Output is correct
28 Correct 5 ms 12636 KB Output is correct
29 Correct 5 ms 12636 KB Output is correct
30 Correct 6 ms 12632 KB Output is correct
31 Correct 5 ms 12636 KB Output is correct
32 Correct 5 ms 12632 KB Output is correct
33 Correct 5 ms 12636 KB Output is correct
34 Correct 124 ms 64480 KB Output is correct
35 Correct 112 ms 63936 KB Output is correct
36 Correct 113 ms 64772 KB Output is correct
37 Correct 150 ms 63936 KB Output is correct
38 Correct 161 ms 64036 KB Output is correct
39 Correct 152 ms 63932 KB Output is correct
40 Correct 149 ms 63852 KB Output is correct
41 Correct 2 ms 12124 KB Output is correct
42 Correct 2 ms 12124 KB Output is correct
43 Correct 184 ms 58992 KB Output is correct
44 Correct 171 ms 59456 KB Output is correct
45 Correct 160 ms 58724 KB Output is correct
46 Correct 204 ms 61536 KB Output is correct
47 Correct 205 ms 60860 KB Output is correct
48 Correct 204 ms 62112 KB Output is correct
49 Correct 194 ms 61116 KB Output is correct
50 Correct 213 ms 58656 KB Output is correct
51 Correct 201 ms 58648 KB Output is correct
52 Correct 201 ms 58460 KB Output is correct
53 Correct 235 ms 58412 KB Output is correct