/* Ignut
started: 13.08.2024
now: 13.08.2024
████████████████████████████████████████████████████████████████████
████████████████████████████████ ████████████████████████████████
██████████████████████████████ ██████████████████████████████
██████ ██████████████████ ██████████████████ ██████
██████ ██████████████ ██████████████ ██████
██████ ██ ████████████ ████████████ ██ ██████
██████ ████ ██████████ ██████████ ████ ██████
██████ ████ ██████████ ██████████ ████ ██████
██████ ████ ██████████ ██████████ ██████ ██████
██████ ██████ ██████████ ██████████ ██████ ██████
██████ ██████ ████████ ████████ ██████ ██████
██████ ██████ ██████ ██████ ██████ ██████
██████ ████ ████ ████ ████ ██████
██████ ██████████ ████ ██████████ ██████
██████ ██ ██████ ████████ ██████ ██ ██████
██████ ██████ ████████ ██████ ██████
██████ ██ ██ ██████
██████████████████████ ████ ████ ██████████████████████
████████████████████████ ██ ██ ████████████████████████
██████████████████████████ ██████████████████████████
██████████████████████████████ ██████████████████████████████
████████████████████████████████████████████████████████████████████
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 2e5 + 123;
vector<pair<int, int>> tree[MAXN];
vector<int> order;
int pos[MAXN];
void dfs(int v, int par) {
order.push_back(v);
for (auto [to, w] : tree[v])
if (to != par)
dfs(to, v);
}
ll dx[MAXN], dy[MAXN];
void dfs2(int v, int par, ll d, bool f) {
if (f) dx[v] = d;
else dy[v] = d;
for (auto [to, w] : tree[v])
if (to != par)
dfs2(to, v, d + w, f);
}
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
order.clear();
for (int i = 0; i < N; i ++)
tree[i].clear();
for (int i = 0; i < N - 1; i ++) {
tree[U[i]].push_back({V[i], W[i]});
tree[V[i]].push_back({U[i], W[i]});
}
int start = 0;
for (int i = 0; i < N; i ++) {
if (tree[i].size() == 1) {
start = i; break;
}
}
dfs(start, -1);
for (int i = 0; i < N; i ++) pos[order[i]] = i;
dfs2(X, -1, 0, true);
dfs2(Y, -1, 0, false);
if (pos[X] > pos[Y]) {
swap(X, Y);
for (int i = 0; i < N; i ++) swap(dx[i], dy[i]);
}
ll sumX[N + 1] = {};
ll sumY[N + 1] = {};
ll sum2[N + 1] = {};
for (int i = 0; i < N; i ++) {
sumX[i + 1] = sumX[i] + dx[order[i]];
sumY[i + 1] = sumY[i] + dy[order[i]];
sum2[i + 1] = sum2[i] + max(dx[order[i]], dy[order[i]]);
}
int res = 0;
for (int l = 0; l < N; l ++) {
for (int r = l; r < N; r ++) {
ll sum = 0;
int ans = (r - l + 1) * 2;
sum = sum2[r + 1] - sum2[l];
if (pos[X] < l) {
ans += l - pos[X];
sum += sumX[l] - sumX[pos[X]];
}
if (pos[Y] > r) {
ans += pos[Y] - r;
sum += sumY[pos[Y] + 1] - sumY[r + 1];
}
int L = min(pos[X], l);
int R = max(pos[Y], r);
for (int i = 0; i <= L; i ++) {
ll sx = sumX[L] - sumX[i];
int cx = L - i;
if (sum + sx <= K) res = max(res, ans + cx);
int lo = R, hi = N - 1;
while (lo < hi) {
int mid = lo + (hi - lo + 1) / 2;
ll sy = sumY[mid + 1] - sumY[R + 1];
if (sum + sx + sy <= K)
lo = mid;
else
hi = mid - 1;
}
ll sy = sumY[lo + 1] - sumY[R + 1];
int cy = lo - R;
if (sum + sx + sy <= K) res = max(res, ans + cx + cy);
}
// vector<ll> ds;
// for (int i = 0; i < L; i ++) ds.push_back(dx[order[i]]);
// for (int i = R + 1; i < N; i ++) ds.push_back(dy[order[i]]);
// sort(ds.begin(), ds.end());
// for (int i = 0; i < ds.size(); i ++) {
// if (sum + ds[i] <= K) {
// sum += ds[i];
// ans ++;
// }
// }
if (sum <= K)
res = max(res, ans);
}
}
// no overlap
vector<ll> ds;
for (int i = 0; i < N; i ++) {
ds.push_back(dx[i]);
ds.push_back(dy[i]);
}
sort(ds.begin(), ds.end());
ll sum = 0;
int ans = 0;
for (int i = 0; i < ds.size(); i ++) {
if (sum + ds[i] <= K) {
sum += ds[i];
ans ++;
}
}
res = max(res, ans);
return res;
}
Compilation message
closing.cpp: In function 'int max_score(int, int, int, ll, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:156:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | for (int i = 0; i < ds.size(); i ++) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1062 ms |
26568 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8832 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8888 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8832 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8888 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8884 KB |
Output is correct |
19 |
Correct |
169 ms |
8920 KB |
Output is correct |
20 |
Correct |
27 ms |
8796 KB |
Output is correct |
21 |
Correct |
77 ms |
8924 KB |
Output is correct |
22 |
Correct |
8 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8948 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8832 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8888 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8884 KB |
Output is correct |
19 |
Correct |
169 ms |
8920 KB |
Output is correct |
20 |
Correct |
27 ms |
8796 KB |
Output is correct |
21 |
Correct |
77 ms |
8924 KB |
Output is correct |
22 |
Correct |
8 ms |
8796 KB |
Output is correct |
23 |
Correct |
2 ms |
8948 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
7 ms |
8796 KB |
Output is correct |
26 |
Execution timed out |
1091 ms |
9052 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8832 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Incorrect |
1 ms |
8796 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8832 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8888 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Incorrect |
1 ms |
8796 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8832 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8888 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8884 KB |
Output is correct |
20 |
Correct |
169 ms |
8920 KB |
Output is correct |
21 |
Correct |
27 ms |
8796 KB |
Output is correct |
22 |
Correct |
77 ms |
8924 KB |
Output is correct |
23 |
Correct |
8 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8948 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Incorrect |
1 ms |
8796 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8832 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8888 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8884 KB |
Output is correct |
20 |
Correct |
169 ms |
8920 KB |
Output is correct |
21 |
Correct |
27 ms |
8796 KB |
Output is correct |
22 |
Correct |
77 ms |
8924 KB |
Output is correct |
23 |
Correct |
8 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8948 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
7 ms |
8796 KB |
Output is correct |
27 |
Execution timed out |
1091 ms |
9052 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8832 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8888 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
1 ms |
8796 KB |
Output is correct |
17 |
Correct |
1 ms |
8796 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
1 ms |
8884 KB |
Output is correct |
20 |
Correct |
169 ms |
8920 KB |
Output is correct |
21 |
Correct |
27 ms |
8796 KB |
Output is correct |
22 |
Correct |
77 ms |
8924 KB |
Output is correct |
23 |
Correct |
8 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8948 KB |
Output is correct |
25 |
Correct |
2 ms |
8796 KB |
Output is correct |
26 |
Correct |
7 ms |
8796 KB |
Output is correct |
27 |
Execution timed out |
1091 ms |
9052 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |