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;
template<typename A, typename B> string to_string(const pair<A, B>& p);
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t);
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t);
string to_string(const string& s) {
  return '"' + s + '"';
}
string to_string(const char& c) {
  return string("'") + c + "'";
}
string to_string(const char *c) {
  return to_string(string(c));
}
string to_string(const bool& b) {
  return (b ? "true" : "false");
}
string to_string(const vector<bool>& v) {
  string res = "{";
  for (int i = 0; i < (int) v.size(); ++i) {
    if (i > 0) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template<size_t T> string to_string(const bitset<T>& bs) {
  return bs.to_string();
}
template<typename T> string to_string(queue<T> q) {
  string res = "{";
  size_t sz = q.size();
  while (sz--) {
    T cur = q.front();
    q.pop();
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
  }
  res += "}";
  return res;
}
template<typename T, class C> string to_string(priority_queue<T, vector<T>, C> pq) {
  string res = "{";
  while (!pq.empty()) {
    T cur = pq.top();
    pq.pop();
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(cur);
  }
  res += "}";
  return res;
}
template<typename T> string to_string(const T& v) {
  string res = "{";
  for (auto &el : v) {
    if ((int) res.size() > 1) {
      res += ", ";
    }
    res += to_string(el);
  }
  res += "}";
  return res;
}
template<typename A, typename B> string to_string(const pair<A, B>& p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(const tuple<A, B, C>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
template<typename A, typename B, typename C, typename D> string to_string(const tuple<A, B, C, D>& t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + ')';
}
void debug_out(int size, bool first, string name) {
  vector<string> tmp = {name};
  vector<int> tmp2 = {size, first};
  cerr << endl;
}
constexpr int buffer_size = 255;
template<typename Head, typename... Tail> void debug_out(int size, bool first, string name, Head H, Tail... T) {
  string tmp;
  int off = 0;
  while ((!name.empty() && name[0] != ',') || off != 0) {
    tmp += name[0];
    name.erase(name.begin());
    char c = tmp.back();
    if (c == '{' || c == '(') {
      ++off;
    } else if (c == '}' || c == ')'){
      --off;
    }
  }
  if (!name.empty()) {
    name.erase(name.begin());
  }
  if (tmp[0] == ' ') {
    tmp.erase(tmp.begin());
  }
  string buff = to_string(H);
  if ((int) buff.size() + size + (int) tmp.size() > buffer_size - 5 && !first) {
    cerr << '\n';
    size = 0;
  }
  cerr << '[' << tmp << ": " << buff << "] ";
  debug_out(((int) buff.size() + size + (int) tmp.size() + 5) % buffer_size, false, name, T...);
}
#ifdef DEBUG
#define debug(...) cerr << "-> ", debug_out(3, true, string(#__VA_ARGS__), __VA_ARGS__)
#define here cerr << "-> " << __LINE__ << endl
#else
#define debug(...) void(37)
#define here void(37)
#endif
int n;
vector<vector<int>> g;
vector<int> a;
vector<int> ord;
int Get() {
  debug(a);
  const int inf = n;
  vector<vector<int>> dp(n, vector<int>(2));  
  for (auto v : ord) {
    for (int j = 0; j < 2; ++j) {
      int me = (a[v] == -1 ? j : a[v]);
      int &res = dp[v][j];      
      if (me != j) {
        res = inf;
        continue;
      }
      for (auto u : g[v]) {
        res += min(dp[u][j], dp[u][j ^ 1] + 1);
      }
    }
  }
  debug(dp);
  return min(dp[0][0], dp[0][1]);
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
  n = N;
  g.resize(n);
  a.resize(n, -1);
  for (int i = 0; i < n - 1; ++i) {
    --B[i], --A[i];
    g[A[i]].push_back(B[i]);
    g[B[i]].push_back(A[i]);
  }
  function<void(int)> Dfs = [&](int v) {
    for (auto u : g[v]) {
      g[u].erase(find(g[u].begin(), g[u].end(), v));
      Dfs(u);
    }
    ord.push_back(v); 
  }; 
  Dfs(0);
  debug(ord);
  debug(g);
}
int cat(int v) {
  --v;
  a[v] = 0;
  return Get();
}
int dog(int v) {
  --v;  
  a[v] = 1;
  return Get();
}
int neighbor(int v) {
  --v;  
  a[v] = -1;
  return Get();
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |