Submission #199496

# Submission time Handle Problem Language Result Execution time Memory
199496 2020-02-01T15:57:06 Z SamAnd Džumbus (COCI19_dzumbus) C++17
110 / 110
92 ms 34808 KB
#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

dzumbus.cpp: In function 'void dfs0(int)':
dzumbus.cpp:15:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
dzumbus.cpp: In function 'void dfs(int, int)':
dzumbus.cpp:34:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
dzumbus.cpp: In function 'int main()':
dzumbus.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
dzumbus.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &d[i]);
         ~~~~~^~~~~~~~~~~~~
dzumbus.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &x, &y);
         ~~~~~^~~~~~~~~~~~~~~~
dzumbus.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &qq);
     ~~~~~^~~~~~~~~~~
dzumbus.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &d);
         ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31992 KB Output is correct
2 Correct 34 ms 31992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31992 KB Output is correct
2 Correct 34 ms 31992 KB Output is correct
3 Correct 83 ms 32888 KB Output is correct
4 Correct 87 ms 34808 KB Output is correct
5 Correct 92 ms 34424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 1912 KB Output is correct
2 Correct 58 ms 2936 KB Output is correct
3 Correct 65 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 31992 KB Output is correct
2 Correct 34 ms 31992 KB Output is correct
3 Correct 83 ms 32888 KB Output is correct
4 Correct 87 ms 34808 KB Output is correct
5 Correct 92 ms 34424 KB Output is correct
6 Correct 57 ms 1912 KB Output is correct
7 Correct 58 ms 2936 KB Output is correct
8 Correct 65 ms 3448 KB Output is correct
9 Correct 82 ms 34040 KB Output is correct
10 Correct 87 ms 34552 KB Output is correct
11 Correct 88 ms 34296 KB Output is correct