답안 #896646

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896646 2024-01-01T20:06:29 Z borisAngelov 바이오칩 (IZhO12_biochips) C++17
60 / 100
2000 ms 408400 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 200005;
const int maxm = 505;
const int inf = 1e9;

int n, m;

int a[maxn];

int root = -1;
vector<int> tree[maxn];

int dp[maxn][maxm];
int tmpDP[maxn][maxm];

void dfs(int node, int par)
{
    for (int i = 0; i < tree[node].size(); ++i)
    {
        if (tree[node][i] != par)
        {
            dfs(tree[node][i], node);

            for (int j = 1; j <= m; ++j)
            {
                tmpDP[node][j] = dp[node][j];
            }

            for (int j = 0; j <= m; ++j)
            {
                for (int k = 0; k <= m - j; ++k)
                {
                    tmpDP[node][j + k] = max(tmpDP[node][j + k], dp[node][j] + dp[tree[node][i]][k]);
                }
            }

            for (int j = 1; j <= m; ++j)
            {
                dp[node][j] = tmpDP[node][j];
            }
        }
    }

    dp[node][1] = max(dp[node][1], a[node]);
}

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    cin >> n >> m;

    for (int i = 1; i <= n; ++i)
    {
        int par;
        cin >> par >> a[i];

        if (par == 0)
        {
            root = i;
        }
        else
        {
            tree[par].push_back(i);
            tree[i].push_back(par);
        }
    }

    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            dp[i][j] = -inf;
        }
    }

    dfs(root, -1);

    cout << dp[root][m] << endl;

    return 0;
}

Compilation message

biochips.cpp: In function 'void dfs(int, int)':
biochips.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for (int i = 0; i < tree[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 7516 KB Output is correct
2 Correct 2 ms 7388 KB Output is correct
3 Correct 2 ms 7512 KB Output is correct
4 Correct 36 ms 45404 KB Output is correct
5 Correct 60 ms 49096 KB Output is correct
6 Correct 86 ms 48988 KB Output is correct
7 Execution timed out 2060 ms 377276 KB Time limit exceeded
8 Execution timed out 2051 ms 365140 KB Time limit exceeded
9 Execution timed out 2035 ms 372048 KB Time limit exceeded
10 Execution timed out 2037 ms 408400 KB Time limit exceeded