#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
vector<vector<pair<int, int>>> G;
vector<int> ban;
void dfs_calc_dist(int v, int par, long long d, vector<long long> &dist) {
dist[v] = d;
for (auto i : G[v]) {
if (i.first != par) {
dfs_calc_dist(i.first, v, d + i.second, dist);
}
}
}
void dfs_find_par(int v, int p, vector<int> &par) {
par[v] = p;
for (auto i : G[v]) {
if (i.first != p) {
dfs_find_par(i.first, v, par);
}
}
}
void init(int n) {
G.clear();
ban.clear();;
G.resize(n);
ban.resize(n);
}
int max_score(int n, int x, int y, long long k,
vector<int> u, vector<int> v, vector<int> w) {
init(n);
for (int i = 0; i < n - 1; ++i) {
G[u[i]].emplace_back(v[i], w[i]);
G[v[i]].emplace_back(u[i], w[i]);
}
vector<long long> dist1(n), dist2(n);
dfs_calc_dist(x, -1, 0, dist1);
dfs_calc_dist(y, -1, 0, dist2);
vector<pair<long long, int>> value;
for (int i = 0; i < n; ++i) {
value.emplace_back(dist1[i], i);
value.emplace_back(dist2[i], i);
}
sort(all(value));
int ans = 0;
{
vector<int> used(n);
long long sum = 0;
int cnt = 0;
for (auto c : value) {
if (used[c.second])
continue;
sum += c.first;
++cnt;
used[c.second] = 1;
if (sum > k)
break;
ans = max(ans, cnt);
}
}
vector<int> path;
{
vector<int> par(n);
dfs_find_par(x, -1, par);
int tmp = y;
while (tmp != -1) {
path.push_back(tmp);
tmp = par[tmp];
}
}
vector<int> used(n);
int cnt = 0;
for (int v : path) {
k -= min(dist1[v], dist2[v]);
++cnt;
used[v] = 1;
}
if (k < 0)
return ans;
for (auto c : value) {
if (used[c.second])
continue;
used[c.second] = 1;
++cnt;
k -= c.first;
if (k < 0)
break;
vector<pair<long long, int>> value1;
vector<pair<long long, int>> value2;
for (int i = 0; i < n; ++i) {
if (used[i]) {
value1.emplace_back(abs(dist1[i] - dist2[i]), i);
} else {
value2.emplace_back(max(dist1[i], dist2[i]), i);
}
}
sort(all(value1));
sort(all(value2));
for (int i1 = 0; i1 <= value1.size(); ++i1) {
for (int i2 = 0; i2 <= value2.size(); ++i2) {
long long sum = 0;
for (int i = 0; i < i1; ++i) {
sum += value1[i].first;
}
for (int i = 0; i < i2; ++i) {
sum += value2[i].first;
}
if (sum <= k) {
ans = max(ans, cnt + i1 + i2 * 2);
} else {
break;
}
}
}
}
return ans;
}
#ifdef LOCAL
int main()
{
// BEGIN SECRET
{
std::string in_secret = "cc61ad56a4797fb3f5c9529f73ce6fcedd85669b";
std::string out_secret = "081ce3c351cbf526b37954b9ad30f2b531a7585c";
char secret[1000];
assert(1 == scanf("%s", secret));
if (std::string(secret) != in_secret)
{
printf("%s\n", out_secret.c_str());
printf("SV\n");
fclose(stdout);
return 0;
}
}
// END SECRET
int Q;
assert(1 == scanf("%d", &Q));
std::vector<int> N(Q), X(Q), Y(Q);
std::vector<long long> K(Q);
std::vector<std::vector<int>> U(Q), V(Q), W(Q);
for (int q = 0; q < Q; q++)
{
assert(4 == scanf("%d %d %d %lld", &N[q], &X[q], &Y[q], &K[q]));
U[q].resize(N[q] - 1);
V[q].resize(N[q] - 1);
W[q].resize(N[q] - 1);
for (int i = 0; i < N[q] - 1; ++i)
{
assert(3 == scanf("%d %d %d", &U[q][i], &V[q][i], &W[q][i]));
}
}
fclose(stdin);
std::vector<int> result(Q);
for (int q = 0; q < Q; q++)
{
result[q] = max_score(N[q], X[q], Y[q], K[q], U[q], V[q], W[q]);
}
// BEGIN SECRET
{
std::string out_secret = "081ce3c351cbf526b37954b9ad30f2b531a7585c";
printf("%s\n", out_secret.c_str());
printf("OK\n");
}
// END SECRET
for (int q = 0; q < Q; q++)
{
printf("%d\n", result[q]);
}
fclose(stdout);
return 0;
}
#endif
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:113:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int i1 = 0; i1 <= value1.size(); ++i1) {
| ~~~^~~~~~~~~~~~~~~~
closing.cpp:114:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int i2 = 0; i2 <= value2.size(); ++i2) {
| ~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
37312 KB |
Output is correct |
2 |
Correct |
131 ms |
43244 KB |
Output is correct |
3 |
Correct |
75 ms |
5456 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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
5 |
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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
5 |
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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '34', found: '20' |
6 |
Halted |
0 ms |
0 KB |
- |