제출 #1353265

#제출 시각아이디문제언어결과실행 시간메모리
1353265hyyhTeam Coding (EGOI24_teamcoding)C++20
50 / 100
4096 ms50484 KiB
#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <iomanip>
#include <set>
#include <bitset>
#include <unordered_map>


#define int long long

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using piii = tuple<int,int,int>;
#define f first
#define s second
#define endl '\n'
#define all(x) begin(x),end(x)

int const N = 2e5+10;
int const INF = 1e18+10;

int val[N];
map<int,int> depthcnt[N];//color -> depth,amount
vector<int> adj[N];
int depth[N];
int nodecnt[N];
map<int,int> curcnt[N];//color -> amount of stuff of in n depth in current subtree
int fullcnt[N];//amount of stuff of in n depth in current subtree
int intime[N];
int outtime[N];
int rtime[N];
pii ans = {-INF,INF};

int t = 0;

void dfs(int n){
    nodecnt[n] = 1;
    if(!depthcnt[val[n]].count(depth[n])) depthcnt[val[n]][depth[n]] = 0;
    depthcnt[val[n]][depth[n]]++;
    intime[n] = t++;
    rtime[intime[n]] = n;
    for(auto k:adj[n]){
        depth[k] = depth[n]+1;
        dfs(k);
        nodecnt[n] += nodecnt[k];
    }
    outtime[n] = t;
}

void manage(int n,int func){
    for(int i{intime[n]};i < outtime[n];i++){
        int k = rtime[i];
        if(func) curcnt[val[k]][depth[k]]++;
        else curcnt[val[k]][depth[k]]--;
    }
    for(int i{intime[n]};i < outtime[n];i++){
        int k = rtime[i];
        if(func) fullcnt[depth[k]]++;
        else fullcnt[depth[k]]--;
    }
}

pii findans(int c){
    pii ans = {0,0};
    for(auto [a,b]:depthcnt[c]){
        int val = min(fullcnt[a],b);
        ans.f += val;
        ans.s += val-curcnt[c][a];
    }
    return ans;
}

void sack1(int n,int keep){
    pii hv = {0,-1};
    for(auto k:adj[n]) hv = max(hv,{nodecnt[k],k});
    for(auto k:adj[n]) if(k != hv.s) sack1(k,0);
    if(hv.s != -1) sack1(hv.s,1);
    for(auto k:adj[n]) if(k != hv.s) manage(k,1);
    curcnt[val[n]][depth[n]]++;
    fullcnt[depth[n]]++;
    auto ret = findans(val[n]);
    ans = max(ans,{ret.f,-ret.s});
    //cout << n << " " << ret.f << " " << ret.s << endl;
    if(!keep) manage(n,0);
}

signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;cin >> n;
    int k;cin >> k;
    for(int i{};i < n;i++){
        cin >> val[i];
    }
    for(int i{1};i < n;i++){
        int g;cin >> g;
        adj[g].emplace_back(i);
    }
    dfs(0);
    // for(int i{};i < k;i++){
    //     for(auto [a,b]:depthcnt[i]) cout << a << "-" << b << " ";
    //     cout << endl;
    // }
    sack1(0,0);
    cout << ans.f << " " << -ans.s;
}
#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...