답안 #1067514

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067514 2024-08-20T19:02:33 Z _rain_ Cat Exercise (JOI23_ho_t4) C++14
100 / 100
199 ms 68280 KB
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define debug false
#define int long long
const int maxn = 2e5;
const int maxlog = 20;
vector<int> g[maxn+2];
int dp[maxn+2] , p[maxn+2] , n , dep[maxn+2] , par[maxn+2][maxlog+2];

void dfs(int u , int p){
	par[u][0] = p;
	dep[u] = dep[p] + 1;
	for (int i = 1; i <= maxlog; ++i){
		par[u][i] = par[par[u][i - 1]][i - 1];
	}
	for (auto& v : g[u]){
		if (v==p) continue;
		dfs(v,u);
	}
	return;
}
int lca(int u , int v){
	if (dep[u] < dep[v]) swap(u,v);
	for (int i = maxlog; i >= 0; --i)
		if (dep[par[u][i]] >= dep[v]) u = par[u][i];
	if (u==v) return u;
	for (int i = maxlog; i >= 0; --i)
		if (par[u][i] != par[v][i]) u = par[u][i] , v = par[v][i];
	return par[u][0];
}
int dist(int u , int v){
	return dep[u] + dep[v] - 2 * dep[lca(u,v)];
}

int f[maxn+2] , sz[maxn+2] , mx[maxn+2];
int find(int u){
	return u == f[u] ? f[u] : f[u] = find(f[u]);
}
void unite(int u, int v){
	u = find(u); v = find(v);
	if (u==v) return ;
	if (sz[u] < sz[v]) swap(u,v);
	mx[u] = max(mx[u] , mx[v]);
	f[v] = u; sz[u] += sz[v];
	return;
}

int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	for (int i = 1; i <= n; ++i) sz[i] = 1 , f[i] = i , mx[i] = i;
	for (int i = 1; i <= n; ++i) cin >> p[i];
	for (int i = 1; i < n; ++i){
		int u , v; cin >> u >> v;
		g[p[u]].push_back(p[v]);
		g[p[v]].push_back(p[u]);
	}
	dfs(n,0);
	for (int i = 1; i <= n; ++i){
		for (auto& v : g[i]){
			int x = mx[find(v)];
			if (x < i){
				dp[i] = max(dp[i] , dp[x] + dist(x , i));
				unite(x,i);
			}
		}
	}
	cout << dp[n];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 2 ms 13916 KB Output is correct
8 Correct 2 ms 13916 KB Output is correct
9 Correct 2 ms 13916 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 2 ms 13916 KB Output is correct
8 Correct 2 ms 13916 KB Output is correct
9 Correct 2 ms 13916 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 2 ms 13916 KB Output is correct
12 Correct 2 ms 13912 KB Output is correct
13 Correct 2 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 2 ms 13916 KB Output is correct
16 Correct 2 ms 13916 KB Output is correct
17 Correct 2 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 2 ms 13916 KB Output is correct
8 Correct 2 ms 13916 KB Output is correct
9 Correct 2 ms 13916 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 2 ms 13916 KB Output is correct
12 Correct 2 ms 13912 KB Output is correct
13 Correct 2 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 2 ms 13916 KB Output is correct
16 Correct 2 ms 13916 KB Output is correct
17 Correct 2 ms 13916 KB Output is correct
18 Correct 4 ms 14428 KB Output is correct
19 Correct 4 ms 14428 KB Output is correct
20 Correct 4 ms 14428 KB Output is correct
21 Correct 4 ms 14172 KB Output is correct
22 Correct 4 ms 14168 KB Output is correct
23 Correct 4 ms 14172 KB Output is correct
24 Correct 4 ms 14172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 2 ms 13916 KB Output is correct
8 Correct 2 ms 13916 KB Output is correct
9 Correct 2 ms 13916 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 2 ms 13916 KB Output is correct
12 Correct 2 ms 13912 KB Output is correct
13 Correct 2 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 2 ms 13916 KB Output is correct
16 Correct 2 ms 13916 KB Output is correct
17 Correct 2 ms 13916 KB Output is correct
18 Correct 4 ms 14428 KB Output is correct
19 Correct 4 ms 14428 KB Output is correct
20 Correct 4 ms 14428 KB Output is correct
21 Correct 4 ms 14172 KB Output is correct
22 Correct 4 ms 14168 KB Output is correct
23 Correct 4 ms 14172 KB Output is correct
24 Correct 4 ms 14172 KB Output is correct
25 Correct 2 ms 13916 KB Output is correct
26 Correct 4 ms 14308 KB Output is correct
27 Correct 4 ms 14172 KB Output is correct
28 Correct 4 ms 14360 KB Output is correct
29 Correct 4 ms 14172 KB Output is correct
30 Correct 4 ms 14172 KB Output is correct
31 Correct 4 ms 14172 KB Output is correct
32 Correct 4 ms 14172 KB Output is correct
33 Correct 4 ms 14224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 2 ms 13916 KB Output is correct
8 Correct 2 ms 13916 KB Output is correct
9 Correct 2 ms 13916 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 2 ms 13916 KB Output is correct
12 Correct 2 ms 13912 KB Output is correct
13 Correct 2 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 2 ms 13916 KB Output is correct
16 Correct 2 ms 13916 KB Output is correct
17 Correct 2 ms 13916 KB Output is correct
18 Correct 4 ms 14428 KB Output is correct
19 Correct 4 ms 14428 KB Output is correct
20 Correct 4 ms 14428 KB Output is correct
21 Correct 4 ms 14172 KB Output is correct
22 Correct 4 ms 14168 KB Output is correct
23 Correct 4 ms 14172 KB Output is correct
24 Correct 4 ms 14172 KB Output is correct
25 Correct 132 ms 64528 KB Output is correct
26 Correct 119 ms 68280 KB Output is correct
27 Correct 107 ms 68176 KB Output is correct
28 Correct 171 ms 66048 KB Output is correct
29 Correct 190 ms 67156 KB Output is correct
30 Correct 178 ms 66896 KB Output is correct
31 Correct 176 ms 67412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13912 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 154 ms 56568 KB Output is correct
4 Correct 154 ms 56656 KB Output is correct
5 Correct 156 ms 56800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13916 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 2 ms 13916 KB Output is correct
8 Correct 2 ms 13916 KB Output is correct
9 Correct 2 ms 13916 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 2 ms 13916 KB Output is correct
12 Correct 2 ms 13912 KB Output is correct
13 Correct 2 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 2 ms 13916 KB Output is correct
16 Correct 2 ms 13916 KB Output is correct
17 Correct 2 ms 13916 KB Output is correct
18 Correct 4 ms 14428 KB Output is correct
19 Correct 4 ms 14428 KB Output is correct
20 Correct 4 ms 14428 KB Output is correct
21 Correct 4 ms 14172 KB Output is correct
22 Correct 4 ms 14168 KB Output is correct
23 Correct 4 ms 14172 KB Output is correct
24 Correct 4 ms 14172 KB Output is correct
25 Correct 2 ms 13916 KB Output is correct
26 Correct 4 ms 14308 KB Output is correct
27 Correct 4 ms 14172 KB Output is correct
28 Correct 4 ms 14360 KB Output is correct
29 Correct 4 ms 14172 KB Output is correct
30 Correct 4 ms 14172 KB Output is correct
31 Correct 4 ms 14172 KB Output is correct
32 Correct 4 ms 14172 KB Output is correct
33 Correct 4 ms 14224 KB Output is correct
34 Correct 132 ms 64528 KB Output is correct
35 Correct 119 ms 68280 KB Output is correct
36 Correct 107 ms 68176 KB Output is correct
37 Correct 171 ms 66048 KB Output is correct
38 Correct 190 ms 67156 KB Output is correct
39 Correct 178 ms 66896 KB Output is correct
40 Correct 176 ms 67412 KB Output is correct
41 Correct 2 ms 13912 KB Output is correct
42 Correct 2 ms 13916 KB Output is correct
43 Correct 154 ms 56568 KB Output is correct
44 Correct 154 ms 56656 KB Output is correct
45 Correct 156 ms 56800 KB Output is correct
46 Correct 146 ms 65876 KB Output is correct
47 Correct 141 ms 65916 KB Output is correct
48 Correct 138 ms 66900 KB Output is correct
49 Correct 131 ms 65944 KB Output is correct
50 Correct 188 ms 59988 KB Output is correct
51 Correct 175 ms 60036 KB Output is correct
52 Correct 178 ms 59992 KB Output is correct
53 Correct 199 ms 59988 KB Output is correct