#include "closing.h"
#include <iostream>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
vector<vector<pair<int, int>>> pov;
vector<int> distX, distY, cost1, cost2, level, type, q0;
vector<pair<int, int>> q1, q2, q3;
void dfs(int u, int p, int dist, vector<int>& distArray)
{
distArray[u] = dist;
for (pair<int, int> pr : pov[u]) {
int v = pr.first;
int w = pr.second;
if (v == p) { continue; }
dfs(v, u, dist + w, distArray);
}
}
int max_score(int N, int X, int Y, ll K, vector <int> U, vector <int> V, vector <int> W)
{
pov.resize(N);
distX.resize(N);
distY.resize(N);
cost1.resize(N);
cost2.resize(N);
level.resize(N, 0);
type.resize(N, 0);
q0.resize(N);
q1.reserve(2 * N);
q2.reserve(N);
q3.reserve(N);
for (int j = 0; j < N - 1; j++)
{
pov[U[j]].push_back({ V[j], W[j] });
pov[V[j]].push_back({ U[j], W[j] });
}
dfs(X, X, 0, distX);
dfs(Y, Y, 0, distY);
for (int i = 0; i < N; i++)
{
cost1[i] = min(distX[i], distY[i]);
cost2[i] = max(distX[i], distY[i]) - cost1[i];
}
// case 1:
for (int i = 0; i < N; i++) q0[i] = -cost1[i];
sort(q0.begin(), q0.end());
int budget = K;
int ans1 = 0;
while (q0.size() > 0)
{
if (q0.back() + budget >= 0)
{
budget += q0.back();
q0.pop_back();
ans1++;
}
else break;
}
// case 2:
budget = K;
int ans2 = 0;
for (int i = 0; i < N; i++) {
if (distX[i] + distY[i] == distY[X]) {
type[i] = 0;
budget -= cost1[i];
if (budget < 0) { return ans1; }
level[i] = 1;
q1.push_back({ -(cost2[i]), i });
ans2++;
}
else if (cost1[i] > cost2[i]) { //MAYBE >=
type[i] = 1;
q2.push_back({ -(cost1[i] + cost2[i]), i });
q3.push_back({ -cost1[i], i });
}
else {
type[i] = 2;
q1.push_back({ -(cost1[i]), i });
q1.push_back({ -(cost2[i]), i });
}
}
sort(q1.begin(), q1.end());
sort(q2.begin(), q2.end());
sort(q3.begin(), q3.end());
while (!q2.empty()) {
if (q2.back().first + budget < 0) { break; }
if (q1.size() >= 2) {
if (-q2.back().first <= -(q1.back().first + q1[q1.size() - 2].first)) {
budget += q2.back().first;
level[q2.back().second] = 2;
q2.pop_back();
ans2 += 2;
continue;
}
budget += q1.back().first;
level[q1.back().second]++;
q1.pop_back();
ans2 += 1;
continue;
}
budget += q2.back().first;
level[q2.back().second] = 2;
q2.pop_back();
ans2 += 2;
continue;
}
while (!q1.empty()) {
if (q1.back().first + budget < 0) { break; }
budget += q1.back().first;
level[q1.back().second]++;
q1.pop_back();
ans2 += 1;
}
while (!q3.empty()) {
if (q3.back().first + budget < 0) { break; }
if (level[q3.back().second] != 0) {
q3.pop_back();
continue;
}
budget += q3.back().first;
level[q3.back().second]++;
q3.pop_back();
ans2 += 1;
}
return max(ans1, ans2);
}
// int main() {
// cout << max_score(7, 0, 2, 10, { 0, 0, 1, 2, 2, 5 }, { 1, 3, 2, 4, 5, 6 }, { 2, 3, 4, 2, 5, 3 });
// return 0;
// }
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
963 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
30548 KB |
1st lines differ - on the 1st token, expected: '451', found: '1' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Execution timed out |
1098 ms |
348 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Execution timed out |
1098 ms |
348 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
963 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
963 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
963 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
963 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
963 ms |
2097152 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |