27852023-01-24 19:39:35bzsofiaTevefarmcpp11Accepted 50/50115ms31972 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <string>

using namespace std;

struct adat
{
    long long tav, db, sum;
    string tevek;
    vector <long long> sz;
    bool lat;
};

long long i, n, j, akt, agdb, agsum;
queue <long long> s;
string agteve;

int main()
{
    cin>>n;
    vector <adat> x(n+1);
    //vector <vector <int>> szint(n+1);

    for (i=1; i<=n; ++i)
    {
        cin>>x[i].sum;
        x[i].db=1;
        x[i].tevek=to_string(i)+" ";
    }

    for (i=2; i<=n; ++i)
    {
        cin>>j;

        x[j].sz.push_back(i);
    }

    x[1].lat=1;
    x[1].tav=1;
    s.push(1);
    //szint[1].push_back(1);
    while (!s.empty())
    {
        akt=s.front();
        s.pop();
        for (auto &e:x[akt].sz)
        {
            if (!x[e].lat)
            {
                x[e].lat=1;
                x[e].tav=x[akt].tav+1;
                s.push(e);
                //szint[x[e].tav].push_back(e);
            }
        }
    }

    for (i=n; i>=1; --i)
    {
        agsum=0;
        agdb=0;
        agteve="";
        for (auto &e:x[i].sz)
        {
            agsum+=x[e].sum;
            agteve+=x[e].tevek;
            agdb+=x[e].db;
        }

        if (x[i].sum<agsum)
        {
            x[i].tevek=agteve;
            x[i].sum=agsum;
            x[i].db=agdb;
        }
    }

    cout<<x[1].sum<<"\n"<<x[1].db<<"\n"<<x[1].tevek;

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1812 KiB
2Accepted0/03ms2324 KiB
3Accepted4/42ms2268 KiB
4Accepted4/42ms2484 KiB
5Accepted4/42ms2856 KiB
6Accepted4/43ms3128 KiB
7Accepted4/446ms17328 KiB
8Accepted6/657ms20312 KiB
9Accepted6/667ms22788 KiB
10Accepted6/679ms25860 KiB
11Accepted6/6105ms29156 KiB
12Accepted6/6115ms31972 KiB