답안 #974590

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
974590 2024-05-03T13:37:33 Z Lecorbio Cat Exercise (JOI23_ho_t4) C++17
100 / 100
284 ms 48236 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define sajz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int N = 2e5+7, P = 19;
int n, timer = -1, p[N], d[N], tin[N], tout[N], up[N][P]; 
int par[N], sz[N], mx[N], pos[N]; ll dp[N];
vector<int> g[N];

void dfs(int v, int parent){
	tin[v] = ++timer;
	up[v][0] = parent;
	for (int i=1; i<P; i++) up[v][i] = up[up[v][i-1]][i-1];
	for (auto u : g[v]){ 
		if (u == parent) continue;
		d[u] = d[v] + 1;
		dfs(u, v);
	}
	tout[v] = timer;
}

bool ancestor(int a, int b){return tin[a] <= tin[b] && tout[a] >= tout[b];}
int lca(int a, int b){
	if (ancestor(a, b)) return a;
	for (int i=P-1; i>=0; i--){
		if (!ancestor(up[a][i], b)) a = up[a][i];
	}
	return up[a][0];
}

int Find(int a){return a == par[a] ? a : par[a] = Find(par[a]);}
void Union(int a, int b){
	a = Find(a); b = Find(b);
	if (a != b){
		if (sz[a] < sz[b]) swap(a, b);
		sz[a] += sz[b];
		par[b] = a;
		mx[a] = max(mx[a], mx[b]);
	}
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> n;
    vector<pair<int,int>> sorted;
    for (int i=1; i<=n; i++){ 
		cin >> p[i];
		sz[i] = 1;
		par[i] = i;
		mx[i] = p[i];
		pos[p[i]] = i;
		sorted.push_back({p[i], i});
	}
	sort(all(sorted));
    for (int i=0; i<n-1; i++){
		int a, b; cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	
	dfs(1, 1);
	
	for (auto [val, v] : sorted){
		for (auto u : g[v]){
			if (p[u] < val){
				int x = pos[mx[Find(u)]];
				ll dist = d[v] + d[x] - 2 * d[lca(v, x)];
				dp[v] = max(dp[v], dp[x] + dist);
				Union(v, u);
			}
		}
		if (val == n){cout << dp[v] << '\n'; return 0;}
	}
 
    return 0;
}
/*

4
3 4 1 2
1 2
2 3
3 4

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 14944 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 14944 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 14944 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 5 ms 15388 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 5 ms 15452 KB Output is correct
22 Correct 5 ms 15452 KB Output is correct
23 Correct 5 ms 15452 KB Output is correct
24 Correct 5 ms 15452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 14944 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 5 ms 15388 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 5 ms 15452 KB Output is correct
22 Correct 5 ms 15452 KB Output is correct
23 Correct 5 ms 15452 KB Output is correct
24 Correct 5 ms 15452 KB Output is correct
25 Correct 2 ms 14940 KB Output is correct
26 Correct 5 ms 15280 KB Output is correct
27 Correct 5 ms 15196 KB Output is correct
28 Correct 6 ms 15196 KB Output is correct
29 Correct 5 ms 15196 KB Output is correct
30 Correct 5 ms 15196 KB Output is correct
31 Correct 5 ms 15196 KB Output is correct
32 Correct 7 ms 15120 KB Output is correct
33 Correct 5 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 14944 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 5 ms 15388 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 5 ms 15452 KB Output is correct
22 Correct 5 ms 15452 KB Output is correct
23 Correct 5 ms 15452 KB Output is correct
24 Correct 5 ms 15452 KB Output is correct
25 Correct 120 ms 48168 KB Output is correct
26 Correct 109 ms 48072 KB Output is correct
27 Correct 109 ms 48068 KB Output is correct
28 Correct 170 ms 48072 KB Output is correct
29 Correct 167 ms 48068 KB Output is correct
30 Correct 174 ms 48236 KB Output is correct
31 Correct 163 ms 48108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 148 ms 35568 KB Output is correct
4 Correct 145 ms 35672 KB Output is correct
5 Correct 147 ms 35528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14936 KB Output is correct
2 Correct 4 ms 14936 KB Output is correct
3 Correct 3 ms 15196 KB Output is correct
4 Correct 3 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 14944 KB Output is correct
8 Correct 2 ms 14940 KB Output is correct
9 Correct 2 ms 14940 KB Output is correct
10 Correct 3 ms 15192 KB Output is correct
11 Correct 3 ms 14940 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 3 ms 14936 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 3 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 5 ms 15388 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 5 ms 15452 KB Output is correct
22 Correct 5 ms 15452 KB Output is correct
23 Correct 5 ms 15452 KB Output is correct
24 Correct 5 ms 15452 KB Output is correct
25 Correct 2 ms 14940 KB Output is correct
26 Correct 5 ms 15280 KB Output is correct
27 Correct 5 ms 15196 KB Output is correct
28 Correct 6 ms 15196 KB Output is correct
29 Correct 5 ms 15196 KB Output is correct
30 Correct 5 ms 15196 KB Output is correct
31 Correct 5 ms 15196 KB Output is correct
32 Correct 7 ms 15120 KB Output is correct
33 Correct 5 ms 15196 KB Output is correct
34 Correct 120 ms 48168 KB Output is correct
35 Correct 109 ms 48072 KB Output is correct
36 Correct 109 ms 48068 KB Output is correct
37 Correct 170 ms 48072 KB Output is correct
38 Correct 167 ms 48068 KB Output is correct
39 Correct 174 ms 48236 KB Output is correct
40 Correct 163 ms 48108 KB Output is correct
41 Correct 2 ms 14936 KB Output is correct
42 Correct 3 ms 14940 KB Output is correct
43 Correct 148 ms 35568 KB Output is correct
44 Correct 145 ms 35672 KB Output is correct
45 Correct 147 ms 35528 KB Output is correct
46 Correct 282 ms 42436 KB Output is correct
47 Correct 277 ms 41412 KB Output is correct
48 Correct 284 ms 43096 KB Output is correct
49 Correct 271 ms 41412 KB Output is correct
50 Correct 264 ms 35620 KB Output is correct
51 Correct 216 ms 35672 KB Output is correct
52 Correct 229 ms 35676 KB Output is correct
53 Correct 261 ms 35552 KB Output is correct