全wa,挑不出来了,还请大佬帮助wuuuu
查看原帖
全wa,挑不出来了,还请大佬帮助wuuuu
640304
ThreeTB楼主2023/8/10 22:02
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N =510;
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
#define endl '\n'
typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1}; // 马
int dy[8] = {-2, -1, 1, 2, 2, 1, -1, -2}; // 马

vector<int> graph[N];
int n, m;
int dfu[N], low[N];
int scc_cnt = 0, timestamp = 0;
int in_stk[N];
stack<int> st;
vector<int> ans[N];
int dp[N][1100];
int w[N], v[N];
int SizeW[N], SizeV[N];
int id[N];
int in[N];
pair<int, int> node[N];
void tar(int x) // 缩点
{
    dfu[x] = low[x] = ++timestamp;
    st.push(x);
    in_stk[x] = true;
    for (auto lb : graph[x])
    {
        if (!dfu[lb])
        {
            tar(lb);
            low[x] = min(low[x], low[lb]);
        }
        else if (in_stk[lb])
        {
            low[x] = min(low[x], dfu[lb]);
        }
    }
    if (dfu[x] == low[x])
    {
        ++scc_cnt;
        int temp;
        do
        {
            temp = st.top();
            st.pop();
            in_stk[temp] = false;
            id[temp] = scc_cnt;
            SizeW[scc_cnt] += w[temp];
            SizeV[scc_cnt] += v[temp];
        } while (temp != x);
    }
}
void dfs(int x,int sum) 
{
    if(sum<=0) return ;
    for(auto lb:graph[x]) 
    {
        for(int j=0;j<=sum-SizeW[lb];j++) 
        dp[lb][j]=dp[x][j];
        dfs(lb,sum-SizeW[lb]);
        for(int j=SizeW[lb];j<=sum;j++) 
        {
            dp[x][j]=max(dp[x][j],dp[lb][j-SizeW[lb]]+SizeV[lb]);
        }
    }
}
void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> w[i];
    for (int i = 1; i <= n; i++)
        cin >> v[i];
    rep(i, 1, n)
    {
        int x;
        cin >> x;
        if (x == 0)
            continue;
        else graph[x].pb(i);
    }
    for (int i = 1; i <= n; i++)
    {
        if (!dfu[i])
        {
            tar(i);
        }
    }
    rep(i, 0, n+1) graph[i].clear();
    // for (int i = 1; i <= n; i++)
    // {
    //     int u = node[i].first;
    //     int v = node[i].second;
    //     if (u == 0)
    //         continue;
    //     if (id[u] != id[v])
    //     {
    //         graph[id[u]].push_back(id[v]);
    //         in[id[v]]++;
    //     }
    // }
    for(int i=1;i<=n;i++) 
    {
        for(auto j:graph[i]) 
        {
            int u=id[i],v=id[j];
            if(u!=v) 
            {
                in[v]++;
                graph[u].pb(v);
            }
        }
    }
    for(int i=1;i<=scc_cnt;i++)
    {
        if(!in[i]) 
        {
            graph[0].pb(i);
        }
        // cout<<SizeV[i]<<" "<<SizeW[i]<<endl;
    }
    dfs(0,m);
    cout<<dp[0][m]<<endl;
}
signed main()
{

    bool multi_case = 0;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (multi_case)
    {
        int t;
        cin >> t;
        while (t--)
        {
            solve();
        }
    }
    else
    {
        solve();
    }
    return 0;
}
2023/8/10 22:02
加载中...