#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<pair<int,ll>>>;
void dfs(int u, int fu, const Graph& g, vector<ll>& dists) {
for (auto [v, w] : g[u]) {
if (v == fu) continue;
assert(dists[v] < 0);
dists[v] = dists[u] + w;
dfs(v, u, g, dists);
}
}
vector<ll> get_dists(int u, const Graph& g) {
int n = g.size();
vector<ll> dists(n, -1);
dists[u] = 0;
dfs(u, -1, g, dists);
return dists;
}
int no_common(ll k, const vector<ll>& dx, const vector<ll>& dy) {
vector<ll> all;
for (auto d : dx) all.push_back(d);
for (auto d : dy) all.push_back(d);
sort(all.begin(), all.end());
int cnt = 0;
for (auto d : all) {
if (d <= k) {
k -= d;
++cnt;
} else break;
}
return cnt;
}
// linear graph
struct Event {
ll cost;
int i;
int typ; // 1 = SINGLE, 2 = PAIR
};
bool operator < (const Event& a, const Event& b) {
return a.cost * (3 - a.typ) < b.cost * (3 - b.typ);
}
int sub4(int n, ll k, int x, int y,
const vector<ll>& dx, const vector<ll>& dy) { // x < y
int res = no_common(k, dx, dy);
// 0 .. [xl .. x .. xr]
// [yl .. y .. yr] .. n-1
// Now we only consider the case where [xl, xr] and [yl, yr] overlap
// They must overlap at least one point between [x, y]
int cur = 0;
set<Event> events;
for (int i = 0; i < n; ++i) {
auto a = min(dx[i], dy[i]);
auto b = max(dx[i], dy[i]);
if (x <= i && i <= y) {
k -= a;
++cur;
events.insert(Event{b - a, i, 2});
} else {
events.insert(Event{a, i, 1});
}
}
if (k >= 0) {
while (!events.empty()) {
Event event = *events.begin();
events.erase(events.begin());
if (event.cost > k) break;
k -= event.cost;
if (event.typ == 1) {
auto a = min(dx[event.i], dy[event.i]);
auto b = max(dx[event.i], dy[event.i]);
events.insert(Event{b - a, event.i, 2});
}
}
res = max(res, cur);
}
return res;
}
int max_score(int n, int X, int Y, long long K,
vector<int> U, vector<int> V, vector<int> W) {
Graph g(n);
assert(int(U.size()) == n-1);
assert(int(V.size()) == n-1);
assert(int(W.size()) == n-1);
for (int i = 0; i < n - 1; i++) {
int u = U[i];
int v = V[i];
int w = W[i];
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
if (X > Y) swap(X, Y);
auto distsX = get_dists(X, g);
auto distsY = get_dists(Y, g);
return sub4(n, K, X, Y, distsX, distsY);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
40828 KB |
Output is correct |
2 |
Correct |
218 ms |
46716 KB |
Output is correct |
3 |
Correct |
98 ms |
3052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '24' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '24' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '24' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |