Submission #1018220

# Submission time Handle Problem Language Result Execution time Memory
1018220 2024-07-09T16:44:29 Z groshi Cat Exercise (JOI23_ho_t4) C++17
100 / 100
210 ms 51972 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;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5148 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5148 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5148 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6236 KB Output is correct
19 Correct 5 ms 6308 KB Output is correct
20 Correct 5 ms 6236 KB Output is correct
21 Correct 4 ms 6276 KB Output is correct
22 Correct 5 ms 6244 KB Output is correct
23 Correct 4 ms 6200 KB Output is correct
24 Correct 4 ms 6232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5148 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6236 KB Output is correct
19 Correct 5 ms 6308 KB Output is correct
20 Correct 5 ms 6236 KB Output is correct
21 Correct 4 ms 6276 KB Output is correct
22 Correct 5 ms 6244 KB Output is correct
23 Correct 4 ms 6200 KB Output is correct
24 Correct 4 ms 6232 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 5 ms 6060 KB Output is correct
27 Correct 5 ms 6032 KB Output is correct
28 Correct 5 ms 6172 KB Output is correct
29 Correct 5 ms 6236 KB Output is correct
30 Correct 5 ms 5980 KB Output is correct
31 Correct 4 ms 6024 KB Output is correct
32 Correct 4 ms 5980 KB Output is correct
33 Correct 5 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5148 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6236 KB Output is correct
19 Correct 5 ms 6308 KB Output is correct
20 Correct 5 ms 6236 KB Output is correct
21 Correct 4 ms 6276 KB Output is correct
22 Correct 5 ms 6244 KB Output is correct
23 Correct 4 ms 6200 KB Output is correct
24 Correct 4 ms 6232 KB Output is correct
25 Correct 110 ms 51872 KB Output is correct
26 Correct 103 ms 51972 KB Output is correct
27 Correct 98 ms 51912 KB Output is correct
28 Correct 143 ms 51928 KB Output is correct
29 Correct 149 ms 51832 KB Output is correct
30 Correct 150 ms 51872 KB Output is correct
31 Correct 175 ms 51956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 124 ms 39120 KB Output is correct
4 Correct 119 ms 39112 KB Output is correct
5 Correct 116 ms 39112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5212 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5148 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 2 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 3 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 2 ms 5212 KB Output is correct
17 Correct 2 ms 5212 KB Output is correct
18 Correct 4 ms 6236 KB Output is correct
19 Correct 5 ms 6308 KB Output is correct
20 Correct 5 ms 6236 KB Output is correct
21 Correct 4 ms 6276 KB Output is correct
22 Correct 5 ms 6244 KB Output is correct
23 Correct 4 ms 6200 KB Output is correct
24 Correct 4 ms 6232 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 5 ms 6060 KB Output is correct
27 Correct 5 ms 6032 KB Output is correct
28 Correct 5 ms 6172 KB Output is correct
29 Correct 5 ms 6236 KB Output is correct
30 Correct 5 ms 5980 KB Output is correct
31 Correct 4 ms 6024 KB Output is correct
32 Correct 4 ms 5980 KB Output is correct
33 Correct 5 ms 5980 KB Output is correct
34 Correct 110 ms 51872 KB Output is correct
35 Correct 103 ms 51972 KB Output is correct
36 Correct 98 ms 51912 KB Output is correct
37 Correct 143 ms 51928 KB Output is correct
38 Correct 149 ms 51832 KB Output is correct
39 Correct 150 ms 51872 KB Output is correct
40 Correct 175 ms 51956 KB Output is correct
41 Correct 2 ms 5208 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 124 ms 39120 KB Output is correct
44 Correct 119 ms 39112 KB Output is correct
45 Correct 116 ms 39112 KB Output is correct
46 Correct 205 ms 46280 KB Output is correct
47 Correct 210 ms 45236 KB Output is correct
48 Correct 193 ms 46732 KB Output is correct
49 Correct 204 ms 45256 KB Output is correct
50 Correct 176 ms 39368 KB Output is correct
51 Correct 168 ms 39372 KB Output is correct
52 Correct 169 ms 39368 KB Output is correct
53 Correct 151 ms 39368 KB Output is correct