#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template <class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
const int mx = 1005;
vector<int> adj[mx];
ll val[mx], visited[mx], inf = (ll)1e17;
//vector<vector<vector<ll>>> dp(mx, vector<vector<ll>>(3));
vector<ll> dp[mx][3]; //unused, used but not connected, used and connected
void dfs(int v, int p)
{
visited[v] = true;
for (int a : adj[v]) {
if (a == p) continue;
dfs(a,v);
}
}
vector<ll> comb(const vector<ll>& a, const vector<ll>& b)
{
vector<ll> ret(SZ(a)+SZ(b)-1,inf);
for (int q = 0; q < SZ(a); q++) {
for (int w = 0; w < SZ(b); w++) {
ret[q+w] = min(ret[q+w],a[q]+b[w]);
}
}
return ret;
}
vector<ll> mi(vector<ll> a, const vector<ll> &b)
{
while (SZ(a) < SZ(b)) a.push_back(inf);
for (int q = 0; q < SZ(b); q++) a[q] = min(a[q], b[q]);
return a;
}
void dfs2(int v, int p)
{
dp[v][0] = {0, inf};
dp[v][1] = {inf, val[v]};
dp[v][2] = {inf, inf};
for (int a : adj[v]) {
if (a == p) continue;
dfs2(a,v);
dp[v][0] = comb(dp[v][0], mi(dp[a][0], dp[a][2]));
dp[v][2] = mi(comb(dp[v][2], mi(dp[a][0], mi(dp[a][1], dp[a][2]))), comb(dp[v][1], mi(dp[a][1], dp[a][2])));
dp[v][1] = comb(dp[v][1], dp[a][0]);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
for (int q = 1; q <= n; q++) cin >> val[q];
for (int q = 0; q < m; q++) {
int a,b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int q = 1; q <= n; q++) {
if (!visited[q]) {
dfs(q,0);
adj[0].push_back(q);
}
}
dfs2(0,-1);
for (int q = SZ(dp[0][0])-2; q >= 0; q--) {
dp[0][0][q] = min(dp[0][0][q], dp[0][0][q+1]);
}
int qq;
cin >> qq;
while (qq--) {
ll x;
cin >> x;
int lo = 0, hi = SZ(dp[0][0]);
while (hi - lo > 1) {
int mm = (lo+hi)/2;
if (dp[0][0][mm] <= x) lo = mm;
else hi = mm;
}
cout << lo << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8684 KB |
Output is correct |
2 |
Correct |
16 ms |
12012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8684 KB |
Output is correct |
2 |
Correct |
16 ms |
12012 KB |
Output is correct |
3 |
Correct |
65 ms |
14956 KB |
Output is correct |
4 |
Correct |
67 ms |
12396 KB |
Output is correct |
5 |
Correct |
65 ms |
10988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
2668 KB |
Output is correct |
2 |
Correct |
44 ms |
2412 KB |
Output is correct |
3 |
Correct |
50 ms |
2924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
8684 KB |
Output is correct |
2 |
Correct |
16 ms |
12012 KB |
Output is correct |
3 |
Correct |
65 ms |
14956 KB |
Output is correct |
4 |
Correct |
67 ms |
12396 KB |
Output is correct |
5 |
Correct |
65 ms |
10988 KB |
Output is correct |
6 |
Correct |
45 ms |
2668 KB |
Output is correct |
7 |
Correct |
44 ms |
2412 KB |
Output is correct |
8 |
Correct |
50 ms |
2924 KB |
Output is correct |
9 |
Correct |
53 ms |
3180 KB |
Output is correct |
10 |
Correct |
58 ms |
3564 KB |
Output is correct |
11 |
Correct |
59 ms |
3308 KB |
Output is correct |