#include <bits/stdc++.h>
using namespace std;
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
using ll = long long;
using ii = pair<int, int>;
const ll INF = 1e18;
const int MAX_N = 2e5 + 9;
vector<ii> adj[MAX_N];
int n;
void dfs(int u, vector<ll> &dist, int p = -1, ll d = 0) {
dist[u] = d;
for (auto [v, w] : adj[u]) {
if (v != p) dfs(v, dist, u, d + w);
}
}
int non_overlaping(ll K, vector<ll> v) {
sort(all(v));
int ans = 0;
for (int i = 0; i < n; i++) {
if (v[i] <= K) {
K -= v[i];
ans++;
}
}
return ans;
}
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
n = N;
for (int i = 0; i < n; i++) adj[i].clear();
for (int i = 0; i < n - 1; i++) {
adj[U[i]].emplace_back(V[i], W[i]);
adj[V[i]].emplace_back(U[i], W[i]);
}
vector<ll> dist1(n), dist2(n);
dfs(X, dist1), dfs(Y, dist2);
vector<bool> path(n, false);
path[Y] = true;
int u = Y;
while (u != X) {
for (auto [v, w] : adj[u]) {
if (dist1[v] + w == dist1[u]) {
u = v;
break;
}
}
path[u] = true;
}
for (int i = 0; i < n; i++) {
if (dist1[i] > dist2[i]) swap(dist1[i], dist2[i]);
}
vector<ll> vec1;
vector<pair<ll, ll>> vec2;
ll remain = K;
int curr = 0;
for (int i = 0; i < n; i++) {
ll val1 = dist1[i];
ll val2 = dist2[i] - dist1[i];
if (path[i]) {
remain -= val1, curr++;
vec1.push_back(val2);
} else if (val1 <= val2) {
vec1.push_back(val1);
vec1.push_back(val2);
} else {
vec2.emplace_back(val1, val2);
}
}
sort(all(vec1));
sort(all(vec2), [](const pair<ll, ll> &a, const pair<ll, ll> &b) {
return a.first + a.second < b.first + b.second;
});
int M = sz(vec2);
vector<ll> prefSum(M + 1);
prefSum[0] = 0LL;
for (int i = 0; i < M; i++) {
prefSum[i + 1] = vec2[i].first + vec2[i].second;
prefSum[i + 1] += prefSum[i];
}
vector<ll> suffMini(M + 1);
suffMini[M] = INF;
for (int i = M - 1; i >= 0; i--) {
suffMini[i] = vec2[i].first;
suffMini[i] = min(suffMini[i], suffMini[i + 1]);
}
auto calc = [&]() {
if (remain < 0) return int(-1e9);
auto it = upper_bound(all(prefSum), remain);
assert(it != prefSum.begin()); it--;
ll diff = remain - *it;
int idx = int(it - prefSum.begin());
int cant = curr + 2 * idx;
if (diff >= suffMini[idx + 1]) cant++;
return cant;
};
int ans = max(non_overlaping(K, dist1), calc());
for (int i = 0; i < sz(vec1); i++) {
remain -= vec1[i], curr++;
ans = max(ans, calc());
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
105 ms |
30660 KB |
Output is correct |
2 |
Correct |
105 ms |
31736 KB |
Output is correct |
3 |
Correct |
59 ms |
9808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4956 KB |
1st lines differ - on the 1st token, expected: '3', found: '4' |
3 |
Halted |
0 ms |
0 KB |
- |