Submission #199495

# Submission time Handle Problem Language Result Execution time Memory
199495 2020-02-01T15:46:49 Z SamAnd Džumbus (COCI19_dzumbus) C++17
20 / 110
84 ms 36088 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];
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];
                }
            }
        }
    }
}

vector<vector<long long> > vv;

struct ban
{
    int i;
    long long d;
};
bool operator<(const ban& a, const ban& b)
{
    return a.d < b.d;
}
int qq;
ban b[Q];

struct ban1
{
    int i, j;
    long long d;
    ban1(int i, int j, long long d)
    {
        this->i = i;
        this->j = j;
        this->d = d;
    }
};
bool operator<(const ban1& a, const ban1& b)
{
    return a.d > b.d;
}

int ans[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 = 1; 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])
        {
            dfs(i, i);
            vector<long long> v;
            for (int j = 0; j <= q[i]; ++j)
            {
                v.push_back(min(min(dp[i][j][0][0], dp[i][j][0][1]), min(dp[i][j][1][0], dp[i][j][1][1])));
            }
            for (int j = q[i] - 1; j >= 0; --j)
                v[j] = min(v[j], v[j + 1]);
            vv.push_back(v);
        }
    }
    scanf("%d", &qq);
    for (int i = 0; i < qq; ++i)
    {
        scanf("%lld", &b[i].d);
        b[i].i = i;
    }
    sort(b, b + qq);
    priority_queue<ban1> q;
    for (int i = 0; i < vv.size(); ++i)
    {
        q.push(ban1(i, 0, vv[i][1] - vv[i][0]));
    }
    long long dd = 0;
    int yans = 0;
    for (int i = 0; i < qq; ++i)
    {
        while (1)
        {
            if (q.empty())
                break;
            ban1 t = q.top();
            q.pop();
            if (dd + t.d > b[i].d)
            {
                q.push(t);
                break;
            }
            dd += t.d;
            ++yans;
            if (t.j + 1 != vv[t.i].size() - 1)
                q.push(ban1(t.i, t.j + 1, vv[t.i][t.j + 2] - vv[t.i][t.j + 1]));
        }
        ans[b[i].i] = yans;
    }
    for (int i = 0; i < qq; ++i)
        printf("%d\n", ans[i]);
    return 0;
}

Compilation message

dzumbus.cpp: In function 'void dfs(int, int)':
dzumbus.cpp:21: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:143:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < vv.size(); ++i)
                     ~~^~~~~~~~~~~
dzumbus.cpp:164:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (t.j + 1 != vv[t.i].size() - 1)
                 ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
dzumbus.cpp:97: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:99:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &d[i]);
         ~~~~~^~~~~~~~~~~~~
dzumbus.cpp:103: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:135:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &qq);
     ~~~~~^~~~~~~~~~~
dzumbus.cpp:138:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", &b[i].d);
         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 28 ms 31992 KB Output is correct
2 Correct 29 ms 31992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 31992 KB Output is correct
2 Correct 29 ms 31992 KB Output is correct
3 Incorrect 84 ms 36088 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 56 ms 4984 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 31992 KB Output is correct
2 Correct 29 ms 31992 KB Output is correct
3 Incorrect 84 ms 36088 KB Output isn't correct
4 Halted 0 ms 0 KB -