#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){
A[i]--; B[i]--;
g[A[i]].pb(B[i]);
g[B[i]].pb(A[i]);
}
}
int cat(int v) {
v--;
in[v] = 2;
return get();
}
int dog(int v) {
v--;
in[v] = 1;
return get();
}
int neighbor(int v) {
v--;
in[v] = -1;
return get();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
21 ms |
352 KB |
Output is correct |
18 |
Correct |
26 ms |
332 KB |
Output is correct |
19 |
Correct |
13 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
4 ms |
296 KB |
Output is correct |
22 |
Correct |
5 ms |
292 KB |
Output is correct |
23 |
Correct |
31 ms |
332 KB |
Output is correct |
24 |
Correct |
28 ms |
360 KB |
Output is correct |
25 |
Correct |
14 ms |
340 KB |
Output is correct |
26 |
Correct |
6 ms |
296 KB |
Output is correct |
27 |
Correct |
4 ms |
204 KB |
Output is correct |
28 |
Correct |
7 ms |
332 KB |
Output is correct |
29 |
Correct |
28 ms |
440 KB |
Output is correct |
30 |
Correct |
4 ms |
332 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
6 ms |
292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
21 ms |
352 KB |
Output is correct |
18 |
Correct |
26 ms |
332 KB |
Output is correct |
19 |
Correct |
13 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
4 ms |
296 KB |
Output is correct |
22 |
Correct |
5 ms |
292 KB |
Output is correct |
23 |
Correct |
31 ms |
332 KB |
Output is correct |
24 |
Correct |
28 ms |
360 KB |
Output is correct |
25 |
Correct |
14 ms |
340 KB |
Output is correct |
26 |
Correct |
6 ms |
296 KB |
Output is correct |
27 |
Correct |
4 ms |
204 KB |
Output is correct |
28 |
Correct |
7 ms |
332 KB |
Output is correct |
29 |
Correct |
28 ms |
440 KB |
Output is correct |
30 |
Correct |
4 ms |
332 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
6 ms |
292 KB |
Output is correct |
33 |
Execution timed out |
3067 ms |
5300 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |