# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
887557 | 2023-12-14T18:26:07 Z | amirhoseinfar1385 | Cats or Dogs (JOI18_catdog) | C++17 | 0 ms | 0 KB |
#include "catdog.h" #include<bits/stdc++.h> using namespace std; const int maxn=100000+10; vector<int>adj[maxn]; int val[maxn],n,inf=1e6+5; void pre(int u,int par=0){ int j=-1; for(int i=0;i<(int)adj[u].size();i++){ if(adj[u][i]!=par){ pre(adj[u][i],u); } else{ j=i; } } if(j>=0){ adj[u].erase(adj[u].begin()+j); } return ; } int res=0; pair<int,int>cal(int u){ pair<int,int>ret=make_pair(0,0); for(auto x:adj[u]){ pair<int,int>fr=cal(x); ret.first+=fr.first; ret.second+=fr.second; } int red=0,blue=0; if(val[u]==1){ res+=ret.second; return make_pair(1,0); } if(val[u]==2){ res+=ret.first; return make_pair(0,1); } res=min(res.first,res.second); if(ret.first==ret.second){ return make_pair(0,0); } if(ret.first>ret.second){ return make_pair(1,0); } return make_pair(0,1); } void initialize(int N, std::vector<int> A, std::vector<int> B) { n=N; for(int i=0;i<n-1;i++){ adj[A[i]].push_back(B[i]); adj[B[i]].push_back(A[i]); } pre(1); } int cat(int v) { res=0; val[v]=1; cal(1); return res; } int dog(int v){ res=0; val[v]=2; cal(1); return res; } int neighbor(int v) { res=0; val[v]=0; cal(1); return res; }