# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
199496 | SamAnd | Džumbus (COCI19_dzumbus) | C++17 | 92 ms | 34808 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1003, Q = 200005;
const long long INF = 1000000009000000009;
int n, m;
int d[N];
vector<int> a[N];
bool c[N];
void dfs0(int x)
{
c[x] = true;
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (c[h])
continue;
dfs0(h);
}
}
long long dp[N][N][2][2];
int q[N];
long long ndp[N][2][2];
void dfs(int x, int p)
{
c[x] = true;
q[x] = 1;
dp[x][0][0][0] = 0;
dp[x][0][1][0] = d[x];
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
if (h == p)
continue;
dfs(h, x);
for (int j = 0; j <= q[x] + q[h]; ++j)
{
for (int z1 = 0; z1 < 2; ++z1)
{
for (int z2 = 0; z2 < 2; ++z2)
ndp[j][z1][z2] = INF;
}
}
for (int j = 0; j <= q[x]; ++j)
{
for (int jj = 0; jj <= q[h]; ++jj)
{
ndp[j + jj][0][0] = min(ndp[j + jj][0][0], dp[x][j][0][0] + dp[h][jj][0][0]);
ndp[j + jj][0][0] = min(ndp[j + jj][0][0], dp[x][j][0][0] + dp[h][jj][1][0]);
ndp[j + jj][0][0] = min(ndp[j + jj][0][0], dp[x][j][0][0] + dp[h][jj][1][1]);
ndp[j + jj][1][0] = min(ndp[j + jj][1][0], dp[x][j][1][0] + dp[h][jj][0][0]);
ndp[j + jj + 2][1][1] = min(ndp[j + jj + 2][1][1], dp[x][j][1][0] + dp[h][jj][1][0]);
ndp[j + jj + 1][1][1] = min(ndp[j + jj + 1][1][1], dp[x][j][1][0] + dp[h][jj][1][1]);
ndp[j + jj][1][1] = min(ndp[j + jj][1][1], dp[x][j][1][1] + dp[h][jj][0][0]);
ndp[j + jj + 1][1][1] = min(ndp[j + jj + 1][1][1], dp[x][j][1][1] + dp[h][jj][1][0]);
ndp[j + jj][1][1] = min(ndp[j + jj][1][1], dp[x][j][1][1] + dp[h][jj][1][1]);
}
}
q[x] += q[h];
for (int j = 0; j <= q[x]; ++j)
{
for (int z1 = 0; z1 < 2; ++z1)
{
for (int z2 = 0; z2 < 2; ++z2)
{
dp[x][j][z1][z2] = ndp[j][z1][z2];
}
}
}
}
}
long long s[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%d", &d[i]);
for (int i = 0; i < m; ++i)
{
int x, y;
scanf("%d%d", &x, &y);
a[x].push_back(y);
a[y].push_back(x);
}
for (int i = 0; i <= n; ++i)
{
for (int j = 0; j <= n; ++j)
{
for (int z1 = 0; z1 < 2; ++z1)
{
for (int z2 = 0; z2 < 2; ++z2)
{
dp[i][j][z1][z2] = INF;
}
}
}
}
for (int i = 1; i <= n; ++i)
{
if (!c[i])
{
dfs0(i);
a[0].push_back(i);
}
}
dfs(0, 0);
s[n + 1] = INF;
for (int i = n; i >= 1; --i)
{
s[i] = min(dp[0][i][0][0], s[i + 1]);
}
int qq;
scanf("%d", &qq);
while (qq--)
{
int d;
scanf("%d", &d);
int l = 1, r = n;
int ans = 0;
while (l <= r)
{
int m = (l + r) / 2;
if (s[m] <= d)
{
ans = m;
l = m + 1;
}
else
r = m - 1;
}
printf("%d\n", ans);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |