#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k, l) for(int i = (j); i < (k); i += (l))
#define FORD(i, j, k, l) for(int i = (j); i >= (k); i += (l))
#define REP(i, n) FOR(i, 0, n, 1)
#define REPD(i, n) FORD(i, n, 0, 1)
typedef long long ll;
#define pb push_back
int n;
vector<array<int, 3>> dp; // nothing; dog; cat;
vector<vector<int>> g;
vector<int> in;
const int INF = (int)1e9;
void solve(int v, int p){
dp[v] = {0, 0, 0};
for(int x : g[v]){
if(x == p)
continue;
solve(x, v);
}
for(int x : g[v]){
if(x == p)
continue;
dp[v][0] += min({dp[x][0], dp[x][1] + 1, dp[x][2] + 1});
}
for(int x : g[v]){
if(x == p)
continue;
dp[v][1] += min({dp[x][0], dp[x][1], dp[x][2] + 1});
}
for(int x : g[v]){
if(x == p)
continue;
dp[v][2] += min({dp[x][0], dp[x][1] + 1, dp[x][2]});
}
if(in[v] == 1){
dp[v][0] = dp[v][2] = INF;
}
if(in[v] == 2){
dp[v][0] = dp[v][1] = INF;
}
}
int get(){
solve(0, -1);
return min({dp[0][0], dp[0][1], dp[0][2]});
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N;
dp.resize(n);
g.resize(n);
in.resize(n, -1);
REP(i, n - 1){
g[A[i]].pb(B[i]);
g[B[i]].pb(A[i]);
}
}
int cat(int v) {
in[v] = 2;
return get();
}
int dog(int v) {
in[v] = 1;
return get();
}
int neighbor(int v) {
in[v] = -1;
return get();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |