#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int N;
vector<int> adj[100009];
int type_pet[100009]; // 0 = none, 1 = cat, 2 = dog
int dp[100009][2];
void dfs(int u, int p) {
dp[u][0] = dp[u][1] = 0;
for (int v : adj[u]) if (v != p) {
dfs(v, u);
dp[u][0] += min(dp[v][0], dp[v][1] + 1);
dp[u][1] += min(dp[v][1], dp[v][0] + 1);
}
if (type_pet[u] == 1) dp[u][1] = INF; // b?t bu?c là cat
if (type_pet[u] == 2) dp[u][0] = INF; // b?t bu?c là dog
}
int compute_danger() {
dfs(1, -1);
return min(dp[1][0], dp[1][1]);
}
// ========== Các hàm theo format yêu c?u ==========
void initialize(int n, vector <int> A, vector <int> B) {
N = n;
for (int i = 1; i <= N; i++) {
adj[i].clear();
type_pet[i] = 0;
}
for (int i = 0; i < N - 1; i++) {
int u = A[i], v = B[i];
adj[u].push_back(v);
adj[v].push_back(u);
}
}
int cat(int v) {
type_pet[v] = 1;
return compute_danger();
}
int dog(int v) {
type_pet[v] = 2;
return compute_danger();
}
int neighbor(int v) {
type_pet[v] = 0;
return compute_danger();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |