This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = 1000000000000000000LL;
ll n, m, q, k, x, y, a, b, c, d;
// unordered_map<ll, ll> G1[200000];
vector< pair<ll, ll> > G[200000];
void dfs(ll x, vector<ll> &dist, ll d) {
// cout << "Enter " << x << endl;
// cout << '\t' << dist[x] << ' ' << d << endl;
dist[x] = d;
for (auto i : G[x]) {
if (dist[i.first] == INF) {
dfs(i.first, dist, d + i.second);
}
}
// cout << "Exit " << x << endl;
}
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W) {
n = N;
x = X;
y = Y;
k = K;
for (auto i = 0; i < n - 1; i++) {
// G[U[i]][V[i]] = W[i];
// G[V[i]][U[i]] = W[i];
G[U[i]].push_back({V[i], W[i]});
G[V[i]].push_back({U[i], W[i]});
}
vector<ll> dist1(n, INF), dist2(n, INF);
dfs(x, dist1, 0);
dfs(y, dist2, 0);
if (dist1[y] > k * 2) {
vector<ll> vec;
for (int i = 0; i < n; i++) {
// cout << dist1[i] << ' ';
vec.push_back(dist1[i]);
}
// cout << '\n';
for (int i = 0; i < n; i++) {
vec.push_back(dist2[i]);
}
sort(vec.begin(), vec.end());
ll cnt = 0;
for (auto i : vec) {
// cout << i << '\n';
if (k >= i) {
cnt++;
k -= i;
}
else {
break;
}
}
for (auto i = 0; i < n; i++) {
// G[U[i]][V[i]] = W[i];
// G[V[i]][U[i]] = W[i];
G[i].clear();
}
return cnt;
}
ll cnt = 2;
vector<ll> alr(n, 0);
array< vector<ll>, 2 > vis;
vis[0].resize(n);
vis[1].resize(n);
set< pair< ll, pair<ll, ll> > > st;
for (auto i : G[x]) {
st.insert({dist1[i.first], {i.first, 0}});
}
for (auto i : G[y]) {
st.insert({dist2[i.first], {i.first, 1}});
}
vis[0][x] = 1;
vis[1][y] = 1;
while (!st.empty()) {
auto p = *st.begin();
// cout << p.first << ' ' << p.second.first << ' ' << p.second.second << endl;
// for (auto i : alr) {
// cout << i << ' ';
// }
// cout << '\n';
st.erase(p);
if (alr[p.second.first] == 0 && (p.second.first != x && p.second.first != y) && st.count({(p.second.second == 0 ? dist2[p.second.first] : dist1[p.second.first]), {p.second.first, 1 - p.second.second}})) {
st.erase({(p.second.second == 0 ? dist2[p.second.first] : dist1[p.second.first]), {p.second.first, 1 - p.second.second}});
st.insert({(p.second.second == 0 ? dist2[p.second.first] : dist1[p.second.first]) - p.first, {p.second.first, 1 - p.second.second}});
}
if (k < p.first) {
break;
}
cnt++;
k -= p.first;
alr[p.second.first] += p.first;
vis[p.second.second][p.second.first] = 1;
for (auto i : G[p.second.first]) {
if (vis[p.second.second][i.first]) {
continue;
}
st.insert({(p.second.second == 0 ? dist1[i.first] : dist2[i.first]) - alr[i.first], {i.first, p.second.second}});
}
}
for (auto i = 0; i < n; i++) {
// G[U[i]][V[i]] = W[i];
// G[V[i]][U[i]] = W[i];
G[i].clear();
}
return cnt;
}
# | 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... |