#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 par[N];
int dp1[N];
int dp2[N];
int dp3[N];
int dp4[N];
int cost1[N];
int cost2[N];
vector<int> g[N];
void build(int a, int p = 0) {
par[a] = p;
vector<int> kids;
for (auto &b : g[a]) {
if (b != p) {
kids.push_back(b);
build(b, a);
}
}
g[a] = kids;
}
void dfs(int a) {
dp1[a] = 0;
dp2[a] = 0;
for (auto &b : g[a]) {
dfs(b);
dp1[a] += dp3[b];
dp2[a] += dp4[b];
}
dp1[a] += cost1[a];
dp2[a] += cost2[a];
dp3[a] = min(dp1[a], 1 + dp2[a]);
dp4[a] = min(dp2[a], 1 + dp1[a]);
}
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);
}
build(1);
}
void change(int v, int c1, int c2) {
cost1[v] = c1;
cost2[v] = c2;
dfs(1);
}
int cat(int v) {
change(v, INF, 0);
return min(dp1[1], dp2[1]);
}
int dog(int v) {
change(v, 0, INF);
return min(dp1[1], dp2[1]);
}
int neighbor(int v) {
change(v, 0, 0);
return min(dp1[1], dp2[1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2636 KB |
Output is correct |
10 |
Correct |
1 ms |
2636 KB |
Output is correct |
11 |
Correct |
1 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
1 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
1 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2636 KB |
Output is correct |
10 |
Correct |
1 ms |
2636 KB |
Output is correct |
11 |
Correct |
1 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
1 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
1 ms |
2636 KB |
Output is correct |
17 |
Correct |
11 ms |
2740 KB |
Output is correct |
18 |
Correct |
11 ms |
2748 KB |
Output is correct |
19 |
Correct |
7 ms |
2740 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
4 ms |
2636 KB |
Output is correct |
23 |
Correct |
13 ms |
2744 KB |
Output is correct |
24 |
Correct |
12 ms |
2636 KB |
Output is correct |
25 |
Correct |
5 ms |
2712 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
3 ms |
2636 KB |
Output is correct |
28 |
Correct |
5 ms |
2764 KB |
Output is correct |
29 |
Correct |
19 ms |
2844 KB |
Output is correct |
30 |
Correct |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2636 KB |
Output is correct |
32 |
Correct |
4 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
1 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
1 ms |
2636 KB |
Output is correct |
10 |
Correct |
1 ms |
2636 KB |
Output is correct |
11 |
Correct |
1 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
1 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
1 ms |
2636 KB |
Output is correct |
17 |
Correct |
11 ms |
2740 KB |
Output is correct |
18 |
Correct |
11 ms |
2748 KB |
Output is correct |
19 |
Correct |
7 ms |
2740 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
4 ms |
2636 KB |
Output is correct |
23 |
Correct |
13 ms |
2744 KB |
Output is correct |
24 |
Correct |
12 ms |
2636 KB |
Output is correct |
25 |
Correct |
5 ms |
2712 KB |
Output is correct |
26 |
Correct |
3 ms |
2636 KB |
Output is correct |
27 |
Correct |
3 ms |
2636 KB |
Output is correct |
28 |
Correct |
5 ms |
2764 KB |
Output is correct |
29 |
Correct |
19 ms |
2844 KB |
Output is correct |
30 |
Correct |
3 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2636 KB |
Output is correct |
32 |
Correct |
4 ms |
2636 KB |
Output is correct |
33 |
Execution timed out |
3039 ms |
6892 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |