답안 #911014

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
911014 2024-01-18T11:15:40 Z OAleksa Cat Exercise (JOI23_ho_t4) C++14
31 / 100
2000 ms 63672 KB
#include <bits/stdc++.h>
//ako ovaj vaso daso misli da me pobedjuje.....
using namespace std;
#define int long long
#define f first
#define s second
const int N = 2e5 + 69;
const int K = 18;
int up[N][K], tin[N], tout[N], timer, dep[N];
int n, a[N], vis[N];
vector<int> g[N];
bool anc(int u, int v) {
	return tin[u] <= tin[v] && tout[u] >= tout[v];
}
void dfs(int v, int p) {
	up[v][0] = p;
	tin[v] = ++timer;
	dep[v] = dep[p] + 1;
	for (int i = 1;i < K;i++)
		up[v][i] = up[up[v][i - 1]][i - 1];
	for (auto u : g[v]) {
		if (u == p)
			continue;
		dfs(u, v);
	}
	tout[v] = timer;
}
int Lca(int a, int b) {
	if (anc(a, b))
		return a;
	else if (anc(b, a))
		return b;
	for (int i = K - 1;i >= 0;i--) {
		if (!anc(up[a][i], b) && up[a][i] > 0)
			a = up[a][i];
	}
	return up[a][0];
}
int distance(int a, int b) {
	int lc = Lca(a, b);
	return dep[a] + dep[b] - 2 * dep[lc];
}
pair<int, int> find_mx(int v, int p) {
	if (vis[v]) 
		return {0, 0};
	int mx = a[v], s = v;
	for (auto u : g[v]) {
		if (u == p)
			continue;
		auto x = find_mx(u, v);
		if (x.f > mx) {
			mx = x.f;
			s = x.s;
		}
	}
	return {mx, s};
}
int solve(int x, int p) {
	int v = find_mx(x, 0).s;
	vis[v] = 1;
	int res = 0;
	for (auto u : g[v]) {
		if (u == p || vis[u])
			continue;
		res = max(res, solve(u, v));
	}
	return res + distance(v, p);
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
  	cin >> n;
  	for (int i = 1;i <= n;i++)	
  		cin >> a[i];
  	for (int i = 1;i <= n - 1;i++) {
  		int a, b;
  		cin >> a >> b;
  		g[a].push_back(b);
  		g[b].push_back(a);
  	}
  	int mx = 1;
  	for (int i = 2;i <= n;i++) {
  		if (a[i] > a[mx])
  			mx = i;
  	}
  	dfs(1, 0);
  	cout << solve(mx, mx) << '\n';
	}
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12888 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12940 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12948 KB Output is correct
9 Correct 3 ms 12888 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12888 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12940 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12948 KB Output is correct
9 Correct 3 ms 12888 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12888 KB Output is correct
13 Correct 3 ms 13144 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 4 ms 12888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12888 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12940 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12948 KB Output is correct
9 Correct 3 ms 12888 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12888 KB Output is correct
13 Correct 3 ms 13144 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 4 ms 12888 KB Output is correct
18 Correct 87 ms 13400 KB Output is correct
19 Correct 78 ms 13652 KB Output is correct
20 Correct 75 ms 13404 KB Output is correct
21 Correct 7 ms 13400 KB Output is correct
22 Correct 6 ms 13404 KB Output is correct
23 Correct 5 ms 13456 KB Output is correct
24 Correct 7 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12888 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12940 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12948 KB Output is correct
9 Correct 3 ms 12888 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12888 KB Output is correct
13 Correct 3 ms 13144 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 4 ms 12888 KB Output is correct
18 Correct 87 ms 13400 KB Output is correct
19 Correct 78 ms 13652 KB Output is correct
20 Correct 75 ms 13404 KB Output is correct
21 Correct 7 ms 13400 KB Output is correct
22 Correct 6 ms 13404 KB Output is correct
23 Correct 5 ms 13456 KB Output is correct
24 Correct 7 ms 13404 KB Output is correct
25 Correct 4 ms 12892 KB Output is correct
26 Correct 106 ms 13428 KB Output is correct
27 Correct 104 ms 13436 KB Output is correct
28 Correct 89 ms 13468 KB Output is correct
29 Correct 115 ms 13400 KB Output is correct
30 Correct 13 ms 13144 KB Output is correct
31 Correct 14 ms 13072 KB Output is correct
32 Correct 10 ms 13144 KB Output is correct
33 Correct 14 ms 13252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12888 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12940 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12948 KB Output is correct
9 Correct 3 ms 12888 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12888 KB Output is correct
13 Correct 3 ms 13144 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 4 ms 12888 KB Output is correct
18 Correct 87 ms 13400 KB Output is correct
19 Correct 78 ms 13652 KB Output is correct
20 Correct 75 ms 13404 KB Output is correct
21 Correct 7 ms 13400 KB Output is correct
22 Correct 6 ms 13404 KB Output is correct
23 Correct 5 ms 13456 KB Output is correct
24 Correct 7 ms 13404 KB Output is correct
25 Execution timed out 2032 ms 63672 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12888 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Execution timed out 2072 ms 52696 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 3 ms 12892 KB Output is correct
3 Correct 2 ms 12888 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 4 ms 12940 KB Output is correct
6 Correct 3 ms 12892 KB Output is correct
7 Correct 3 ms 12892 KB Output is correct
8 Correct 3 ms 12948 KB Output is correct
9 Correct 3 ms 12888 KB Output is correct
10 Correct 3 ms 12892 KB Output is correct
11 Correct 4 ms 12892 KB Output is correct
12 Correct 4 ms 12888 KB Output is correct
13 Correct 3 ms 13144 KB Output is correct
14 Correct 4 ms 12892 KB Output is correct
15 Correct 3 ms 12892 KB Output is correct
16 Correct 3 ms 12892 KB Output is correct
17 Correct 4 ms 12888 KB Output is correct
18 Correct 87 ms 13400 KB Output is correct
19 Correct 78 ms 13652 KB Output is correct
20 Correct 75 ms 13404 KB Output is correct
21 Correct 7 ms 13400 KB Output is correct
22 Correct 6 ms 13404 KB Output is correct
23 Correct 5 ms 13456 KB Output is correct
24 Correct 7 ms 13404 KB Output is correct
25 Correct 4 ms 12892 KB Output is correct
26 Correct 106 ms 13428 KB Output is correct
27 Correct 104 ms 13436 KB Output is correct
28 Correct 89 ms 13468 KB Output is correct
29 Correct 115 ms 13400 KB Output is correct
30 Correct 13 ms 13144 KB Output is correct
31 Correct 14 ms 13072 KB Output is correct
32 Correct 10 ms 13144 KB Output is correct
33 Correct 14 ms 13252 KB Output is correct
34 Execution timed out 2032 ms 63672 KB Time limit exceeded
35 Halted 0 ms 0 KB -