#include <bits/stdc++.h>
#define Y8o "dzumbus"
#define maxn (int) 1005
#define ll long long
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define int long long
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
const ll INF = 1e18;
int n, m, Q;
int a[maxn], sz[maxn], dd[maxn];
vector<int> o[maxn];
ll dp[maxn][maxn][2], tmp[maxn][2], ans[maxn];
void dfs(int u, int p = 0) {
dd[u] = sz[u] = 1;
dp[u][0][0] = 0;
for(int v : o[u]) if(v != p) {
dfs(v, u);
memset(tmp, 0x3f, sizeof tmp);
fi(i, 0, sz[u]) {
fi(j, 0, sz[v]) {
tmp[i + j][0] = min( tmp[i + j][0],
dp[u][i][0] + min(dp[v][j][0], dp[v][j][1]) );
tmp[i + j][1] = min( tmp[i + j][1],
dp[u][i][1] + min(dp[v][j][0], dp[v][j][1]) );
if(i + j + 1 <= sz[u] + sz[v])
tmp[i + j + 1][1] = min( tmp[i + j + 1][1],
dp[u][i][0] + dp[v][j][1] + a[u] );
if(i + j + 1 <= sz[u] + sz[v])
tmp[i + j + 1][1] = min( tmp[i + j + 1][1],
dp[u][i][1] + dp[v][j][0] + a[v] );
if(i + j + 2 <= sz[u] + sz[v])
tmp[i + j + 2][1] = min( tmp[i + j + 2][1],
dp[u][i][0] + dp[v][j][0] + a[u] + a[v] );
}
}
memcpy(dp[u], tmp, sizeof tmp);
sz[u] += sz[v];
}
}
void solve() {
cin >> n >> m;
fi(i, 1, n) cin >> a[i];
fi(i, 1, m) {
int u, v; cin >> u >> v;
o[u].push_back(v), o[v].push_back(u);
}
memset(dp, 0x3f, sizeof dp);
memset(ans, 0x3f, sizeof ans);
ans[0] = 0;
fi(i, 1, n) if(!dd[i]) {
dfs(i);
fid(j, n, 1) {
fi(t, 1, min(j, sz[i])) {
ans[j] = min(ans[j], ans[j - t] + min(dp[i][t][0], dp[i][t][1]));
}
}
}
cin >> Q;
fi(i, 1, Q) {
ll lim; cin >> lim;
fid(j, n, 0) if(ans[j] <= lim) {
cout << j << '\n';
break;
}
}
}
main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r")) {
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
return 0;
}
Compilation message
dzumbus.cpp:89:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
89 | main() {
| ^~~~
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16464 KB |
Output is correct |
2 |
Correct |
7 ms |
16464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16464 KB |
Output is correct |
2 |
Correct |
7 ms |
16464 KB |
Output is correct |
3 |
Correct |
41 ms |
17992 KB |
Output is correct |
4 |
Correct |
43 ms |
17224 KB |
Output is correct |
5 |
Correct |
107 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
16976 KB |
Output is correct |
2 |
Correct |
28 ms |
16832 KB |
Output is correct |
3 |
Correct |
35 ms |
18532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16464 KB |
Output is correct |
2 |
Correct |
7 ms |
16464 KB |
Output is correct |
3 |
Correct |
41 ms |
17992 KB |
Output is correct |
4 |
Correct |
43 ms |
17224 KB |
Output is correct |
5 |
Correct |
107 ms |
16712 KB |
Output is correct |
6 |
Correct |
24 ms |
16976 KB |
Output is correct |
7 |
Correct |
28 ms |
16832 KB |
Output is correct |
8 |
Correct |
35 ms |
18532 KB |
Output is correct |
9 |
Correct |
38 ms |
18256 KB |
Output is correct |
10 |
Correct |
53 ms |
19024 KB |
Output is correct |
11 |
Correct |
97 ms |
18760 KB |
Output is correct |