#include <vector>
#include <bits/stdc++.h>
#include <iostream>
#include "catdog.h"
using namespace std;
const int NMAX = 1e5;
const int INF = 1e9;
int x , n;
vector<int> adj[NMAX+1];
int stat[NMAX+1];
int dp[3][NMAX+1];
void dfs (int nod = 1, int par = 0)
{
dp[0][nod] = 0;
dp[1][nod] = 0;
dp[2][nod] = 0;
for (int &to : adj[nod])
{
if (to != par)
{
dfs(to, nod);
if (stat[nod] != 2)
dp[stat[nod]][nod] += min(min(dp[2][to] , dp[stat[nod]][to]) , dp[stat[nod]^1][to]+1);
else {
dp[2][nod] += min({dp[0][to] , dp[1][to] , dp[2][to]});
dp[1][nod] += min({dp[1][to] , dp[2][to] , dp[0][to]+1});
dp[0][nod] += min({dp[2][to] , dp[0][to] , dp[1][to]+1});
}
}
}
if (stat[nod] != 2)
for (int i = 0; i < 3; i++)
{
if (i != stat[nod])
dp[i][nod] = INF;
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
x = A.size();
n = N;
for (int i = 0; i < n-1; i++)
{
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
for (int i = 1; i <= n; i++)
{
stat[i] = 2;
}
}
int cat(int v) {
stat[v] = 1;
dfs();
return min({dp[0][1], dp[1][1], dp[2][1]});
}
int dog(int v) {
stat[v] = 0;
dfs();
return min({dp[0][1], dp[1][1], dp[2][1]});
}
int neighbor(int v) {
stat[v] = 2;
dfs();
return min({dp[0][1], dp[1][1], dp[2][1]});
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3928 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3928 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3928 KB |
Output is correct |
2 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |