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 <bits/stdc++.h>
#include "catdog.h"
using namespace std;
const int inf = 1e9;
vector<int> a;
vector<vector<int>> g;
vector<vector<int>> dp;
void initialize(int N, vector<int> A, vector<int> B)
{
g = vector<vector<int>>(N + 1);
dp = vector<vector<int>>(N + 1, vector<int>(2));
a = vector<int>(N + 1, 2);
for (int i = 1; i <= N; ++i)
{
g[A[i]].emplace_back(B[i]);
g[B[i]].emplace_back(A[i]);
}
}
void dfs(int node, int parent)
{
for (int i = 0; i < 2; ++i)
{
dp[node][i] = inf;
}
bool leaf = true;
for (auto i : g[node])
{
if (i != parent)
{
dfs(i, node);
for (int j = 0; j < 2; ++j)
{
dp[node][j] = min({dp[node][j], dp[i][j], dp[i][j ^ 1] + 1});
}
leaf = false;
}
}
if (leaf)
{
if (a[node] == 2)
{
for (int i = 0; i < 1; ++i)
{
dp[node][i] = 0;
}
}
else
{
dp[node][a[node]] = 0;
}
}
if (a[node] != 2)
{
dp[node][a[node] ^ 1] = inf;
}
}
int cat(int v)
{
a[v] = 0;
dfs(1, 0);
return min(dp[1][0], dp[1][1]);
}
int dog(int v)
{
a[v] = 1;
dfs(1, 0);
return min(dp[1][0], dp[1][1]);
}
int neighbor(int v)
{
a[v] = 2;
dfs(1, 0);
return min(dp[1][0], dp[1][1]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |