#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e14;
const int MAXN = 1e3 + 25;
int n, m;
vector <int> adj[MAXN];
ll d[MAXN], sze[MAXN];
bool vis[MAXN];
void pre (int pos, int par) {
vis[pos] = 1; sze[pos] = 1;
for (int j = 0; j < (int)adj[pos].size(); j++) {
if (adj[pos][j] == par) {
adj[pos].erase(adj[pos].begin() + j);
}
}
for (auto j : adj[pos]) {
pre(j, pos); sze[pos] += sze[j];
}
}
vector <ll> dp[MAXN][2][2];
vector <ll> convolve (vector <ll> a, vector <ll> b) {
vector <ll> ret((int)a.size() + (int)b.size() - 1);
for (auto &i : ret) i = inf;
for (int i = 0; i < (int)a.size(); i++) {
for (int j = 0; j < (int)b.size(); j++) {
ret[i + j] = min(ret[i + j], a[i] + b[j]);
}
}
return ret;
}
vector <ll> minimize (vector <ll> a, vector <ll> b) {
vector <ll> ret(max((int)a.size(), (int)b.size()));
for (auto &i : ret) i = inf;
for (int i = 0; i < (int)a.size(); i++) ret[i] = min(ret[i], a[i]);
for (int i = 0; i < (int)b.size(); i++) ret[i] = min(ret[i], b[i]);
return ret;
}
vector <ll> shift (vector <ll> a) {
a.insert(a.begin(), inf);
return a;
}
vector <ll> add (vector <ll> a, ll x) {
for (auto &i : a) i += x;
return a;
}
void dfs (int pos) {
for (auto j : adj[pos]) dfs(j);
{ //!b
dp[pos][0][0] = {0};
for (auto j : adj[pos]) {
auto g = minimize(dp[j][0][0], dp[j][0][1]);
dp[pos][0][0] = convolve(dp[pos][0][0], g);
}
dp[pos][1][0] = dp[pos][0][0];
}
{ //!a and b
vector <ll> dp2[2];
dp2[0] = {0}; dp2[1] = {inf};
for (auto j : adj[pos]) {
auto g = dp2[0], h = dp2[1];
dp2[0] = convolve(dp2[0], dp[j][1][0]);
dp2[1] = convolve(g, dp[j][1][1]);
dp2[1] = minimize(dp2[1], convolve(h, minimize(dp[j][1][1], dp[j][1][0])));
}
dp[pos][0][1] = minimize(dp2[0], shift(dp2[1]));
dp[pos][0][1] = add(dp[pos][0][1], d[pos]);
}
{ //a and b
dp[pos][1][1] = {0};
for (auto j : adj[pos]) {
auto g = minimize(dp[j][1][1], dp[j][1][0]);
dp[pos][1][1] = convolve(dp[pos][1][1], g);
}
dp[pos][1][1] = add(shift(dp[pos][1][1]), d[pos]);
}
}
int main () {
//memset(dp, -1, sizeof(dp));
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> d[i];
for (int i = 1; i <= m; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
adj[0].push_back(i); pre(i, -1); sze[0] += sze[i];
}
}
d[0] = inf; sze[0]++;
dfs(0);
int q; cin >> q;
while (q--) {
ll x; cin >> x;
ll ret = 0;
for (int i = 1; i <= n; i++) {
if (dp[0][0][0][i] <= x) ret = i;
}
cout << ret << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12636 KB |
Output is correct |
2 |
Correct |
18 ms |
16216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12636 KB |
Output is correct |
2 |
Correct |
18 ms |
16216 KB |
Output is correct |
3 |
Correct |
167 ms |
19196 KB |
Output is correct |
4 |
Correct |
170 ms |
16116 KB |
Output is correct |
5 |
Correct |
165 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
1368 KB |
Output is correct |
2 |
Correct |
40 ms |
1108 KB |
Output is correct |
3 |
Correct |
46 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12636 KB |
Output is correct |
2 |
Correct |
18 ms |
16216 KB |
Output is correct |
3 |
Correct |
167 ms |
19196 KB |
Output is correct |
4 |
Correct |
170 ms |
16116 KB |
Output is correct |
5 |
Correct |
165 ms |
14420 KB |
Output is correct |
6 |
Correct |
41 ms |
1368 KB |
Output is correct |
7 |
Correct |
40 ms |
1108 KB |
Output is correct |
8 |
Correct |
46 ms |
1112 KB |
Output is correct |
9 |
Correct |
152 ms |
3664 KB |
Output is correct |
10 |
Correct |
156 ms |
3780 KB |
Output is correct |
11 |
Correct |
161 ms |
3208 KB |
Output is correct |