#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
const int INF = 1e9 + 7;
const int MAX = 1010;
int n, m, q;
int D[MAX];
int sz[MAX];
int res[MAX];
bool vis[MAX];
vector<int> adj[MAX];
int dp[MAX][MAX][2][2];
void dfs(int u, int p) {
FOR(j, 0, n) FOR(k, 0, 1) FOR(l, 0, 1) dp[u][j][k][l] = INF;
sz[u] = 1;
dp[u][0][0][0] = 0;
dp[u][0][1][0] = D[u];
for (int v: adj[u]) if (!vis[v]) {
vis[v] = true; dfs(v, u);
FORD(j, 0, sz[u]) FOR(k, 0, 1) FOR(l, 0, 1) {
FORD(n, 0, sz[v]) FOR(m, 0, 1) FOR(p, 0, 1) {
int J = j + n + (k & m) * (!l + !p), K = k, L = l | (k & m);
minimize(dp[u][J][K][L], dp[u][j][k][l] + dp[v][n][m][p]);
}
}
sz[u] += sz[v];
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> D[i];
adj[0].push_back(i);
}
for (int i = 1; i <= m; ++i) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
res[n + 1] = INF;
D[0] = INF;
vis[0] = 1;
dfs(0, 0);
FORD(j, 0, n) {
res[j] = INF;
FOR(k, 0, 1) FOR(l, 0, 1)
minimize(res[j], dp[0][j][k][l]);
minimize(res[j], res[j + 1]);
}
cin >> q; while (q--) {
int S; cin >> S;
cout << (upper_bound(res, res + 1 + n, S) - res - 1) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
16212 KB |
Output is correct |
2 |
Correct |
24 ms |
16328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
16212 KB |
Output is correct |
2 |
Correct |
24 ms |
16328 KB |
Output is correct |
3 |
Correct |
49 ms |
17100 KB |
Output is correct |
4 |
Correct |
51 ms |
17068 KB |
Output is correct |
5 |
Correct |
49 ms |
16760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
1632 KB |
Output is correct |
2 |
Correct |
23 ms |
1472 KB |
Output is correct |
3 |
Correct |
26 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
16212 KB |
Output is correct |
2 |
Correct |
24 ms |
16328 KB |
Output is correct |
3 |
Correct |
49 ms |
17100 KB |
Output is correct |
4 |
Correct |
51 ms |
17068 KB |
Output is correct |
5 |
Correct |
49 ms |
16760 KB |
Output is correct |
6 |
Correct |
24 ms |
1632 KB |
Output is correct |
7 |
Correct |
23 ms |
1472 KB |
Output is correct |
8 |
Correct |
26 ms |
1228 KB |
Output is correct |
9 |
Correct |
45 ms |
16976 KB |
Output is correct |
10 |
Correct |
51 ms |
16984 KB |
Output is correct |
11 |
Correct |
55 ms |
16692 KB |
Output is correct |