Submission #789513

#TimeUsernameProblemLanguageResultExecution timeMemory
789513antonCats or Dogs (JOI18_catdog)C++17
0 / 100
1 ms596 KiB
#include "catdog.h" #include<bits/stdc++.h> using namespace std; #define pii pair<int, int> const int N_MAX = 1e5; int n, t[N_MAX]; bool vis[N_MAX]; vector<vector<int>> adj; vector<vector<int>> ch; void dfs(int id){ //cout<<"dfs "<<id<<endl; vis[id] = true; for(auto e: adj[id]){ if(!vis[e]){ ch[id].push_back(e); dfs(e); } } } void initialize(int N, std::vector<int> A, std::vector<int> B) { n = N; adj.resize(n); ch.resize(n); fill(t, t+N_MAX, -1); for(int i = 0; i<n-1; i++){ int a = A[i]-1; int b= B[i]-1; adj[a].push_back(b); adj[b].push_back(a); } dfs(0); } pii dp(int i){ int s= 0, oc[2] = {0, 0}; for(auto e: ch[i]){ pii r = dp(e); s+=r.first; if(r.second!=-1){ oc[r.second]++; } } pii res; if(t[i] == -1){ res = pii(s +min(oc[0], oc[1]), -1); if(oc[0]<oc[1]){ res.second = 1; } else if(oc[0]>oc[1]){ res.second = 0; } else{ res.second = -1; } } else{ res= pii(s+ oc[t[i]^1], t[i]); } //cout<<"dp "<<i<<" "<<res.first<<" "<<res.second<<endl; return res; } int cat(int v) { t[v] = 0; return dp(0).first; } int dog(int v) { t[v]=1; return dp(0).first; } int neighbor(int v) { t[v] = -1; return dp(0).first; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...