Submission #1332337

#TimeUsernameProblemLanguageResultExecution timeMemory
1332337JuanJLKrugomet (COCI25_krugomet)C++20
42 / 70
59 ms24352 KiB
#include <bits/stdc++.h>

#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;
typedef long long ll;

#ifdef D
    #define debug(x) cout << x
#else 
    #define debug(x) // nothing
#endif

const int MAXN = 100000+5;

ll n,k;
ll a[MAXN];
ll newa[MAXN];
ll s[MAXN];

ll myciclo[MAXN];
ll myind[MAXN];
bool visa[MAXN];
bool vis[MAXN];

vector<ll> cicle;

vector<vector<ll>> regciclos;

void ciclo(ll nd){
    if(visa[nd]){
        myciclo[nd]=SZ(regciclos);
        regciclos.pb({});
        for(int i = SZ(cicle)-1; i>=0; i--){
            myciclo[cicle[i]]=myciclo[nd];
            //myind[cicle[i]]=SZ(regciclos.back());
            regciclos.back().pb(cicle[i]);
            if(cicle[i]==nd) break;
        }
        reverse(ALL(regciclos.back()));
        forn(i,0,SZ(regciclos.back())){
            myind[regciclos.back()[i]]=i;
        }
        return;
    }    
    if(vis[nd]) return;
    vis[nd]=true;
    visa[nd]=true;

    cicle.pb(nd);
    ciclo(s[nd]);

    visa[nd]=false;
}

vector<ll> radj[MAXN];
ll inpciclo[MAXN];
ll lvl[MAXN];
ll pa[MAXN];

void dfs(ll nd, ll p, ll ori, ll lv){
    if(vis[nd]) return;
    vis[nd]=true;
    inpciclo[nd]=ori;
    lvl[nd]=lv;
    pa[nd]=p;

    for(auto i:radj[nd]) if(i!=p && myciclo[i]==-1){
        dfs(i,nd,ori,lv+1);
    }
}


int main(){
    cin>>n>>k;
    forn(i,0,n) cin>>a[i], newa[i]=0;
    forn(i,0,n) cin>>s[i], s[i]--;

    mset(vis,false);
    mset(visa,false);
    mset(myciclo,-1);

    forn(i,0,n) if(!vis[i]){
        ciclo(i);
    }

    mset(vis,false);

    forn(i,0,n){
        radj[s[i]].pb(i);
    }

    forn(i,0,n) if(myciclo[i]!=-1){
        dfs(i,-1,i,0);
    }

    debug("ciclos: \n");

    forn(i,0,SZ(regciclos)){
        for(auto j:regciclos[i]) debug(j<<" "); debug('\n');
    }

    forn(i,0,n){
        debug(i<<" "<<inpciclo[i]<<" "<<myind[inpciclo[i]]<<'\n');
    }

    forn(i,0,n){
        if(lvl[i]<=k||lvl[i]==0){
            ll cicloact = myciclo[inpciclo[i]];
            ll indact = myind[inpciclo[i]];

            indact+=(k-lvl[i]);
            indact%=SZ(regciclos[cicloact]);

            debug(" Nodo i= "<<i<<" le cede "<<a[i]<<" a "<<regciclos[cicloact][indact]<<'\n');
            newa[regciclos[cicloact][indact]]+=a[i];

        }else{
            
            ll nd = i;
            forn(j,0,k){
                nd=pa[nd];
            }

            debug(" Nodo i= "<<i<<" le cede "<<a[i]<<" a "<<nd<<'\n');
            newa[nd]+=a[i];
        }
    }

    pair<ll,ll> res = {0,-1};
    forn(i,0,n){
        res=max(res,pair<ll,ll>{newa[i],(ll)i});
    }

    cout<<res.fst<<'\n';
    cout<<res.snd+1<<'\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...