// 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]);
}
}
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;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12268 KB |
Output is correct |
2 |
Correct |
15 ms |
12268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12268 KB |
Output is correct |
2 |
Correct |
15 ms |
12268 KB |
Output is correct |
3 |
Correct |
72 ms |
14444 KB |
Output is correct |
4 |
Correct |
78 ms |
15152 KB |
Output is correct |
5 |
Correct |
159 ms |
14700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
3084 KB |
Output is correct |
2 |
Correct |
41 ms |
2796 KB |
Output is correct |
3 |
Correct |
64 ms |
3288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12268 KB |
Output is correct |
2 |
Correct |
15 ms |
12268 KB |
Output is correct |
3 |
Correct |
72 ms |
14444 KB |
Output is correct |
4 |
Correct |
78 ms |
15152 KB |
Output is correct |
5 |
Correct |
159 ms |
14700 KB |
Output is correct |
6 |
Correct |
39 ms |
3084 KB |
Output is correct |
7 |
Correct |
41 ms |
2796 KB |
Output is correct |
8 |
Correct |
64 ms |
3288 KB |
Output is correct |
9 |
Correct |
70 ms |
14316 KB |
Output is correct |
10 |
Correct |
82 ms |
14956 KB |
Output is correct |
11 |
Correct |
170 ms |
14700 KB |
Output is correct |