#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
void setIO(const string &name) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin.exceptions(istream::failbit);
#ifdef LOCAL
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
freopen((name + ".out").c_str(), "w", stderr);
#endif
}
const int inf = 0x3f3f3f3f, mod = 1e9 + 7, maxn = 1e3 + 5;
const long long INFL = 0x3f3f3f3f3f3f3f3f;
long long d[maxn];
bool vis[maxn];
vector<int> adj[maxn];
vector<long long> dp[maxn][2];
vector<long long> merge(vector<long long> a, vector<long long> b) {
vector<long long> res(a.size() + b.size() - 1, INFL);
for (int i = 0; i < (int) a.size(); ++i) {
for (int j = 0; j < (int) b.size(); ++j) {
res[i + j] = min(res[i + j], a[i] + b[j]);
}
}
return res;
}
void dfs1(int c, int p) {
vis[c] = true;
for (auto &i : adj[c]) {
if (i != p) {
dfs1(i, c);
}
}
}
void dfs2(int c, int p) {
dp[c][0] = {0, INFL};
dp[c][1] = {0, d[c]};
for (auto &i : adj[c]) {
if (i != p) {
dfs2(i, c);
dp[c][0] = merge(dp[c][0], dp[i][0]);
dp[c][1] = merge(dp[c][1], dp[i][1]);
}
}
for (int i = 2; i < (int) dp[c][0].size(); ++i) {
dp[c][0][i] = min(dp[c][0][i], dp[c][1][i]);
}
}
int main() {
setIO("3");
int n, m;
cin >> n >> m;
++n;
d[0] = INFL;
for (int i = 1; i < n; ++i) {
cin >> d[i];
}
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
// connect roots of all forests to node 0
for (int i = 1; i < n; ++i) {
if (!vis[i]) {
adj[0].push_back(i);
adj[i].push_back(0);
dfs1(i, -1);
}
}
dfs2(0, -1);
int q;
cin >> q;
vector<pair<long long, int>> queries(q);
for (int i = 0; i < q; ++i) {
cin >> queries[i].first;
queries[i].second = i;
}
sort(queries.begin(), queries.end());
reverse(queries.begin(), queries.end());
vector<int> sol(q);
int j = 0;
for (int i = n - 1; i >= 0; --i) {
while (j < q && dp[0][0][i] <= queries[j].first) {
sol[queries[j].second] = i;
++j;
}
}
for (auto &i : sol) {
cout << i << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
5068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
5068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
49 ms |
5044 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
5068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |