// Sometimes, the questions are complicated - and the answers are simple. //
#include<iostream>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 1e3 + 5, inf = 1e9 + 2;
int n, m, q, D[N];
vector<int> adj[N];
bool visited[N];
int dp[N][N][3];
int sz[N];
void dfs(int node, int p) {
if(visited[node])
return;
visited[node] = true;
sz[node] = 1;
for(int i = 0; i <= n; i++)
for(int j = 0; j < 3; j++)
dp[node][i][j] = inf;
dp[node][0][0] = 0;
dp[node][1][1] = D[node];
dp[node][0][1] = inf;
for(auto i : adj[node])
if(i != p) {
int child = i;
dfs(child, node);
for(int j = sz[node]; j >= 0; j--) {
for(int k = 1; k <= sz[child]; k++) {
dp[node][j + k][0] = min(dp[node][j + k][0], dp[node][j][0] + dp[child][k][0]);
dp[node][j + k][0] = min(dp[node][j + k][0], dp[node][j][0] + dp[child][k][2]);
//----------------------------------------------------------------------------------------
dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][2] + dp[child][k][0]);
dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][2] + dp[child][k][1]);
dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][2] + dp[child][k][2]);
dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][1] + dp[child][k][1]);
dp[node][j + k][2] = min(dp[node][j + k][2], dp[node][j][1] + dp[child][k][2]);
dp[node][j + k][1] = min(dp[node][j + k][1], dp[node][j][1] + dp[child][k][0]);
dp[node][j + k][1] = min(dp[node][j + k][1], dp[node][j][1] + dp[child][k][2]);
}
}
sz[node] += sz[child];
}
}
int main() {
IO
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> D[i];
for(int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
D[0] = inf;
for(int i = 1; i <= n; i++)
if(!visited[i]) {
dfs(i, 0);
adj[0].push_back(i);
}
dfs(0, 0);
cin >> q;
while(q--) {
int s;
cin >> s;
for(int i = n; i >= 0; i--)
if(dp[0][i][0] <= s) {
cout << i << "\n";
break;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12268 KB |
Output is correct |
2 |
Correct |
16 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12268 KB |
Output is correct |
2 |
Correct |
16 ms |
12416 KB |
Output is correct |
3 |
Correct |
71 ms |
13164 KB |
Output is correct |
4 |
Correct |
73 ms |
13164 KB |
Output is correct |
5 |
Correct |
156 ms |
12780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
1772 KB |
Output is correct |
2 |
Correct |
49 ms |
1516 KB |
Output is correct |
3 |
Correct |
55 ms |
1388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
12268 KB |
Output is correct |
2 |
Correct |
16 ms |
12416 KB |
Output is correct |
3 |
Correct |
71 ms |
13164 KB |
Output is correct |
4 |
Correct |
73 ms |
13164 KB |
Output is correct |
5 |
Correct |
156 ms |
12780 KB |
Output is correct |
6 |
Correct |
37 ms |
1772 KB |
Output is correct |
7 |
Correct |
49 ms |
1516 KB |
Output is correct |
8 |
Correct |
55 ms |
1388 KB |
Output is correct |
9 |
Correct |
73 ms |
13036 KB |
Output is correct |
10 |
Correct |
80 ms |
13036 KB |
Output is correct |
11 |
Correct |
160 ms |
12780 KB |
Output is correct |