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 <bits/stdc++.h>
using namespace std;
#ifndef _IN_LOCAL
#define dbg(...)
#endif
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)
template<typename T> void read(T& x) { cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p) { read(p.first), read(p.second);}
template<typename T> void read(vector<T>& v) { for (auto& x : v) read(x); }
template<typename T1, typename T2> void read(T1& x, T2& y) { read(x), read(y); }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z) { read(x), read(y), read(z); }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz) { read(x), read(y), read(z), read(zz); }
template<typename T> void print(vector<T>& v) { for (auto& x : v) cout << x << ' '; cout << endl; }
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define QYES quit("YES")
#define QNO quit("NO")
#define int long long
#define double long double
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=20;
int n, x, y, k;
vector<vector<pair<int, int>>> adj;
vector<int> dist1, dist2, has, is;
void merge(int u, int v, priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<>>& pq) {
if (has[v]==3 || has[u]==has[v]) return;
if (has[u]==3) {
if (has[v]==0) {
if (dist1[v]<dist2[v]) pq.push({(dist1[v]-is[v])*2, {1, v}}), pq.push({(dist2[v]-is[v]), {3, v}});
else pq.push({(dist2[v]-is[v])*2, {2, v}}), pq.push({(dist1[v]-is[v]), {3, v}});
}
else if (has[v]==1) pq.push({(dist2[v]-is[v])*2, {2, v}});
else if (has[v]==2) pq.push({(dist1[v]-is[v])*2, {1, v}});
} else if (has[u]==1) pq.push({(dist1[v]-is[v])*2, {1, v}});
else if (has[u]==2) pq.push({(dist2[v]-is[v])*2, {2, v}});
}
int calc(int ans=0) {
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<>> pq;
for (int i=0; i<n; i++) {
if (has[i]) {
for (auto [u, v]: adj[i]) merge(i, u, pq);
}
}
auto base=k;
while (!pq.empty()) {
auto [d, p] = pq.top(); pq.pop();
auto [act, u] = p; if ((has[u]&act)==act) continue;
if (act!=3) d/=2;
assert(act!=3);
if (d>k) continue;
if (act&1) ans++;
if (act&2) ans++;
k-=d;
has[u] |= act;
is[u] += d;
for (auto [v, w]: adj[u]) {
merge(u, v, pq);
}
}
k=base;
return ans;
}
signed max_score(signed N, signed X, signed Y, long long K, vector<signed> U, vector<signed> V, vector<signed> W) {
n = N, x = X, y = Y, k = K, adj.clear(), adj.resize(n);
for (int i=0; i<n-1; i++) {
adj[U[i]].pb(mp(V[i], W[i]));
adj[V[i]].pb(mp(U[i], W[i]));
}
dist1.clear(), dist2.clear(), has.clear(), is.clear();
is.resize(n, 0), dist1.resize(n, inf), dist2.resize(n, inf), has.resize(n, 0); has[x] = 1, has[y] = 2;
{
queue<pair<int, int>> q; q.push({x, 0});
dist1[x] = 0;
while (!q.empty()) {
auto [u, d] = q.front(); q.pop();
for (auto& [v, w] : adj[u]) {
if (dist1[v] == inf) {
dist1[v] = dist1[u] + w;
q.push({v, d+w});
}
}
}
}
{
queue<pair<int, int>> q; q.push({y, 0});
dist2[y] = 0;
while (!q.empty()) {
auto [u, d] = q.front(); q.pop();
for (auto& [v, w] : adj[u]) {
if (dist2[v] == inf) {
dist2[v] = dist2[u] + w;
q.push({v, d+w});
}
}
}
}
int ans=calc(2);
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... |