#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline void remin(ll& x, ll y) { x = (y < x ? y : x); }
const int N = (int)1e3 + 1, Q = (int)2e5;
int n, m, q, d[N], ans[Q];
bool vis[N];
vector<ll> dp0[N];
vector<ll> dp1[N][2];
vector<int> adj[N];
void dfs(int x, int pr) {
vis[x] = true;
for(auto& e : adj[x]) {
if(e != pr) {
dfs(e, x);
}
}
}
void dfs2(int x, int pr) {
dp0[x] = {0, LLONG_MAX};
dp1[x][0] = {d[x], LLONG_MAX};
dp1[x][1] = {LLONG_MAX, LLONG_MAX};
for(auto& e : adj[x]) {
if(e != pr) {
dfs2(e, x);
int a = (int)dp0[x].size(), b = (int)dp0[e].size();
vector<ll> res(a + b - 1, LLONG_MAX);
for(int i = 0;i < a;i ++) {
for(int j = 0;j < b;j ++) {
if(dp1[x][0][i] != LLONG_MAX) {
if(dp1[e][0][j] != LLONG_MAX)
remin(res[i + j + 2], dp1[x][0][i] + dp1[e][0][j]);
if(dp1[e][1][j] != LLONG_MAX)
remin(res[i + j + 1], dp1[x][0][i] + dp1[e][1][j]);
}
if(dp1[x][1][i] != LLONG_MAX) {
if(dp1[e][0][j] != LLONG_MAX)
remin(res[i + j + 1], dp1[x][1][i] + dp1[e][0][j]);
if(dp1[e][1][j] != LLONG_MAX)
remin(res[i + j], dp1[x][1][i] + dp1[e][1][j]);
if(dp0[e][j] != LLONG_MAX)
remin(res[i + j], dp1[x][1][i] + dp0[e][j]);
}
}
}
dp1[x][1] = res;
res = vector<ll>(a + b - 1, LLONG_MAX);
for(int i = 0;i < a;i ++) {
for(int j = 0;j < b;j ++) {
if(dp1[x][0][i] != LLONG_MAX) {
if(dp0[e][j] != LLONG_MAX)
remin(res[i + j], dp1[x][0][i] + dp0[e][j]);
}
}
}
dp1[x][0] = res;
res = vector<ll>(a + b - 1, LLONG_MAX);
for(int i = 0;i < a;i ++) {
for(int j = 0;j < b;j ++) {
if(dp0[x][i] != LLONG_MAX) {
if(dp1[e][1][j] != LLONG_MAX)
remin(res[i + j], dp0[x][i] + dp1[e][1][j]);
if(dp1[e][0][j] != LLONG_MAX)
remin(res[i + j], dp0[x][i] + dp1[e][0][j]);
if(dp0[e][j] != LLONG_MAX)
remin(res[i + j], dp0[x][i] + dp0[e][j]);
}
}
}
dp0[x] = res;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
d[0] = 0;
for(int i = 1;i <= n;i ++) {
cin >> d[i];
}
for(int i = 0;i < m;i ++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1;i <= n;i ++) {
if(!vis[i]) {
dfs(i, -1);
adj[0].push_back(i);
adj[i].push_back(0);
}
}
dfs2(0, -1);
cin >> q;
vector<pair<int, int>> queries;
for(int i = 0;i < q;i ++) {
int s;
cin >> s;
queries.emplace_back(s, i);
}
sort(queries.begin(), queries.end());
vector<pair<ll, int>> values;
for(int i = 0;i <= n;i ++) {
values.emplace_back(dp0[0][i], i);
}
sort(values.begin(), values.end());
int ptr = 0;
int mx = 0;
for(auto& e : queries) {
while(ptr <= n && values[ptr].first <= e.first) {
mx = max(mx, values[ptr].second);
ptr ++;
}
ans[e.second] = mx;
}
for(int i = 0;i < q;i ++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
7156 KB |
Output is correct |
2 |
Correct |
22 ms |
8240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
7156 KB |
Output is correct |
2 |
Correct |
22 ms |
8240 KB |
Output is correct |
3 |
Correct |
73 ms |
13652 KB |
Output is correct |
4 |
Correct |
64 ms |
12236 KB |
Output is correct |
5 |
Correct |
74 ms |
11436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
4912 KB |
Output is correct |
2 |
Correct |
41 ms |
4796 KB |
Output is correct |
3 |
Correct |
45 ms |
5296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
7156 KB |
Output is correct |
2 |
Correct |
22 ms |
8240 KB |
Output is correct |
3 |
Correct |
73 ms |
13652 KB |
Output is correct |
4 |
Correct |
64 ms |
12236 KB |
Output is correct |
5 |
Correct |
74 ms |
11436 KB |
Output is correct |
6 |
Correct |
51 ms |
4912 KB |
Output is correct |
7 |
Correct |
41 ms |
4796 KB |
Output is correct |
8 |
Correct |
45 ms |
5296 KB |
Output is correct |
9 |
Correct |
55 ms |
5516 KB |
Output is correct |
10 |
Correct |
58 ms |
5868 KB |
Output is correct |
11 |
Correct |
57 ms |
5568 KB |
Output is correct |