이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
int x;
const int MX = 1000 + 7;
int val[MX], par[MX];
vector<int> adj[MX];
int dp[MX][2];
void pull(int u)
{
       dp[u][0] = dp[u][1] = 0 ; 
       if (val[u] != 2)
              dp[u][!val[u]] = MX;
       for(auto v : adj[u]){
              if(v == par[u])continue;
              for(int j = 0 ;j <2; ++ j){
                     dp[u][j] += min(dp[v][j] , dp[v][!j] + 1) ; 
              }
       }
}
void dfz(int x, int p)
{
       if (val[x] != 2)
              dp[x][!val[x]] = MX;
       par[x] = p;
       for (auto u : adj[x])
       {
              if (u == p)
                     continue;
              dfz(u, x);
       }
       pull(x) ;
}
void initialize(int N, std::vector<int> A, std::vector<int> B)
{
       x = N;
       for (int i = 0; i + 1 < N; ++i)
       {
              adj[A[i]].push_back(B[i]);
              adj[B[i]].push_back(A[i]);
       }
       fill(val + 1, val + N + 1, 2);
       dfz(1, 1);
}
void touch(int x)
{
       while (x != 1)
       {
              pull(x);
              x = par[x];
       }
       pull(1) ;
}
int solve()
{
       return min(dp[1][0], dp[1][1]);
}
int cat(int v)
{
       val[v] = 0;
       touch(v);
       return solve();
}
int dog(int v)
{
       val[v] = 1;
       touch(v);
       return solve();
}
int neighbor(int v)
{
       val[v] = 2;
       touch(v);
       return solve();
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |