답안 #896674

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896674 2024-01-01T20:44:38 Z borisAngelov 바이오칩 (IZhO12_biochips) C++17
100 / 100
309 ms 13352 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];

vector<int> combine(vector<int>& a, vector<int>& b)
{
    vector<int> c(min(m + 1, int(a.size() + b.size())), -inf);

    for (int i = 0; i < a.size(); ++i)
    {
        for (int j = 0; j < b.size() && i + j <= m; ++j)
        {
            c[i + j] = max(c[i + j], a[i] + b[j]);
        }
    }

    return c;
}

vector<int> dfs(int node, int par)
{
    vector<int> dp(2, 0);

    for (int i = 0; i < tree[node].size(); ++i)
    {
        if (tree[node][i] != par)
        {
            vector<int> child = dfs(tree[node][i], node);
            dp = combine(dp, child);
        }
    }

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

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);
        }
    }

    vector<int> dp = dfs(root, -1);
    cout << dp[m] << endl;

    return 0;
}

Compilation message

biochips.cpp: In function 'std::vector<int> combine(std::vector<int>&, std::vector<int>&)':
biochips.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 0; i < a.size(); ++i)
      |                     ~~^~~~~~~~~~
biochips.cpp:22:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |         for (int j = 0; j < b.size() && i + j <= m; ++j)
      |                         ~~^~~~~~~~~~
biochips.cpp: In function 'std::vector<int> dfs(int, int)':
biochips.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |     for (int i = 0; i < tree[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 4 ms 5468 KB Output is correct
5 Correct 6 ms 5620 KB Output is correct
6 Correct 5 ms 5468 KB Output is correct
7 Correct 153 ms 11424 KB Output is correct
8 Correct 152 ms 11428 KB Output is correct
9 Correct 225 ms 12888 KB Output is correct
10 Correct 309 ms 13352 KB Output is correct