제출 #1353063

#제출 시각아이디문제언어결과실행 시간메모리
1353063Francisco_MartinTeam Coding (EGOI24_teamcoding)C++20
100 / 100
596 ms35032 KiB
//EGOI 2024 Team Coding
//https://qoj.ac/contest/1764/problem/9184

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
const ll MAXN = 1e5+5;
const ll B = 350;

vll g[MAXN], H[MAXN], top[MAXN], h(MAXN), tin(MAXN), tout(MAXN), vis(MAXN,0); ll timer=0;
void dfs(ll v,vll &A){
    if(vis[A[v]]==0) top[A[v]].push_back(v);
    tin[v]=timer++; H[h[v]].push_back(tin[v]); vis[A[v]]++;
    for(auto u:g[v]) h[u]=h[v]+1, dfs(u,A);
    tout[v]=timer-1; vis[A[v]]--;
}

vll Y(MAXN,0), temp; ll tot=0;
void dfs2(ll v,ll color,vll &A){
    Y[h[v]]++; temp.push_back(h[v]);
    if(A[v]==color) tot++;
    for(auto u:g[v]) dfs2(u,color,A);
}

int main(){
    ll n, k, a; pair<ll,ll> ans={0,0};
    cin >> n >> k;
    vll A(n), col[k];
    for(int i=0; i<n; i++) cin >> A[i], col[A[i]].push_back(i);
    for(int i=1; i<n; i++) cin >> a, g[a].push_back(i);
    h[0]=0; dfs(0,A); vll X(n,0);
    for(int i=0; i<k; i++) if(!col[i].empty()){
        vll temp2;
        for(auto v:col[i]) X[h[v]]++, temp2.push_back(h[v]);
        if((ll)col[i].size()<=B){
            for(auto v:top[i]){
                ll res=0, tot=0; sort(temp2.begin(),temp2.end()); temp2.erase(unique(temp2.begin(),temp2.end()),temp2.end());
                for(auto d:temp2){
                    auto it1=lower_bound(H[d].begin(),H[d].end(),tin[v]);
                    auto it2=upper_bound(H[d].begin(),H[d].end(),tout[v]);
                    ll x=it2-it1; res+=min(x,X[d]);
                }
                for(auto u:col[i]) if(tin[v]<=tin[u] && tout[v]>=tout[u]) tot++;
                ans=max(ans,{res,-res+tot});
            }
        }else{
            for(auto v:top[i]){
                ll res=0; tot=0; temp.clear(); dfs2(v,i,A);
                sort(temp.begin(),temp.end()); temp.erase(unique(temp.begin(),temp.end()),temp.end());
                for(auto d:temp) res+=min(X[d],Y[d]), Y[d]=0;
                ans=max(ans,{res,-res+tot});
            }
        }
        for(auto v:col[i]) X[h[v]]--;
    }
    cout << ans.first << " " << -ans.second << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...