답안 #334679

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
334679 2020-12-09T18:32:58 Z iulia13 바이오칩 (IZhO12_biochips) C++14
100 / 100
917 ms 390652 KB
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
int dp[505][200005];
vector <int> g[200005];
int v[200005];
int in[200005];
int ord[400005];
int out[400005];
int eul[400005];
int timp = 0;
void dfs(int nod)
{
  	timp++;
    in[nod] = timp;
    ord[timp] = nod;
    dp[1][timp] = v[nod];
    for (int i = 0; i < g[nod].size(); i++)
    {
        int x = g[nod][i];
        dfs(x);
    }
    out[in[nod]] = timp;
}
int main()
{
    int n, m, i, j, r, k, l, x;
    cin >> n >> m;
    for (i = 1; i <= n; i++)
    {
        cin >> x >> v[i];
        if (x)
            g[x].push_back(i);
        else
            r = i;
    }
    dfs(r);
    int ans = 0;
    for (j = 1; j <= m; j++)
        for (i = timp; i; i--)
        {
            dp[j][i] = max(dp[j][i], dp[j][i + 1]);
            int last = out[i] + 1;
            if (last <= timp)
            dp[j][i] = max(dp[j][i], dp[j - 1][last] + v[ord[i]]);
            if (j == m)
                ans = max(ans, dp[m][i]);
        }
    cout << ans;
    return 0;
}

Compilation message

biochips.cpp: In function 'void dfs(int)':
biochips.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int i = 0; i < g[nod].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~
biochips.cpp: In function 'int main()':
biochips.cpp:28:24: warning: unused variable 'k' [-Wunused-variable]
   28 |     int n, m, i, j, r, k, l, x;
      |                        ^
biochips.cpp:28:27: warning: unused variable 'l' [-Wunused-variable]
   28 |     int n, m, i, j, r, k, l, x;
      |                           ^
biochips.cpp:38:8: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |     dfs(r);
      |     ~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 14 ms 7788 KB Output is correct
5 Correct 16 ms 8812 KB Output is correct
6 Correct 18 ms 9836 KB Output is correct
7 Correct 456 ms 183168 KB Output is correct
8 Correct 456 ms 183148 KB Output is correct
9 Correct 665 ms 286772 KB Output is correct
10 Correct 917 ms 390652 KB Output is correct