답안 #1095080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1095080 2024-10-01T08:32:19 Z Sunbae 바이오칩 (IZhO12_biochips) C++17
100 / 100
355 ms 13916 KB
#include <bits/stdc++.h>
#define z exit(0)
typedef long long ll;
using namespace std;
const int N = 2e5 + 5;
vector<int> g[N];
ll a[N];
vector<ll> cur, nxt;
int invp[N], jump[N], ti;
void dfs(int u){
    int pre = ti++;
    invp[pre] = u;
    for(int v: g[u]) dfs(v);
    jump[pre] = ti;
}
signed main(){
    int n, K, rt; scanf("%d %d", &n, &K);
    for(int v = 0, u; v<n; ++v){
        scanf("%d %lld", &u, a+v);
        if(--u >= 0) g[u].push_back(v);
        else rt = v;
    }
    dfs(rt);    
    cur.resize(n+1); nxt.resize(n+1);
    fill(cur.begin(), cur.end(), INT_MIN);
    cur[0] = 0;
    for(int k = 0; k<K; ++k){
        fill(nxt.begin(), nxt.end(), INT_MIN);
        for(int i = 0; i<n; ++i){
            cur[i+1] = max(cur[i+1], cur[i]);
            nxt[jump[i]] = max(nxt[jump[i]], cur[i] + a[invp[i]]);
        }
        cur = nxt;
    }
    printf("%lld", *max_element(cur.begin(), cur.end()));
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:17:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     int n, K, rt; scanf("%d %d", &n, &K);
      |                   ~~~~~^~~~~~~~~~~~~~~~~
biochips.cpp:19:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         scanf("%d %lld", &u, a+v);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~
biochips.cpp:23:8: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
   23 |     dfs(rt);
      |     ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 5 ms 5468 KB Output is correct
5 Correct 6 ms 5468 KB Output is correct
6 Correct 6 ms 5668 KB Output is correct
7 Correct 196 ms 12376 KB Output is correct
8 Correct 164 ms 12372 KB Output is correct
9 Correct 293 ms 13112 KB Output is correct
10 Correct 355 ms 13916 KB Output is correct