记录编号 584321 评测结果 AAAAAAAAAA
题目名称 [HAOI 2010]软件安装 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 0.004 s
提交时间 2023-11-07 20:33:34 内存使用 3.07 MiB
显示代码纯文本
#include <bits/stdc++.h> 
using namespace std;
const int N = 110,M = 1e4+10;
int n,m;
struct side{
    int nx,ver;
}e[M<<1],re[M<<1];
int hd[N],rhd[N],tot,cnt,top,num;
int low[N],dfn[N],st[N],c[N];
int v[N],w[N],va[N];
struct made{
    int v,w;
}a[N];
void add(int x,int y){
    tot++;
    e[tot].ver = y,e[tot].nx = hd[x],hd[x] = tot;
}
void radd(int x,int y){
    tot++;//重置!! 
    re[tot].ver = y,re[tot].nx = rhd[x],rhd[x] = tot;
}
void tarjan(int x){
    low[x] = dfn[x] = ++cnt;
    v[x] = 1,st[++top] = x;
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(!dfn[y])tarjan(y),low[x] = min(low[x],low[y]);
        else if(v[y])low[x] = min(low[x],dfn[y]);
    }
    if(low[x] == dfn[x]){
        ++num;int y = 0;
        do{
            y = st[top--];
            v[y] = 0,c[y] = num;
            va[num] += a[y].v;
            w[num] += a[y].w; 
        }while(x != y);
    }
}//tarjan:)

int dp[N][M];
void dfs(int x){
    for(int i = w[x];i <= m;i++)
        dp[x][i] = va[x];
    for(int i = rhd[x];i;i = re[i].nx){
        int y = re[i].ver;
        dfs(y);
        for(int j = m-w[x];j >= 0;j--)
            for(int k = 0;k <= j;k++)
                dp[x][j+w[x]] = max(dp[x][j+w[x]],dp[y][k]+dp[x][j+w[x]-k]);
    }//树上背包dp 
}
int main(){
    freopen("install.in","r",stdin);
    freopen("install.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)scanf("%d",&a[i].w);
    for(int i = 1;i <= n;i++)scanf("%d",&a[i].v);
    for(int i = 1;i <= n;i++){
        int x;scanf("%d",&x);
        if(x)add(x,i);
    }
    for(int i = 1;i <= n;i++)
        if(!dfn[i])tarjan(i);
    memset(v,0,sizeof(v));
    tot = 0;
    for(int x = 1;x <= n;x++){
        for(int i = hd[x];i;i = e[i].nx){
            int y = e[i].ver;
            if(c[x] == c[y])continue;
            radd(c[x],c[y]);
            v[c[y]]++;
        }
    }
    for(int i = 1;i <= num;i++)
        if(!v[i])radd(0,i);
    dfs(0);
    printf("%d\n",dp[0][m]);
    
    
    return 0;
    
}