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>
#define fore(i, a, b) for (signed i = (a), i##_last = (b); i < i##_last; ++i)
#define fort(i, a, b) for (signed i = (a), i##_last = (b); i <= i##_last; ++i)
#define ford(i, a, b) for (signed i = (a), i##_last = (b); i >= i##_last; --i)
#define fi first
#define se second
#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ld = long double;
template<class A, class B> bool maxi(A &a, const B &b) {return (a < b) ? (a = b, true):false;};
template<class A, class B> bool mini(A &a, const B &b) {return (a > b) ? (a = b, true):false;};
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const int maxN = 1005;
const ll inf = 0x3f3f3f3f3f3f3f3f;
vi adj[maxN];
ll cost[maxN];
bool seen[maxN];
int n, m, r, q, s, d[maxN], a[maxN], b[maxN];
set<ii> p;
set<array<int, 3> > pq;
void expand(const vi &v) {
cost[r + sz(v)] = cost[r];
r += sz(v);
for (const int &u : v) {
seen[u] = true;
p.erase({d[u], u});
cost[r] += d[u];
}
for (const int &x : v)
for (const int &y : adj[x]) {
if (seen[y])
continue;
p.insert({d[y], y});
}
}
array<int, 3> getData() {
array<int, 3> res;
auto x = p.begin();
res[0] = x -> first;
res[1] = x -> second;
++x;
res[0] += (x -> first);
res[2] = x -> second;
return res;
}
void solve() {
cin >> n >> m;
fort(i, 1, n) {
cin >> d[i];
cost[i] = inf;
}
fort(i, 1, m) {
cin >> a[i] >> b[i];
pq.insert({d[a[i]] + d[b[i]], a[i], b[i]});
adj[a[i]].pb(b[i]);
adj[b[i]].pb(a[i]);
}
cost[n + 1] = inf;
while (!pq.empty() || p.size() >= 2) {
const auto [w1, x, y] = *pq.begin();
if (seen[x] || seen[y]) {
pq.erase(pq.begin());
continue;
}
if (p.size() <= 1) {
pq.erase(pq.begin());
expand({x, y});
} else {
const auto [w2, u, v] = getData();
if (w1 <= w2) {
pq.erase(pq.begin());
expand({x, y});
} else
expand({u, v});
}
if (!p.empty())
mini(cost[r + 1], cost[r] + (p.begin() -> fi));
}
ford(i, n, 0)
mini(cost[i], cost[i + 1]);
cin >> q;
for (int lo, hi, mid; q--;) {
cin >> s;
lo = 0;
hi = n + 1;
while (lo + 1 < hi) {
mid = lo + hi >> 1;
if (cost[mid] <= s)
lo = mid;
else
hi = mid;
}
cout << lo << '\n';
}
}
int main() {
#ifdef LOCAL
freopen("input.INP", "r", stdin);
#endif // LOCAL
cin.tie(0) -> sync_with_stdio(0);
cout.tie(0);
solve();
return 0;
}
Compilation message (stderr)
dzumbus.cpp: In function 'void solve()':
dzumbus.cpp:87:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | const auto [w1, x, y] = *pq.begin();
| ^
dzumbus.cpp:96:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
96 | const auto [w2, u, v] = getData();
| ^
dzumbus.cpp:114:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
114 | mid = lo + hi >> 1;
| ~~~^~~~
# | 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... |