Submission #742983

# Submission time Handle Problem Language Result Execution time Memory
742983 2023-05-17T07:13:25 Z zaneyu Cat Exercise (JOI23_ho_t4) C++17
100 / 100
440 ms 55828 KB
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=5e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-4;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
vector<int> g[maxn];
vector<pii> v;
ll dp[maxn];
int arr[maxn];
int dep[maxn];
struct uf{
	int par[maxn],rank1[maxn],mx[maxn];
	void init(int n){
		REP(i,n) par[i]=i,rank1[i]=1,mx[i]=i;
	}
	int find(int u){
		if(par[u]==u) return u;
		return par[u]=find(par[u]);
	}
	void merge(int a,int b){
		a=find(a),b=find(b);
		if(a==b) return;
		if(rank1[a]>rank1[b]) swap(a,b);
		rank1[b]+=rank1[a];
		par[a]=b;
		if(arr[mx[a]]>arr[mx[b]]) mx[b]=mx[a];
		
	}
}uf;
int par[maxn][20];
void dfs(int u,int p){
	par[u][0]=p;
	for(int x:g[u]){
		if(x==p) continue;
		dep[x]=dep[u]+1;
		dfs(x,u);
	}
}
int lca(int a,int b){
	if(dep[a]<dep[b]) swap(a,b);
	int d=dep[a]-dep[b];
	while(d){
		a=par[a][__lg(lowb(d))];
		d-=lowb(d);
	}
	if(a==b){
		return a;
	}
	for(int j=19;j>=0;j--){
		if(par[a][j]!=par[b][j]){
			a=par[a][j],b=par[b][j];
		}
	}
	return par[a][0];
}
int get(int a,int b){
	return dep[a]+dep[b]-2*dep[lca(a,b)];
}
int main(){
	int n;
	cin>>n;
	
	REP(i,n){
		int x;
		cin>>x;
		--x;
		v.pb({x,i});
		arr[i]=x;
	}
	uf.init(n);
	REP(i,n-1){
		int a,b;
		cin>>a>>b;
		--a,--b;
		g[a].pb(b),g[b].pb(a);
	}
	dfs(0,-1);
	REP1(j,19){
		REP(i,n){
			if(par[i][j-1]!=-1) par[i][j]=par[par[i][j-1]][j-1];
			else par[i][j]=-1;
		}
	}
	sort(ALL(v));
	for(auto x:v){
		for(int a:g[x.s]){
			if(arr[a]>x.f) continue;
			int p=uf.mx[uf.find(a)];
			MXTO(dp[x.s],dp[p]+get(x.s,p));
			uf.merge(a,x.s);
		}
		//cout<<dp[x.s]<<' ';
	}
	cout<<dp[v.back().s];
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 12060 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 6 ms 12092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 12060 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 6 ms 12092 KB Output is correct
11 Correct 7 ms 12048 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 12116 KB Output is correct
14 Correct 7 ms 12144 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 7 ms 12048 KB Output is correct
17 Correct 7 ms 12116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 12060 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 6 ms 12092 KB Output is correct
11 Correct 7 ms 12048 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 12116 KB Output is correct
14 Correct 7 ms 12144 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 7 ms 12048 KB Output is correct
17 Correct 7 ms 12116 KB Output is correct
18 Correct 11 ms 13140 KB Output is correct
19 Correct 11 ms 13140 KB Output is correct
20 Correct 11 ms 13140 KB Output is correct
21 Correct 12 ms 13124 KB Output is correct
22 Correct 13 ms 13120 KB Output is correct
23 Correct 13 ms 13140 KB Output is correct
24 Correct 11 ms 13140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 12060 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 6 ms 12092 KB Output is correct
11 Correct 7 ms 12048 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 12116 KB Output is correct
14 Correct 7 ms 12144 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 7 ms 12048 KB Output is correct
17 Correct 7 ms 12116 KB Output is correct
18 Correct 11 ms 13140 KB Output is correct
19 Correct 11 ms 13140 KB Output is correct
20 Correct 11 ms 13140 KB Output is correct
21 Correct 12 ms 13124 KB Output is correct
22 Correct 13 ms 13120 KB Output is correct
23 Correct 13 ms 13140 KB Output is correct
24 Correct 11 ms 13140 KB Output is correct
25 Correct 6 ms 11988 KB Output is correct
26 Correct 12 ms 13084 KB Output is correct
27 Correct 11 ms 13080 KB Output is correct
28 Correct 13 ms 13012 KB Output is correct
29 Correct 12 ms 13140 KB Output is correct
30 Correct 12 ms 12800 KB Output is correct
31 Correct 12 ms 12832 KB Output is correct
32 Correct 12 ms 12792 KB Output is correct
33 Correct 11 ms 12756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 12060 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 6 ms 12092 KB Output is correct
11 Correct 7 ms 12048 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 12116 KB Output is correct
14 Correct 7 ms 12144 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 7 ms 12048 KB Output is correct
17 Correct 7 ms 12116 KB Output is correct
18 Correct 11 ms 13140 KB Output is correct
19 Correct 11 ms 13140 KB Output is correct
20 Correct 11 ms 13140 KB Output is correct
21 Correct 12 ms 13124 KB Output is correct
22 Correct 13 ms 13120 KB Output is correct
23 Correct 13 ms 13140 KB Output is correct
24 Correct 11 ms 13140 KB Output is correct
25 Correct 260 ms 53832 KB Output is correct
26 Correct 253 ms 55176 KB Output is correct
27 Correct 253 ms 55828 KB Output is correct
28 Correct 287 ms 53800 KB Output is correct
29 Correct 288 ms 53712 KB Output is correct
30 Correct 296 ms 54984 KB Output is correct
31 Correct 291 ms 54128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 12048 KB Output is correct
3 Correct 294 ms 41156 KB Output is correct
4 Correct 321 ms 41340 KB Output is correct
5 Correct 308 ms 42028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 7 ms 12060 KB Output is correct
8 Correct 6 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 6 ms 12092 KB Output is correct
11 Correct 7 ms 12048 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 12116 KB Output is correct
14 Correct 7 ms 12144 KB Output is correct
15 Correct 6 ms 12116 KB Output is correct
16 Correct 7 ms 12048 KB Output is correct
17 Correct 7 ms 12116 KB Output is correct
18 Correct 11 ms 13140 KB Output is correct
19 Correct 11 ms 13140 KB Output is correct
20 Correct 11 ms 13140 KB Output is correct
21 Correct 12 ms 13124 KB Output is correct
22 Correct 13 ms 13120 KB Output is correct
23 Correct 13 ms 13140 KB Output is correct
24 Correct 11 ms 13140 KB Output is correct
25 Correct 6 ms 11988 KB Output is correct
26 Correct 12 ms 13084 KB Output is correct
27 Correct 11 ms 13080 KB Output is correct
28 Correct 13 ms 13012 KB Output is correct
29 Correct 12 ms 13140 KB Output is correct
30 Correct 12 ms 12800 KB Output is correct
31 Correct 12 ms 12832 KB Output is correct
32 Correct 12 ms 12792 KB Output is correct
33 Correct 11 ms 12756 KB Output is correct
34 Correct 260 ms 53832 KB Output is correct
35 Correct 253 ms 55176 KB Output is correct
36 Correct 253 ms 55828 KB Output is correct
37 Correct 287 ms 53800 KB Output is correct
38 Correct 288 ms 53712 KB Output is correct
39 Correct 296 ms 54984 KB Output is correct
40 Correct 291 ms 54128 KB Output is correct
41 Correct 7 ms 11988 KB Output is correct
42 Correct 7 ms 12048 KB Output is correct
43 Correct 294 ms 41156 KB Output is correct
44 Correct 321 ms 41340 KB Output is correct
45 Correct 308 ms 42028 KB Output is correct
46 Correct 418 ms 48720 KB Output is correct
47 Correct 440 ms 47120 KB Output is correct
48 Correct 369 ms 49356 KB Output is correct
49 Correct 400 ms 47636 KB Output is correct
50 Correct 374 ms 43440 KB Output is correct
51 Correct 370 ms 43916 KB Output is correct
52 Correct 369 ms 43944 KB Output is correct
53 Correct 345 ms 41452 KB Output is correct