#include <bits/stdc++.h>
//#pragma GCC target("popcnt")
#define fi first
#define se second
#define lc id<<1
#define rc id<<1^1
const long long inf = 1e18;
#define nmax 1008
double begintime, endtime;
using namespace std;
inline void CALC_TIME()
{
endtime = clock();
cout << "\nexecution time : " << (endtime - begintime + 1) / 1000 << " s";
}
typedef pair<int, int> pii;
int q, n, m, u, v, sz[nmax];
long long p[nmax][nmax], k[nmax][nmax], f[nmax][2][nmax], a[nmax];
bool ck[nmax];
vector<int> l[nmax];
vector<long long> vt;
stack<int> s;
void discover(int x, int pre)
{
assert(!ck[x]);
ck[x] = true;
for (int tmp : l[x])
if (tmp != pre)
discover(tmp, x);
}
void dfs(int x, int pre)
{
sz[x] = 1;
for (int i = 0; i <= n + 1; i++)
f[x][0][i] = f[x][1][i] = p[x][i] = k[x][i] = inf;
f[x][0][0] = 0;
for (int tmp : l[x])
if (tmp != pre)
{
dfs(tmp, x);
for (int i = 0; i <= sz[x]; i++)
for (int j = 0; j <= sz[tmp]; j++)
{
p[x][i + j] = min(p[x][i + j], f[x][0][i] + min(f[tmp][0][j], f[tmp][1][j]));
k[x][i + j + 2] = min(k[x][i + j + 2], f[x][0][i] + f[tmp][0][j] + a[x] + a[tmp]);
k[x][i + j + 1] = min(k[x][i + j + 1], f[x][0][i] + f[tmp][1][j] + a[x]);
k[x][i + j + 1] = min(k[x][i + j + 1], f[x][1][i] + f[tmp][0][j] + a[tmp]);
k[x][i + j] = min(k[x][i + j], f[x][1][i] + min(f[tmp][0][j], f[tmp][1][j]));
}
for (int i = 0; i <= sz[x] + sz[tmp]; i++)
{
f[x][0][i] = p[x][i];
f[x][1][i] = k[x][i];
}
sz[x] += sz[tmp];
f[x][0][1] = f[x][1][1] = p[x][1] = k[x][1] = inf;
}
f[x][0][1] = f[x][1][1] = inf;
}
int cs[nmax];
int cmp(int x, int y)
{
return a[x] < a[y];
}
int main()
{
begintime = clock();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(ck, false, sizeof(ck));
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
while (m--)
{
cin >> u >> v;
l[u].push_back(v);
l[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!ck[i])
{
l[0].push_back(i);
discover(i, 0);
}
for (int i = 1; i <= n; i++)
cs[i] = i;
sort(cs + 1, cs + n + 1, cmp);
a[0] = inf;
dfs(0, 0);
vt.push_back(0);
vt.push_back(0);
for (int i = 2; i <= n; i++)
vt.push_back(f[0][0][i]);
for (int i = n - 1; i >= 0; i--)
vt[i] = min(vt[i], vt[i + 1]);
cin >> q;
while (q--)
{
cin >> m;
u = upper_bound(vt.begin(), vt.end(), m) - vt.begin() - 1;
if (u == 1)
u--;
cout << u << '\n';
}
return 0;
}
Compilation message
dzumbus.cpp: In function 'void dfs(int, int)':
dzumbus.cpp:38:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
38 | for (int tmp : l[x])
| ^~~
dzumbus.cpp:59:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
59 | f[x][0][1] = f[x][1][1] = inf;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
32076 KB |
Output is correct |
2 |
Correct |
28 ms |
32152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
32076 KB |
Output is correct |
2 |
Correct |
28 ms |
32152 KB |
Output is correct |
3 |
Correct |
73 ms |
34252 KB |
Output is correct |
4 |
Correct |
74 ms |
34936 KB |
Output is correct |
5 |
Correct |
68 ms |
34440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
4348 KB |
Output is correct |
2 |
Correct |
44 ms |
4072 KB |
Output is correct |
3 |
Correct |
63 ms |
4556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
32076 KB |
Output is correct |
2 |
Correct |
28 ms |
32152 KB |
Output is correct |
3 |
Correct |
73 ms |
34252 KB |
Output is correct |
4 |
Correct |
74 ms |
34936 KB |
Output is correct |
5 |
Correct |
68 ms |
34440 KB |
Output is correct |
6 |
Correct |
44 ms |
4348 KB |
Output is correct |
7 |
Correct |
44 ms |
4072 KB |
Output is correct |
8 |
Correct |
63 ms |
4556 KB |
Output is correct |
9 |
Correct |
62 ms |
34068 KB |
Output is correct |
10 |
Correct |
55 ms |
34728 KB |
Output is correct |
11 |
Correct |
70 ms |
34392 KB |
Output is correct |