#include<bits/stdc++.h>
#include "closing.h"
#define fi first
#define se second
#define ll long long
using namespace std;
const ll N = 2e5 + 10;
bitset<N> us;
vector<pair<ll, ll>> gr[N];
ll dist[N][2];
ll ds[N][2];
bool flag;
void ultra_clear(int n) {
flag &= 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 2; ++j)
dist[i][j] &= 0,
ds[i][j] &= 0;
gr[i].clear();
us[i] = 0;
}
}
void pre_calc(ll city, ll last, ll type) {
for (auto i : gr[city]) {
if (i.fi == last)
continue;
dist[i.fi][type] = dist[city][type] + i.se;
pre_calc(i.fi, city, type);
}
}
void get_path(int city, int last, int y, vector<int> &path) {
if (!flag)
path.push_back(city);
if (city == y) {
flag = 1;
return;
}
for (auto i : gr[city]) {
if (i.fi == last)
continue;
get_path(i.fi, city, y, path);
}
if (!flag)
path.pop_back();
}
int max_score(int n, int x, int y, ll k, vector<int> u, vector<int> v, vector<int> w) {
ultra_clear(n);
int m = n - 1, ans = 0;
for (int i = 0; i < m; ++i) {
gr[u[i]].push_back({v[i], w[i]});
gr[v[i]].push_back({u[i], w[i]});
}
dist[x][0] = dist[y][1] &= 0;
pre_calc(x, 0, 0);
pre_calc(y, 0, 1);
set<pair<int, int>> stx, sty;
vector<int> path;
get_path(x, 0, y, path);
int psz = path.size() - 1;
for (int i = 0; i < path.size(); ++i) {
for (int j = 0; j < path.size(); ++j) {
ll hp = k;
int cnt = 0;
ll now[n] = {};
for (int q = 0; q < n; ++q)
ds[q][0] = dist[q][0],
ds[q][1] = dist[q][1];
for (int q = 0; q < i - 1; ++q)
now[path[q]] = max(now[path[q]], dist[path[q]][0]);
for (int q = 0; q < j - 1; ++q)
now[path[psz - q]] = max(now[path[psz - q]], dist[path[psz - q]][1]);
for (int q = 0; q <= psz; ++q) {
for (int z = 0; z < 2; ++z)
ds[path[q]][z] = max(0ll, ds[path[q]][z] - now[path[q]]);
hp -= now[path[q]];
}
if (hp < 0)
break;
set<pair<int, int>> stx, sty;
for (int i = 0; i < n; ++i)
stx.insert({ds[i][0], i}),
sty.insert({ds[i][1], i});
while (true) {
auto p1 = *stx.begin(), p2 = *sty.begin();
if (p1.fi <= p2.fi) {
if (hp - p1.fi < 0)
break;
hp -= p1.fi;
sty.erase({max(0ll, dist[p1.se][1] - now[p1.se]), p1.se});
now[p1.se] += p1.fi;
stx.insert({max(0ll, dist[p1.se][0] - now[p2.se]), p2.se});
stx.erase(stx.begin());
} else {
if (hp - p2.fi < 0)
break;
hp -= p2.fi;
stx.erase({max(0ll, dist[p1.se][0] - now[p2.se]), p2.se});
now[p2.se] += p2.fi;
stx.insert({max(0ll, dist[p1.se][0] - now[p2.se]), p2.se});
sty.erase(sty.begin());
}
++cnt;
}
ans = max(ans, cnt);
}
}
return ans;
}
//signed main() {
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
// int t;
// cin >> t;
// while (t--) {
// int n;
// int x, y;
// ll k;
// cin >> n >> x >> y >> k;
// vector<int> u(n - 1), v(n - 1), w(n - 1);
// for (int i = 0; i < n - 1; ++i)
// cin >> u[i] >> v[i] >> w[i];
// cout << max_score(n, x, y, k, u, v, w);
// }
// return 0;
//}
//2
//7 0 2 10
//0 1 2
//0 3 3
//1 2 4
//2 4 2
//2 5 5
//5 6 3
//4 0 3 20
//0 1 18
//1 2 1
//2 3 19
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:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < path.size(); ++i) {
| ~~^~~~~~~~~~~~~
closing.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int j = 0; j < path.size(); ++j) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1073 ms |
54240 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9048 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9048 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9048 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9048 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
2 ms |
9048 KB |
Output is correct |
3 |
Incorrect |
2 ms |
9052 KB |
1st lines differ - on the 1st token, expected: '30', found: '37' |
4 |
Halted |
0 ms |
0 KB |
- |