답안 #236920

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
236920 2020-06-03T20:28:02 Z mohamedsobhi777 바이오칩 (IZhO12_biochips) C++14
0 / 100
770 ms 524288 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 7;

int n, k;
int root;
int dp[N][505];
int ans[N];
int a[N];
vector<int> adj[N];

void dfs(int x)
{

        int ret = 0;
        dp[x][0] = 0;
        for (auto u : adj[x])
        {
                dfs(u);
                for (int j = 0; j <= k; j++)
                {
                        for (int k1 = 0; k1 <= k; k1++)
                        {
                                dp[x][j] = max(dp[x][j], dp[u][k1] + dp[x][k - k1]);
                        }
                }
        }
        for (int i = k; i; i--)
        {
                dp[x][i] = max(dp[x][i], dp[x][i - 1] + a[x]);
        }
        ans[x] = max(dp[x][n], dp[x][n - 1] + a[x]);
}

int t, st[N], en[N], ver[N];
int sz[N];

int dfz(int x)
{
        sz[x] = 1;
        st[x] = ++t;
        ver[t] = x;
        for (auto u : adj[x])
        {
                sz[x] += dfz(u);
        }
        en[x] = ++t;
        return sz[x];
}

int solve(int x, int r)
{

        if (r > k)
                return -1e9;
        if (x > 2 * n)
        {
                if (r == k)
                        return 0;
                return -1e9;
        }
        if (dp[x][r] != -1)
                return dp[x][r];
        int ret = 0;
        ret = max(ret, solve(x + 1, r));
        ret = max(ret, solve(x + sz[ver[x]] * 2 , r + 1) + a[ver[x]]);
        return dp[x][r] = ret;
}

int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        //freopen("in.in", "r", stdin);
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
        {
                int u, c;
                cin >> u >> a[i];
                if (u)
                        adj[u].push_back(i);
                else
                        root = i;
        }
        memset(dp, -1, sizeof dp);
        dfz(root);
        cout << solve(1, 0);
        return 0;
}

Compilation message

biochips.cpp: In function 'void dfs(int)':
biochips.cpp:17:13: warning: unused variable 'ret' [-Wunused-variable]
         int ret = 0;
             ^~~
biochips.cpp: In function 'int main()':
biochips.cpp:80:24: warning: unused variable 'c' [-Wunused-variable]
                 int u, c;
                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 400380 KB Output is correct
2 Correct 226 ms 400508 KB Output is correct
3 Correct 205 ms 400504 KB Output is correct
4 Correct 222 ms 401656 KB Output is correct
5 Correct 243 ms 401784 KB Output is correct
6 Correct 226 ms 401912 KB Output is correct
7 Runtime error 770 ms 524288 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Halted 0 ms 0 KB -