This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Mayoeba Yabureru
*/
#include<bits/stdc++.h>
using namespace std;
int max_score(int n, int x, int y, long long k, vector<int> U, vector<int> V, vector<int> W) {
x ++, y ++;
vector dis(n + 1, vector<long long>(2));
vector<vector<pair<int, long long>>> gr(n + 1);
for (int i = 0; i < n - 1; i ++) {
int u = U[i] + 1, v = V[i] + 1, w = W[i];
gr[u].push_back({v, w});
gr[v].push_back({u, w});
}
function<void(int, int, int)> dfs = [&] (int v, int p, int wt) {
for (auto [u, w] : gr[v]) {
if (u == p) continue;
dis[u][wt] = dis[v][wt] + w;
dfs(u, v, wt);
}
};
dfs(x, 0, 0), dfs(y, 0, 1);
vector<int> vc, path, onpath(n + 1);
function<void(int, int)> go = [&] (int v, int p) {
vc.push_back(v);
if (v == y) path = vc;
for (auto [u, w] : gr[v]) {
if (u == p) continue;
go(u, v);
}
vc.pop_back();
};
go(x, 0);
int ans = 0;
function f = [&] {
vector<int> did(n + 1);
multiset<pair<long long, int>> s;
for (int i = 1; i <= n; i ++) {
s.insert({dis[i][0], i});
s.insert({dis[i][1], i});
}
int cnt = 0;
vector<int> v;
while (s.size()) {
auto [x, y] = *s.begin();
s.erase(s.begin());
if (did[y]) continue;
if (x > k) break;
k -= x;
did[y] = 1;
cnt ++;
}
if (k >= 0) return cnt;
return -100000000;
};
long long kk = k;
ans = f();
k = kk;
int cnt = 0;
vector cost (n + 1, vector<long long> (2));
for (int i = 1; i <= n; i ++) {
cost[i][0] = min(dis[i][0], dis[i][1]);
cost[i][1] = max(dis[i][0], dis[i][1]) - cost[i][0];
}
for (auto x : path) {
k -= cost[x][0];
cnt ++;
onpath[x] = 1;
}
if (k < 0) return ans;
set<pair<long long, int>> s, ss;
for (int i = 1; i <= n; i ++) {
if (onpath[i]) s.insert({cost[i][1], i});
else {
if (cost[i][0] > cost[i][1]) ss.insert({cost[i][0] + cost[i][1], i});
else s.insert({cost[i][0], i});
}
}
while (s.size()) {
if (ss.size() == 0) {
auto [a, x] = *s.begin();
s.erase(s.begin());
if (a > k) break;
k -= a;
cnt ++;
onpath[x] ++;
if (onpath[x] == 1) s.insert({cost[x][1], x});
}
else {
long long mn = 1e17;
auto [a, x] = *ss.begin();
auto [b, y] = *s.begin();
if (s.size() != 1) {
auto [c, z] = * ++s.begin();
mn = min(mn, b + c);
}
if (onpath[y] == 0) mn = min(mn, dis[y][0] + dis[y][1]);
if (mn <= a || a > k) {
s.erase(s.begin());
if (b > k) break;
k -= b;
cnt ++;
onpath[y] ++;
if (onpath[y] == 1) s.insert({cost[y][1], y});
}
else {
ss.erase(ss.begin());
if (a > k) break;
k -= a;
cnt += 2;
}
}
}
multiset<pair<long long, int>> mss;
while (ss.size()) {
auto [a, x] = *ss.begin();
ss.erase(ss.begin());
mss.insert({cost[x][0], x});
}
while (mss.size()) {
auto [a, x] = *mss.begin();
mss.erase(mss.begin());
if (a > k) break;
k -= a;
cnt ++;
}
return max(cnt, ans);
}
/*
1
7 0 2 10
0 1 2
0 3 3
1 2 4
2 4 2
2 5 5
5 6 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |