답안 #1120040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1120040 2024-11-27T19:47:25 Z stefanneagu Cat Exercise (JOI23_ho_t4) C++17
100 / 100
1175 ms 89308 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int nmax = 2e5 + 1;

struct str {
	int a, i;
} v[nmax];

bool cmp(str x, str y) {
	return x.a < y.a;
}

vector<int> adj[nmax];
set<int> sz[nmax];
int root[nmax], d[nmax], dp[nmax], back[nmax], to[nmax];
int up[nmax][20];

int find(int a) {
	if(root[a] == a) {
		return a;
	}
	return root[a] = find(root[a]);
}

void unite(int a, int b) {
	if(a == b) {
		return;
	}
	if(sz[a].size() < sz[b].size()) {
		swap(a, b);
	}
	for(auto it : sz[b]) {
		sz[a].insert(it);
	}
	sz[b].clear();
	root[b] = a;
}

void dfs(int i, int tata = 0) {
	up[i][0] = tata;
	for(int j = 1; (1 << j) <= d[i]; j++) {
		up[i][j] = up[up[i][j - 1]][j - 1];
	}

	for(auto it : adj[i]) {
		if(it != tata) {
			d[it] = d[i] + 1;
			dfs(it, i);
		}
	}
}

int lift(int a, int nr) {
	for(int i = 0; (1 << i) <= nr; i++) {
		if((1 << i) & nr) {
			a = up[a][i];
		}
	}
	return a;
}

int get_dist(int a, int b) {
	if(d[a] > d[b]) {
		swap(a, b);
	}
	int l = 0, r = d[a], ans = 0;
	while(l <= r) {
		int mid = (l + r) / 2;
		if(l == -1) {
			exit(0);
		}
		//while(true);
		if(lift(a, mid) == lift(b, mid + d[b] - d[a])) {
			ans = mid;
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	return 2 * ans + d[b] - d[a];
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  int n;
  cin >> n;
  for(int i = 1; i <= n; i++) {
		cin >> v[i].a;
		back[v[i].a] = i;
		v[i].i = i;
		root[i] = i;
	}
	sort(v + 1, v + n + 1, cmp);
	for(int i = 1; i <= n; i++) {
		to[v[i].i] = i;
	}
	for(int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1);
	for(int i = 1; i <= n; i++) {
		for(auto it : adj[v[i].i]) {
			if(v[to[it]].a < v[i].a) {
				if(sz[find(it)].size() != 0) {
					int x = back[*sz[find(it)].rbegin()];
					dp[v[i].i] = max(dp[v[i].i], dp[x] + get_dist(v[i].i, x));
					// cout << v[i].i << " " << x << " " << dp[v[i].i] << '\n';
				}
				unite(find(it), find(v[i].i));
			}
		}
		sz[find(v[i].i)].insert(v[i].a);
	}
	cout << dp[back[n]];
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14420 KB Output is correct
2 Correct 11 ms 14676 KB Output is correct
3 Correct 13 ms 14432 KB Output is correct
4 Correct 11 ms 14428 KB Output is correct
5 Correct 11 ms 14420 KB Output is correct
6 Correct 11 ms 14420 KB Output is correct
7 Correct 11 ms 14524 KB Output is correct
8 Correct 10 ms 14420 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14420 KB Output is correct
2 Correct 11 ms 14676 KB Output is correct
3 Correct 13 ms 14432 KB Output is correct
4 Correct 11 ms 14428 KB Output is correct
5 Correct 11 ms 14420 KB Output is correct
6 Correct 11 ms 14420 KB Output is correct
7 Correct 11 ms 14524 KB Output is correct
8 Correct 10 ms 14420 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14676 KB Output is correct
12 Correct 11 ms 14676 KB Output is correct
13 Correct 10 ms 14484 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 11 ms 14676 KB Output is correct
16 Correct 10 ms 14592 KB Output is correct
17 Correct 12 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14420 KB Output is correct
2 Correct 11 ms 14676 KB Output is correct
3 Correct 13 ms 14432 KB Output is correct
4 Correct 11 ms 14428 KB Output is correct
5 Correct 11 ms 14420 KB Output is correct
6 Correct 11 ms 14420 KB Output is correct
7 Correct 11 ms 14524 KB Output is correct
8 Correct 10 ms 14420 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14676 KB Output is correct
12 Correct 11 ms 14676 KB Output is correct
13 Correct 10 ms 14484 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 11 ms 14676 KB Output is correct
16 Correct 10 ms 14592 KB Output is correct
17 Correct 12 ms 14680 KB Output is correct
18 Correct 16 ms 16220 KB Output is correct
19 Correct 15 ms 16220 KB Output is correct
20 Correct 17 ms 16316 KB Output is correct
21 Correct 17 ms 16376 KB Output is correct
22 Correct 17 ms 16508 KB Output is correct
23 Correct 20 ms 16208 KB Output is correct
24 Correct 17 ms 16372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14420 KB Output is correct
2 Correct 11 ms 14676 KB Output is correct
3 Correct 13 ms 14432 KB Output is correct
4 Correct 11 ms 14428 KB Output is correct
5 Correct 11 ms 14420 KB Output is correct
6 Correct 11 ms 14420 KB Output is correct
7 Correct 11 ms 14524 KB Output is correct
8 Correct 10 ms 14420 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14676 KB Output is correct
12 Correct 11 ms 14676 KB Output is correct
13 Correct 10 ms 14484 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 11 ms 14676 KB Output is correct
16 Correct 10 ms 14592 KB Output is correct
17 Correct 12 ms 14680 KB Output is correct
18 Correct 16 ms 16220 KB Output is correct
19 Correct 15 ms 16220 KB Output is correct
20 Correct 17 ms 16316 KB Output is correct
21 Correct 17 ms 16376 KB Output is correct
22 Correct 17 ms 16508 KB Output is correct
23 Correct 20 ms 16208 KB Output is correct
24 Correct 17 ms 16372 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 15 ms 16220 KB Output is correct
27 Correct 15 ms 16212 KB Output is correct
28 Correct 14 ms 16116 KB Output is correct
29 Correct 15 ms 16212 KB Output is correct
30 Correct 15 ms 16008 KB Output is correct
31 Correct 14 ms 16064 KB Output is correct
32 Correct 15 ms 16212 KB Output is correct
33 Correct 17 ms 15956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14420 KB Output is correct
2 Correct 11 ms 14676 KB Output is correct
3 Correct 13 ms 14432 KB Output is correct
4 Correct 11 ms 14428 KB Output is correct
5 Correct 11 ms 14420 KB Output is correct
6 Correct 11 ms 14420 KB Output is correct
7 Correct 11 ms 14524 KB Output is correct
8 Correct 10 ms 14420 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14676 KB Output is correct
12 Correct 11 ms 14676 KB Output is correct
13 Correct 10 ms 14484 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 11 ms 14676 KB Output is correct
16 Correct 10 ms 14592 KB Output is correct
17 Correct 12 ms 14680 KB Output is correct
18 Correct 16 ms 16220 KB Output is correct
19 Correct 15 ms 16220 KB Output is correct
20 Correct 17 ms 16316 KB Output is correct
21 Correct 17 ms 16376 KB Output is correct
22 Correct 17 ms 16508 KB Output is correct
23 Correct 20 ms 16208 KB Output is correct
24 Correct 17 ms 16372 KB Output is correct
25 Correct 465 ms 85608 KB Output is correct
26 Correct 393 ms 85652 KB Output is correct
27 Correct 461 ms 85580 KB Output is correct
28 Correct 1149 ms 87884 KB Output is correct
29 Correct 1111 ms 89308 KB Output is correct
30 Correct 1175 ms 88440 KB Output is correct
31 Correct 1151 ms 89104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14420 KB Output is correct
2 Correct 10 ms 14420 KB Output is correct
3 Correct 388 ms 77504 KB Output is correct
4 Correct 459 ms 77612 KB Output is correct
5 Correct 441 ms 77848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 14420 KB Output is correct
2 Correct 11 ms 14676 KB Output is correct
3 Correct 13 ms 14432 KB Output is correct
4 Correct 11 ms 14428 KB Output is correct
5 Correct 11 ms 14420 KB Output is correct
6 Correct 11 ms 14420 KB Output is correct
7 Correct 11 ms 14524 KB Output is correct
8 Correct 10 ms 14420 KB Output is correct
9 Correct 11 ms 14420 KB Output is correct
10 Correct 11 ms 14420 KB Output is correct
11 Correct 9 ms 14676 KB Output is correct
12 Correct 11 ms 14676 KB Output is correct
13 Correct 10 ms 14484 KB Output is correct
14 Correct 10 ms 14676 KB Output is correct
15 Correct 11 ms 14676 KB Output is correct
16 Correct 10 ms 14592 KB Output is correct
17 Correct 12 ms 14680 KB Output is correct
18 Correct 16 ms 16220 KB Output is correct
19 Correct 15 ms 16220 KB Output is correct
20 Correct 17 ms 16316 KB Output is correct
21 Correct 17 ms 16376 KB Output is correct
22 Correct 17 ms 16508 KB Output is correct
23 Correct 20 ms 16208 KB Output is correct
24 Correct 17 ms 16372 KB Output is correct
25 Correct 10 ms 14420 KB Output is correct
26 Correct 15 ms 16220 KB Output is correct
27 Correct 15 ms 16212 KB Output is correct
28 Correct 14 ms 16116 KB Output is correct
29 Correct 15 ms 16212 KB Output is correct
30 Correct 15 ms 16008 KB Output is correct
31 Correct 14 ms 16064 KB Output is correct
32 Correct 15 ms 16212 KB Output is correct
33 Correct 17 ms 15956 KB Output is correct
34 Correct 465 ms 85608 KB Output is correct
35 Correct 393 ms 85652 KB Output is correct
36 Correct 461 ms 85580 KB Output is correct
37 Correct 1149 ms 87884 KB Output is correct
38 Correct 1111 ms 89308 KB Output is correct
39 Correct 1175 ms 88440 KB Output is correct
40 Correct 1151 ms 89104 KB Output is correct
41 Correct 11 ms 14420 KB Output is correct
42 Correct 10 ms 14420 KB Output is correct
43 Correct 388 ms 77504 KB Output is correct
44 Correct 459 ms 77612 KB Output is correct
45 Correct 441 ms 77848 KB Output is correct
46 Correct 560 ms 83112 KB Output is correct
47 Correct 604 ms 82252 KB Output is correct
48 Correct 616 ms 83360 KB Output is correct
49 Correct 583 ms 82252 KB Output is correct
50 Correct 627 ms 80188 KB Output is correct
51 Correct 671 ms 78164 KB Output is correct
52 Correct 643 ms 79584 KB Output is correct
53 Correct 683 ms 77644 KB Output is correct