// 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][2];
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++)
dp[node][i][0] = dp[node][i][1] = inf;
dp[node][0][0] = 0;
dp[node][1][1] = D[node];
dp[node][0][1] = 0;
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 = 0; k <= sz[child]; k++) {
if(k!=1)
dp[node][j + k][0] = min(dp[node][j + k][0], dp[node][j][0] + dp[child][k][0]);
if(k != 1)
dp[node][j + k][0] = min(dp[node][j + k][0], dp[node][j][0] + dp[child][k][1]);
//----------------------------------------------------------------------------------------
if(j != 1 && k!=1)
dp[node][j + k][1] = min(dp[node][j + k][1], dp[node][j][1] + dp[child][k][0]);
if(j+k!=1)
dp[node][j + k][1] = min(dp[node][j + k][1], dp[node][j][1] + dp[child][k][1]);
}
}
sz[node] += sz[child];
}
/*cout << node << "\n------\n";
for(int i = 0; i <= sz[node]; i++)
cout << dp[node][i][0] << " " << dp[node][i][1] << "\n";
cout << "--------------------------------------\n";*/
}
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);
/* vector<int> v;
for(int i = 1; i <= n; i++) {
v.push_back(dp[0][i][0]);
cout << v.back() << " ";
}*/
//cout << "\n";
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 |
Incorrect |
9 ms |
8300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
8300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
1644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
8300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |