This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
int b= B[i];
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |