답안 #974593

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
974593 2024-05-03T13:46:42 Z Lecorbio Cat Exercise (JOI23_ho_t4) C++14
100 / 100
288 ms 48180 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

*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:70:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |  for (auto [val, v] : sorted){
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14944 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14944 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14988 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 14936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14944 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14988 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 14936 KB Output is correct
18 Correct 6 ms 15460 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 6 ms 15452 KB Output is correct
22 Correct 5 ms 15448 KB Output is correct
23 Correct 5 ms 15448 KB Output is correct
24 Correct 5 ms 15452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14944 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14988 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 14936 KB Output is correct
18 Correct 6 ms 15460 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 6 ms 15452 KB Output is correct
22 Correct 5 ms 15448 KB Output is correct
23 Correct 5 ms 15448 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 15196 KB Output is correct
27 Correct 5 ms 15196 KB Output is correct
28 Correct 7 ms 15196 KB Output is correct
29 Correct 5 ms 15196 KB Output is correct
30 Correct 6 ms 15204 KB Output is correct
31 Correct 6 ms 15196 KB Output is correct
32 Correct 5 ms 15208 KB Output is correct
33 Correct 5 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14944 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14988 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 14936 KB Output is correct
18 Correct 6 ms 15460 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 6 ms 15452 KB Output is correct
22 Correct 5 ms 15448 KB Output is correct
23 Correct 5 ms 15448 KB Output is correct
24 Correct 5 ms 15452 KB Output is correct
25 Correct 113 ms 48068 KB Output is correct
26 Correct 115 ms 48068 KB Output is correct
27 Correct 110 ms 48180 KB Output is correct
28 Correct 163 ms 48064 KB Output is correct
29 Correct 165 ms 48068 KB Output is correct
30 Correct 166 ms 48164 KB Output is correct
31 Correct 160 ms 48100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 3 ms 14936 KB Output is correct
3 Correct 144 ms 35672 KB Output is correct
4 Correct 144 ms 35652 KB Output is correct
5 Correct 146 ms 35524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 14940 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 2 ms 14944 KB Output is correct
4 Correct 2 ms 14940 KB Output is correct
5 Correct 3 ms 14940 KB Output is correct
6 Correct 3 ms 14936 KB Output is correct
7 Correct 3 ms 14940 KB Output is correct
8 Correct 3 ms 14936 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14988 KB Output is correct
11 Correct 2 ms 14936 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 3 ms 14940 KB Output is correct
14 Correct 3 ms 14940 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 14936 KB Output is correct
18 Correct 6 ms 15460 KB Output is correct
19 Correct 5 ms 15452 KB Output is correct
20 Correct 5 ms 15452 KB Output is correct
21 Correct 6 ms 15452 KB Output is correct
22 Correct 5 ms 15448 KB Output is correct
23 Correct 5 ms 15448 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 15196 KB Output is correct
27 Correct 5 ms 15196 KB Output is correct
28 Correct 7 ms 15196 KB Output is correct
29 Correct 5 ms 15196 KB Output is correct
30 Correct 6 ms 15204 KB Output is correct
31 Correct 6 ms 15196 KB Output is correct
32 Correct 5 ms 15208 KB Output is correct
33 Correct 5 ms 15196 KB Output is correct
34 Correct 113 ms 48068 KB Output is correct
35 Correct 115 ms 48068 KB Output is correct
36 Correct 110 ms 48180 KB Output is correct
37 Correct 163 ms 48064 KB Output is correct
38 Correct 165 ms 48068 KB Output is correct
39 Correct 166 ms 48164 KB Output is correct
40 Correct 160 ms 48100 KB Output is correct
41 Correct 2 ms 14940 KB Output is correct
42 Correct 3 ms 14936 KB Output is correct
43 Correct 144 ms 35672 KB Output is correct
44 Correct 144 ms 35652 KB Output is correct
45 Correct 146 ms 35524 KB Output is correct
46 Correct 251 ms 42408 KB Output is correct
47 Correct 288 ms 41380 KB Output is correct
48 Correct 244 ms 43072 KB Output is correct
49 Correct 255 ms 41420 KB Output is correct
50 Correct 215 ms 35776 KB Output is correct
51 Correct 217 ms 35676 KB Output is correct
52 Correct 265 ms 35524 KB Output is correct
53 Correct 209 ms 35616 KB Output is correct