답안 #920281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
920281 2024-02-02T11:48:23 Z ting39 Cat Exercise (JOI23_ho_t4) C++17
100 / 100
491 ms 83348 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<vector<int>> g,jump;
vector<int> dep;
struct DSU{
	int n;
	vector<int> to,sz,mx,val;
	DSU(int _n):n(_n){
		to.resize(n);
		sz.resize(n,1);
		val.resize(n);
		mx.resize(n);
		for(int i=0;i<n;i++){
			to[i]=i;
			mx[i]=i;
		}
	}
	int fnd(int p){
		if(to[p]==p) return p;
		to[p]=fnd(to[p]);
		return to[p];
	}
	void conn(int a,int b){
		a=fnd(a);
		b=fnd(b);
		if(a==b) return;
		if(sz[a]>sz[b]) swap(a,b);
		to[a]=b;
		sz[b]+=sz[a];
		mx[b]=max(mx[b],mx[a]);
	}
};
void dfs(int pre,int pos){
	for(int i:g[pos]){
		if(i==pre) continue;
		dep[i]=dep[pos]+1;
		jump[i][0]=pos;
		dfs(pos,i);
	}
}
int lca(int a,int b){
	if(dep[a]<dep[b]) swap(a,b);
	int dif=dep[a]-dep[b],c=0;
	while(dif){
		if(dif&1){
			a=jump[a][c];
		}
		c++;
		dif>>=1;
	}
	if(a==b) return a;
	for(int i=20;i>=0;i--){
		if(jump[a][i]!=jump[b][i]){
			a=jump[a][i];
			b=jump[b][i];
		}
	}
	return jump[a][0];
}
int dis(int a,int b){
	int c=lca(a,b);
	return dep[a]+dep[b]-2*dep[c];
}
signed main(){
	int n;
	cin>>n;
	g.resize(n);
	jump.resize(n,vector<int>(21));
	dep.resize(n);
	vector<vector<int>> gg(n);
	vector<int> v(n);
	for(int &i:v){
		cin>>i;
		i--;
	}
	for(int i=0;i<n-1;i++){
		int a,b;
		cin>>a>>b;
		a--;
		b--;
		a=v[a];
		b=v[b];
		g[a].push_back(b);
		g[b].push_back(a);
		gg[max(a,b)].push_back(min(a,b));
	}
	dfs(0,0);
	for(int j=1;j<21;j++){
		for(int i=0;i<n;i++){
			jump[i][j]=jump[jump[i][j-1]][j-1];
		}
	}
	DSU dsu(n);
	for(int i=0;i<n;i++){
		int tmp=0;
		for(int j:gg[i]){
			int a=dis(i,dsu.mx[dsu.fnd(j)])+dsu.val[dsu.fnd(j)];
			if(a>tmp){
				tmp=a;
			}
			dsu.conn(i,j);
		}
		dsu.val[dsu.fnd(i)]=tmp;
	}
	cout<<dsu.val[dsu.fnd(n-1)]<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 6 ms 2136 KB Output is correct
19 Correct 5 ms 2392 KB Output is correct
20 Correct 5 ms 2396 KB Output is correct
21 Correct 5 ms 2140 KB Output is correct
22 Correct 5 ms 2392 KB Output is correct
23 Correct 5 ms 2140 KB Output is correct
24 Correct 5 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 6 ms 2136 KB Output is correct
19 Correct 5 ms 2392 KB Output is correct
20 Correct 5 ms 2396 KB Output is correct
21 Correct 5 ms 2140 KB Output is correct
22 Correct 5 ms 2392 KB Output is correct
23 Correct 5 ms 2140 KB Output is correct
24 Correct 5 ms 2396 KB Output is correct
25 Correct 1 ms 432 KB Output is correct
26 Correct 5 ms 2396 KB Output is correct
27 Correct 5 ms 2392 KB Output is correct
28 Correct 5 ms 2392 KB Output is correct
29 Correct 5 ms 2392 KB Output is correct
30 Correct 5 ms 2140 KB Output is correct
31 Correct 5 ms 2136 KB Output is correct
32 Correct 5 ms 2136 KB Output is correct
33 Correct 5 ms 2136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 6 ms 2136 KB Output is correct
19 Correct 5 ms 2392 KB Output is correct
20 Correct 5 ms 2396 KB Output is correct
21 Correct 5 ms 2140 KB Output is correct
22 Correct 5 ms 2392 KB Output is correct
23 Correct 5 ms 2140 KB Output is correct
24 Correct 5 ms 2396 KB Output is correct
25 Correct 271 ms 79224 KB Output is correct
26 Correct 308 ms 83348 KB Output is correct
27 Correct 293 ms 83280 KB Output is correct
28 Correct 420 ms 81488 KB Output is correct
29 Correct 451 ms 77672 KB Output is correct
30 Correct 436 ms 80464 KB Output is correct
31 Correct 419 ms 79564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 397 ms 74152 KB Output is correct
4 Correct 400 ms 74092 KB Output is correct
5 Correct 391 ms 73988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 6 ms 2136 KB Output is correct
19 Correct 5 ms 2392 KB Output is correct
20 Correct 5 ms 2396 KB Output is correct
21 Correct 5 ms 2140 KB Output is correct
22 Correct 5 ms 2392 KB Output is correct
23 Correct 5 ms 2140 KB Output is correct
24 Correct 5 ms 2396 KB Output is correct
25 Correct 1 ms 432 KB Output is correct
26 Correct 5 ms 2396 KB Output is correct
27 Correct 5 ms 2392 KB Output is correct
28 Correct 5 ms 2392 KB Output is correct
29 Correct 5 ms 2392 KB Output is correct
30 Correct 5 ms 2140 KB Output is correct
31 Correct 5 ms 2136 KB Output is correct
32 Correct 5 ms 2136 KB Output is correct
33 Correct 5 ms 2136 KB Output is correct
34 Correct 271 ms 79224 KB Output is correct
35 Correct 308 ms 83348 KB Output is correct
36 Correct 293 ms 83280 KB Output is correct
37 Correct 420 ms 81488 KB Output is correct
38 Correct 451 ms 77672 KB Output is correct
39 Correct 436 ms 80464 KB Output is correct
40 Correct 419 ms 79564 KB Output is correct
41 Correct 0 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 397 ms 74152 KB Output is correct
44 Correct 400 ms 74092 KB Output is correct
45 Correct 391 ms 73988 KB Output is correct
46 Correct 357 ms 80964 KB Output is correct
47 Correct 332 ms 80720 KB Output is correct
48 Correct 352 ms 80620 KB Output is correct
49 Correct 327 ms 80612 KB Output is correct
50 Correct 491 ms 73684 KB Output is correct
51 Correct 446 ms 73664 KB Output is correct
52 Correct 462 ms 73808 KB Output is correct
53 Correct 444 ms 73592 KB Output is correct