13292022-05-14 13:10:09nkdorka1212Tevefarmcpp11Elfogadva 50/5087ms31248 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

int n;
vector<vector<int>>g;
vector<bool>vis;
vector<int>row;
vector<ll>teve;

void top(int v)
{
    vis[v]=true;
    for(int x:g[v])
    {
        if(!vis[x])
        {
            top(x);
        }
    }
    row.push_back(v);
    return;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n;
    teve.resize(n+1);
    vis.resize(n+1,0);
    g.resize(n+1);
    vector<vector<int>>child(n+1);
    vector<int>db(n+1);
    vector<ll>maxi(n+1,0);
    vector<bool>kifok(n+1,0);
    for(int i=1;i<=n;i++)
    {
        cin>>teve[i];
    }
    for(int i=2;i<=n;i++)
    {
        int v;
        cin>>v;
        g[v].push_back(i);
        kifok[v]=true;
    }
    top(1);
    for(int x:row)
    {
        if(!kifok[x])
        {
            maxi[x]=teve[x];
            child[x].push_back(x);
            db[x]=1;
        }else
        {
            ll num=0;
            for(int y:g[x])
            {
                num+=maxi[y];
            }
            if(teve[x]<num)
            {
                maxi[x]=num;
                for(int y:g[x])
                {
                    child[x].push_back(y);
                    db[x]+=db[y];
                }
            }else
            {
                maxi[x]=teve[x];
                child[x].push_back(x);
                db[x]=1;
            }
        }
    }
    cout<<maxi[1]<<'\n';
    cout<<db[1]<<'\n';
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        if(child[v][0]==v)
        {
            cout<<v<<" ";
        }else
        {
            for(int x:g[v])
            {
                q.push(x);
            }
        }
    }

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/02ms1820 KiB
2Elfogadva0/02ms2056 KiB
3Elfogadva4/42ms1876 KiB
4Elfogadva4/41ms1896 KiB
5Elfogadva4/41ms1944 KiB
6Elfogadva4/41ms2144 KiB
7Elfogadva4/432ms14120 KiB
8Elfogadva6/639ms17304 KiB
9Elfogadva6/648ms20444 KiB
10Elfogadva6/670ms23552 KiB
11Elfogadva6/674ms27168 KiB
12Elfogadva6/687ms31248 KiB