답안 #881175

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
881175 2023-11-30T19:38:11 Z alexdd 바이오칩 (IZhO12_biochips) C++17
100 / 100
478 ms 403792 KB
#include<iostream>
#include<vector>
using namespace std;
const int INF = 1e9;
int n,m,root;
vector<int> con[200005];
int a[200005];
int siz[200005];
int dp[200005][505];
void dfs(int nod)
{
    for(int i=1;i<=m;i++)
        dp[nod][i] = -INF;
    for(auto adj:con[nod])
    {
        dfs(adj);
        for(int i=min(m,siz[nod]+siz[adj]);i>0;i--)
        {
            ///j <= siz[nod]
            ///i-j <= siz[adj]  =>  j >= i - siz[adj]
            ///i-j >= 0  =>  j <= i
            if(1 || min(siz[nod],i) - max(0,i-siz[adj]) < min(siz[adj],i) - max(0,i-siz[nod]))
            {
                for(int j=max(0,i-siz[adj]);j<=min(siz[nod],i);j++)
                {
                    if(i-j<=0)
                        continue;
                    dp[nod][i] = max(dp[nod][i], dp[nod][j] + dp[adj][i-j]);
                }
            }
            else
            {
                for(int j=0;j<=i;j++)
                {
                    if(i-j>0)
                        dp[nod][i] = max(dp[nod][i], dp[nod][i-j] + dp[adj][j]);
                }
            }
        }
        siz[nod]=min(siz[nod]+siz[adj],m);
    }
    dp[nod][1] = max(dp[nod][1], a[nod]);
    siz[nod] = max(siz[nod], 1);
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>m;
    int p;
    for(int i=1;i<=n;i++)
    {
        cin>>p>>a[i];
        if(p!=0) con[p].push_back(i);
        else root = i;
    }
    dfs(root);
    cout<<dp[root][m];
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7768 KB Output is correct
2 Correct 2 ms 7768 KB Output is correct
3 Correct 4 ms 10076 KB Output is correct
4 Correct 13 ms 26692 KB Output is correct
5 Correct 8 ms 28764 KB Output is correct
6 Correct 9 ms 28764 KB Output is correct
7 Correct 309 ms 301900 KB Output is correct
8 Correct 236 ms 301900 KB Output is correct
9 Correct 369 ms 365912 KB Output is correct
10 Correct 478 ms 403792 KB Output is correct