Submission #1223885

#TimeUsernameProblemLanguageResultExecution timeMemory
1223885asli_bgTeam Coding (EGOI24_teamcoding)C++20
0 / 100
11 ms23880 KiB
#include<bits/stdc++.h>
using namespace std;

#define sp <<' '<<
#define se second
#define fi first
#define pb push_back

#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)

#define DEBUG(x) cout<<#x sp x<<endl

#define mid (l+r)/2

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;

const int MAXN=2e5+5;

int n,k;
vi a;

int p[MAXN], dep[MAXN];
vi adj[MAXN];

map<int,int> cocuk[MAXN];
map<int,int> say[MAXN];

pii ans;

int f, renk;

pii res[MAXN];

int tot[MAXN];

void merge(int nd,int kom){
    if(cocuk[nd].size()<cocuk[kom].size()){
        cocuk[nd].swap(cocuk[kom]);
        res[nd].swap(res[kom]);
        say[nd].swap(say[kom]);
    }

    for(auto el:cocuk[kom]){
        int bir=cocuk[nd][el.fi]-say[nd][el.fi]; //bende renkte olmayanlar
        int iki=tot[el.fi]-say[nd][el.fi]; //dışardakiler
        res[nd].fi-=min(bir,iki);
        res[nd].se-=min(bir,iki);

        cocuk[nd][el.fi]+=el.se;
        say[nd][el.fi]+=say[kom][el.fi];

        bir=cocuk[nd][el.fi]-say[nd][el.fi]; //bende renkte olmayanlar
        iki=tot[el.fi]-say[nd][el.fi]; //dışardakiler
        res[nd].fi+=min(bir,iki);
        res[nd].fi+=say[kom][el.fi];
        res[nd].se+=min(bir,iki);
    }
}

void dfs(int nd,int ata,int h){
    if(!f){
        dep[nd]=h;
        if(a[nd]==renk) tot[dep[nd]]++;
    }
    else{
        cocuk[nd][dep[nd]]++;
    }

    res[nd]={0,0};

    for(auto kom:adj[nd]){
        if(kom==ata) continue;
        dfs(kom,nd,h+1);
        if(f) merge(nd,kom);
    }

    if(f){
        if(a[nd]==renk){
            say[nd][dep[nd]]++; 
            res[nd].fi++;

            if(ans.fi<res[nd].fi){
                ans=res[nd];
            }
            if(ans.fi==res[nd].fi and ans.se>res[nd].se){
                ans=res[nd];
            }

            //cout<<"here" sp nd sp res[nd].fi sp res[nd].se<<endl;
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0); 

    ans={0,0};
    
    cin>>n>>k;
    a.resize(n+1);
    FORE(i,1,n+1){
        cin>>a[i];
        a[i]++;
        if(a[i]==a[1]) ans.fi++;
    }

    FORE(i,2,n+1){
        cin>>p[i];
        p[i]++;
        adj[p[i]].pb(i);
    }

    if(a[1]==1) renk=2;
    else renk=1;

    dfs(1,-1,0);

    f=1;
    dfs(1,-1,0);

    cout<<ans.fi sp ans.se<<endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...