답안 #90158

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
90158 2018-12-20T15:48:03 Z mirbek01 바이오칩 (IZhO12_biochips) C++17
100 / 100
1683 ms 14968 KB
# include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 2;

int n, m, rt, a[N], tin[N], tout[N], timer, cur, pre;
long long dp[2][N], x[N];
vector <int> g[N];

void dfs1(int v){
      tin[v] = ++  timer;
      for(int to : g[v]){
            dfs1(to);
      }
      tout[v] = timer;
}

void dfs(int v){
      for(int to : g[v]){
            dfs(to);
      }
      dp[cur][tout[v]] = max(dp[cur][tout[v]], dp[pre][tin[v] - 1] + x[v]);
}

int main(){
      scanf("%d %d", &n, &m);

      for(int i = 1; i <= n; i ++){
            int p;
            scanf("%d %lld", &p, x + i);
            if(!p)
                  rt = i;
            else {
                  g[p].push_back(i);
            }
      }

      dfs1(rt);

      for(int i = 1; i <= n; i ++){
            dp[1][tout[i]] = max(x[i], dp[1][tout[i]]);
      }

      for(int i = 2; i <= m; i ++){
            cur = i % 2;
            pre = cur ^ 1;
            for(int j = 1; j <= n; j ++)
                  dp[pre][j] = max(dp[pre][j - 1], dp[pre][j]);
            fill(dp[cur], dp[cur] + n + 2, -1e18);
            dfs(rt);
      }

      long long ans = 0;

      for(int i = 1; i <= n; i ++)
            ans = max(ans, dp[m % 2][i]);

      cout << ans << endl;
}

Compilation message

biochips.cpp: In function 'int main()':
biochips.cpp:27:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d %d", &n, &m);
       ~~~~~^~~~~~~~~~~~~~~~~
biochips.cpp:31:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %lld", &p, x + i);
             ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 7416 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 8 ms 7480 KB Output is correct
4 Correct 27 ms 7880 KB Output is correct
5 Correct 28 ms 8008 KB Output is correct
6 Correct 33 ms 8040 KB Output is correct
7 Correct 902 ms 13260 KB Output is correct
8 Correct 1002 ms 13304 KB Output is correct
9 Correct 1244 ms 14200 KB Output is correct
10 Correct 1683 ms 14968 KB Output is correct