#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 1e5 + 7;
const int INF = (int) 1e9 + 7;
int n;
int no_cat[N];
int no_dog[N];
vector<int> g[N];
int type[N];
/**
nothing = 0
cat = 1
dog = 2
**/
void dfs(int a, int p = -1) {
if (type[a] == 0) {
no_cat[a] = no_dog[a] = 0;
for (auto &b : g[a]) {
if (b == p) {
continue;
}
dfs(b, a);
no_cat[a] += min(no_cat[b], 1 + no_dog[b]);
no_dog[a] += min(no_dog[b], 1 + no_cat[b]);
}
return;
}
if (type[a] == 1) {
no_cat[a] = INF;
no_dog[a] = 0;
for (auto &b : g[a]) {
if (b == p) {
continue;
}
dfs(b, a);
no_dog[a] += min(no_dog[b], 1 + no_cat[b]);
}
return;
}
if (type[a] == 2) {
no_cat[a] = 0;
no_dog[a] = INF;
for (auto &b : g[a]) {
if (b == p) {
continue;
}
dfs(b, a);
no_cat[a] += min(no_cat[b], 1 + no_dog[b]);
}
return;
}
assert(false);
}
void initialize(int nn, std::vector<int> edgesA, std::vector<int> edgesB) {
n = nn;
for (int i = 0; i < n - 1; i++) {
int a = edgesA[i];
int b = edgesB[i];
g[a].push_back(b);
g[b].push_back(a);
}
}
int cat(int v) {
type[v] = 1;
dfs(1);
return min(no_cat[1], no_dog[1]);
}
int dog(int v) {
type[v] = 2;
dfs(1);
return min(no_cat[1], no_dog[1]);
}
int neighbor(int v) {
type[v] = 0;
dfs(1);
return min(no_cat[1], no_dog[1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2660 KB |
Output is correct |
5 |
Correct |
2 ms |
2656 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
1 ms |
2656 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
1 ms |
2580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2660 KB |
Output is correct |
5 |
Correct |
2 ms |
2656 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
1 ms |
2656 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
1 ms |
2580 KB |
Output is correct |
17 |
Correct |
9 ms |
2640 KB |
Output is correct |
18 |
Correct |
11 ms |
2768 KB |
Output is correct |
19 |
Correct |
6 ms |
2640 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
22 |
Correct |
3 ms |
2640 KB |
Output is correct |
23 |
Correct |
12 ms |
2688 KB |
Output is correct |
24 |
Correct |
10 ms |
2720 KB |
Output is correct |
25 |
Correct |
4 ms |
2640 KB |
Output is correct |
26 |
Correct |
3 ms |
2640 KB |
Output is correct |
27 |
Correct |
3 ms |
2656 KB |
Output is correct |
28 |
Correct |
4 ms |
2660 KB |
Output is correct |
29 |
Correct |
15 ms |
2780 KB |
Output is correct |
30 |
Correct |
4 ms |
2640 KB |
Output is correct |
31 |
Correct |
3 ms |
2660 KB |
Output is correct |
32 |
Correct |
4 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2640 KB |
Output is correct |
3 |
Correct |
2 ms |
2640 KB |
Output is correct |
4 |
Correct |
2 ms |
2660 KB |
Output is correct |
5 |
Correct |
2 ms |
2656 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2640 KB |
Output is correct |
8 |
Correct |
2 ms |
2640 KB |
Output is correct |
9 |
Correct |
1 ms |
2640 KB |
Output is correct |
10 |
Correct |
1 ms |
2640 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2640 KB |
Output is correct |
13 |
Correct |
1 ms |
2656 KB |
Output is correct |
14 |
Correct |
2 ms |
2640 KB |
Output is correct |
15 |
Correct |
2 ms |
2640 KB |
Output is correct |
16 |
Correct |
1 ms |
2580 KB |
Output is correct |
17 |
Correct |
9 ms |
2640 KB |
Output is correct |
18 |
Correct |
11 ms |
2768 KB |
Output is correct |
19 |
Correct |
6 ms |
2640 KB |
Output is correct |
20 |
Correct |
2 ms |
2640 KB |
Output is correct |
21 |
Correct |
3 ms |
2640 KB |
Output is correct |
22 |
Correct |
3 ms |
2640 KB |
Output is correct |
23 |
Correct |
12 ms |
2688 KB |
Output is correct |
24 |
Correct |
10 ms |
2720 KB |
Output is correct |
25 |
Correct |
4 ms |
2640 KB |
Output is correct |
26 |
Correct |
3 ms |
2640 KB |
Output is correct |
27 |
Correct |
3 ms |
2656 KB |
Output is correct |
28 |
Correct |
4 ms |
2660 KB |
Output is correct |
29 |
Correct |
15 ms |
2780 KB |
Output is correct |
30 |
Correct |
4 ms |
2640 KB |
Output is correct |
31 |
Correct |
3 ms |
2660 KB |
Output is correct |
32 |
Correct |
4 ms |
2640 KB |
Output is correct |
33 |
Execution timed out |
3070 ms |
7156 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |