#include "closing.h"
#include "bits/stdc++.h"
#define ll long long
using namespace std;
struct Ed {
ll to, w;
};
bool operator<(const Ed &l, const Ed &r) {
return l.w > r.w;
}
using g_t = vector<vector<Ed>>;
void diDfs(int n, ll d, g_t &g, vector<ll> &di) {
if (di[n] != -1) return;
di[n] = d;
for (auto e: g[n]) {
diDfs(e.to, d + e.w, g, di);
}
}
bool markdfs(int n, int p, int y, g_t &g, vector<bool> &ma) {
if (n == y) return ma[n] = true;
for (auto e: g[n]) {
if (e.to == p) continue;
if (markdfs(e.to, n, y, g, ma)) return ma[n] = true;
}
return false;
}
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W) {
g_t g(N);
for (int i = 0; i < N - 1; ++i) {
g[U[i]].push_back({V[i], W[i]});
g[V[i]].push_back({U[i], W[i]});
}
array<vector<ll>, 2> dist;
dist.fill(vector<ll>(N, -1));
diDfs(X, 0, g, dist[0]);
diDfs(Y, 0, g, dist[1]);
int nuWithout = 0;
vector<bool> vis(N, false);
priority_queue<pair<Ed, bool>> q;
q.push({{X, 0}, false});
q.push({{Y, 0}, true});
ll kC = K;
while (!q.empty()) {
auto [ed, com] = q.top();
q.pop();
if (vis[ed.to]) continue;
if (kC < ed.w) break;
kC -= ed.w;
nuWithout++;
vis[ed.to] = true;
for (auto e: g[ed.to]) {
q.push({{e.to, dist[com][e.to]}, com});
}
}
vector<bool> ma(N, false);
markdfs(X, X, Y, g, ma);
int nuW = 0;
g_t nG(2 * N + 1);
vector<pair<int, int>> pos;
for (int i = 0; i < N; ++i) {
if (ma[i]) {
K -= min(dist[0][i], dist[1][i]);
nuW++;
if (dist[0][i] == dist[1][i]) nuW++;
else pos.push_back({2 * (max(dist[0][i], dist[1][i]) - min(dist[0][i], dist[1][i])),
2 * (max(dist[0][i], dist[1][i]) - min(dist[0][i], dist[1][i]))});
} else {
bool co = dist[0][i] > dist[1][i];
if (dist[co][i] < dist[!co][i] - dist[co][i]) {
pos.push_back({2*dist[co][i], 2*dist[co][i]});
pos.push_back({2*(dist[!co][i] - dist[co][i]), 2*(dist[!co][i] - dist[co][i])});
} else {
pos.push_back({dist[!co][i], 2*dist[co][i]});
pos.push_back({dist[!co][i], 2*(dist[!co][i] - dist[co][i])});
}
}
}
if (K < 0) return nuWithout;
K *= 2;
std::sort(pos.begin(), pos.end());
for (int i = 0; i < pos.size(); ++i) {
if (pos[i].second > K) {
break;
}
K -= pos[i].second;
nuW++;
}
return max(nuW, nuWithout);
}
Compilation message
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:89:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 0; i < pos.size(); ++i) {
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
47928 KB |
Output is correct |
2 |
Correct |
86 ms |
51764 KB |
Output is correct |
3 |
Correct |
46 ms |
5464 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: '31' |
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: '31' |
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: '31' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '31' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '31' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '31' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '31' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '30', found: '31' |
4 |
Halted |
0 ms |
0 KB |
- |