제출 #1356522

#제출 시각아이디문제언어결과실행 시간메모리
1356522yyc000123Team Coding (EGOI24_teamcoding)C++20
23 / 100
4110 ms1114112 KiB
#include<bits/stdc++.h>
using namespace std ;
#define F first
#define S second
const int N = 1e5+5 ;
int n , k , color[N] , par[N] , cnt[N] , deep[N] ;
vector<int> nei[N] , pos[N] ;
pair<int,int> ans ;

void bfs(int node){
    queue<int> q ;
    q.push(node) ;
    while(!q.empty()){
        int x = q.front() ; q.pop() ;
        pos[color[x]].push_back(deep[x]) ;
        for(int i:nei[x]){
            deep[i]=deep[x]+1 ;
            q.push(i) ;
        }
    }
}

void dfs(int node){
    vector<int> v , v1 ;
    queue<pair<int,int>> q ;
    q.push({node,0}) ;
    int temp = 0 , change = 0 ;
    while(!q.empty()){
        int x = q.front().F , d = q.front().S ; q.pop() ;
        if(v.size()<=d) v.push_back(0) , v1.push_back(0) ;
        if(color[x]==color[node]) temp++ , v1[d]++ ;
        else v[d]++ ;
        for(int i:nei[x]) q.push({i,d+1}) ;
    }
    for(int i=0 ; i<v.size() ; i++){
        // i , pos[!color[0]] deep[node]+i
        int cur = upper_bound(pos[color[node]].begin(),pos[color[node]].end(),deep[node]+i)-lower_bound(pos[color[node]].begin(),pos[color[node]].end(),deep[node]+i) ;
        temp+=min(v[i],cur-v1[i]) ; change+=min(v[i],cur-v1[i]) ;
    }
    ans = max(ans,{temp,-change}) ;
    for(int i:nei[node]) dfs(i) ;
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) ;
    cin >> n >> k ;
    for(int i=0 ; i<n ; i++) cin >> color[i] , cnt[color[i]]++ ;
    for(int i=1 ; i<n ; i++) cin >> par[i] , nei[par[i]].push_back(i) ;
    bfs(0) ; dfs(0) ;
    cout << ans.F << ' ' << -ans.S << '\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...