#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];
int ans[maxn];
vector<pair<ll, int>> cur;
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);
fi(i, 1, n) if(!dd[i]) {
dfs(i);
fi(j, 2, n) {
ll res = min({ dp[i][j][0], dp[i][j][1] });
cur.push_back({ res, j });
}
}
sort(cur.begin(), cur.end());
int pre = 0;
fi(i, 0, (int)cur.size() - 1) {
ans[i] = max(pre, cur[i].second);
pre = ans[i];
}
cin >> Q;
fi(i, 1, Q) {
ll lim; cin >> lim;
int l = 0, r = cur.size() - 1;
while(l <= r) {
int mid = (l + r) >> 1;
if(cur[mid].first <= lim) l = mid + 1;
else r = mid - 1;
}
cout << ans[r] << '\n';
}
}
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:100:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
100 | main() {
| ^~~~
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16464 KB |
Output is correct |
2 |
Correct |
7 ms |
16464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16464 KB |
Output is correct |
2 |
Correct |
7 ms |
16464 KB |
Output is correct |
3 |
Correct |
30 ms |
18512 KB |
Output is correct |
4 |
Correct |
34 ms |
19272 KB |
Output is correct |
5 |
Correct |
34 ms |
18760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
18348 KB |
Output is correct |
2 |
Incorrect |
27 ms |
17992 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16464 KB |
Output is correct |
2 |
Correct |
7 ms |
16464 KB |
Output is correct |
3 |
Correct |
30 ms |
18512 KB |
Output is correct |
4 |
Correct |
34 ms |
19272 KB |
Output is correct |
5 |
Correct |
34 ms |
18760 KB |
Output is correct |
6 |
Correct |
28 ms |
18348 KB |
Output is correct |
7 |
Incorrect |
27 ms |
17992 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |