Submission #31935

# Submission time Handle Problem Language Result Execution time Memory
31935 2017-09-16T09:10:13 Z kazel Biochips (IZhO12_biochips) C++14
100 / 100
149 ms 21576 KB
#include<bits/stdc++.h>
using namespace std;

int val[200001], m;
vector<int> g[200001], dp[200001];

void dfs(int c)
{
    if(g[c].empty())
    {
        dp[c].push_back(val[c]);
        return;
    }
    priority_queue<tuple<int,int,int>> pq;
    for(int e : g[c])
    {
        dfs(e);
        pq.emplace(dp[e][0],e,1);
    }
    int v = 0;
    for(int t=0;t<m;t++)
    {
        if(pq.empty()) break;
        int x,y,z;
        tie(x,y,z) = pq.top();
        pq.pop();
        v += x;
        dp[c].push_back(v);
        if(dp[y].size()<=z) continue;
        x = dp[y][z]-dp[y][z-1];
        pq.emplace(x,y,z+1);
    }
    if(dp[c][0] < val[c]) dp[c][0] = val[c];
    for(int e : g[c]) dp[e].clear();
}

int main()
{
    int n, r;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int p;
        scanf("%d%d",&p,val+i);
        if(!p) r = i;
        else g[p].push_back(i);
    }
    dfs(r);
    printf("%d\n",dp[r][m-1]);
}

Compilation message

biochips.cpp: In function 'void dfs(int)':
biochips.cpp:29:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(dp[y].size()<=z) continue;
                        ^
biochips.cpp: In function 'int main()':
biochips.cpp:40:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
                        ^
biochips.cpp:44:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&p,val+i);
                               ^
biochips.cpp:49:28: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
     printf("%d\n",dp[r][m-1]);
                            ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12176 KB Output is correct
2 Correct 0 ms 12176 KB Output is correct
3 Correct 6 ms 12176 KB Output is correct
4 Correct 9 ms 12704 KB Output is correct
5 Correct 9 ms 12704 KB Output is correct
6 Correct 6 ms 12704 KB Output is correct
7 Correct 116 ms 18784 KB Output is correct
8 Correct 113 ms 18784 KB Output is correct
9 Correct 139 ms 20920 KB Output is correct
10 Correct 149 ms 21576 KB Output is correct