This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//GRT_2018
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define all(x) x.begin(), x.end()
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 200 * 1000 + 10;
int n, endX, endY;
vector<pi> V[nax];
pair<ll,ll> dist[nax];
void dfs(int x, int par, int bit) {
for (auto [y, w] : V[x]) {
if (y == par) continue;
if (bit == 0) dist[y].ST = dist[x].ST + w;
else dist[y].ND = dist[x].ND + w;
dfs(y, x, bit);
}
}
int max_score(int N, int x, int y, ll k, vi u, vi v, vi w) {
n = N;
endX = x;
endY = y;
for (int i = 0; i < n; ++i) {
V[i].clear();
}
for (int i = 0; i < n - 1; ++i) {
V[u[i]].emplace_back(v[i], w[i]);
V[v[i]].emplace_back(u[i], w[i]);
}
dist[endX].ST = 0;
dfs(endX, -1, 0);
dist[endY].ND = 0;
dfs(endY, -1, 1);
ll d = dist[endY].ST;
for (int i = 0; i < n; ++i) {
if (dist[i].ST > dist[i].ND) {
swap(dist[i].ST, dist[i].ND);
}
}
sort(dist, dist + n);
int ans = 0;
ll s = 0;
for (int i = 0; i < n; ++i) {
s += dist[i].ST;
if (s <= k) ans = max(ans, i + 1);
swap(dist[i].ST, dist[i].ND);
}
sort(dist, dist + n);
int cur = 0;
multiset<ll>ext;
for (int i = 0; i < n; ++i) {
if (dist[i].ST + dist[i].ND == d) {
cur++;
k -= dist[i].ND;
} else {
ext.insert(dist[i].ND);
}
}
if (k < 0) return ans;
s = 0;
for (int i = 0; i < n; ++i) {
if (dist[i].ST + dist[i].ND == d) {
s += dist[i].ST - dist[i].ND;
cur++;
} else {
s += dist[i].ST;
cur += 2;
ext.erase(ext.find(dist[i].ND));
}
ll s2 = 0;
int add = 0;
for (auto it : ext) {
if (s + s2 + it <= k) {
add++;
s2 += it;
} else {
break;
}
}
if (s <= k) {
ans = max(ans, cur + add);
}
}
return ans;
}
# | 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... |