답안 #97436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
97436 2019-02-16T05:25:15 Z aquablitz11 바이오칩 (IZhO12_biochips) C++14
100 / 100
204 ms 12380 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200010;
const int M = 510;

vector<int> G[N];
int val[N], dp[2][N];
int ip[N], idx[N], nxt[N], tim;

void dfs(int u) {
    int p = ++tim;
    idx[p] = u;
    ip[p] = val[u];
    for (auto v : G[u])
        dfs(v);
    nxt[p] = tim+1;
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    int r = 0;
    for (int i = 1; i <= n; ++i) {
        int p;
        scanf("%d%d", &p, &val[i]);
        if (p) G[p].push_back(i);
        else r = i;
    }
    dfs(r);
    /*for (int i = 1; i <= n; ++i)
        printf("%d ", idx[i]);
    printf("\n");
    for (int i = 1; i <= n; ++i)
        printf("%d ", nxt[i]);
    printf("\n");
    for (int i = 1; i <= n; ++i)
        printf("%d ", ip[i]);
    printf("\n");*/
    for (int i = 1; i <= m; ++i) {
        int x = i&1;
        int p = x^1;
        dp[x][n+1] = -(2e9+1);
        for (int j = n; j >= 1; --j)
            dp[x][j] = max(dp[x][j+1], dp[p][nxt[j]]+ip[j]);
    }
    printf("%d\n", dp[m&1][1]);
    
    return 0;
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
biochips.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &p, &val[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 5320 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 4992 KB Output is correct
4 Correct 13 ms 5496 KB Output is correct
5 Correct 13 ms 5496 KB Output is correct
6 Correct 13 ms 5500 KB Output is correct
7 Correct 132 ms 11004 KB Output is correct
8 Correct 159 ms 11260 KB Output is correct
9 Correct 180 ms 11868 KB Output is correct
10 Correct 204 ms 12380 KB Output is correct