#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int inf = 1e9;
int n, a[MAXN];
vector<int> gph[MAXN];
int dp[MAXN][3], par[MAXN];
void dfs(int x){
for(auto &i : gph[x]){
gph[i].erase(find(gph[i].begin(), gph[i].end(), x));
par[i] = x;
dfs(i);
}
}
void initialize(int N, std::vector<int> A, std::vector<int> B) {
n = N;
for(int i=0; i<n-1; i++){
gph[A[i]].push_back(B[i]);
gph[B[i]].push_back(A[i]);
}
dfs(1);
}
void cons(int v){
if(a[v] == 0){
memset(dp[v], 0, sizeof(dp[v]));
for(auto &i : gph[v]){
dp[v][0] += min({dp[i][0], dp[i][1] + 1, dp[i][2] + 1});
dp[v][1] += min({dp[i][0], dp[i][1], dp[i][2] + 1});
dp[v][2] += min({dp[i][0], dp[i][1] + 1, dp[i][2]});
}
}
else{
memset(dp[v], 0x3f, sizeof(dp[v]));
dp[v][a[v]] = 0;
for(auto &i : gph[v]){
dp[v][a[v]] += min({dp[i][0], dp[i][1] + (a[v] == 2), dp[i][2] + (a[v] == 1)});
}
}
}
int cat(int v) {
a[v] = 1;
while(v){
cons(v);
v = par[v];
}
return min({dp[1][0], dp[1][1], dp[1][2]});
}
int dog(int v) {
a[v] = 2;
while(v){
cons(v);
v = par[v];
}
return min({dp[1][0], dp[1][1], dp[1][2]});
}
int neighbor(int v) {
a[v] = 0;
while(v){
cons(v);
v = par[v];
}
return min({dp[1][0], dp[1][1], dp[1][2]});
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
5 ms |
2688 KB |
Output is correct |
5 |
Correct |
5 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2688 KB |
Output is correct |
7 |
Correct |
4 ms |
2688 KB |
Output is correct |
8 |
Correct |
4 ms |
2688 KB |
Output is correct |
9 |
Correct |
5 ms |
2688 KB |
Output is correct |
10 |
Correct |
5 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
5 ms |
2660 KB |
Output is correct |
13 |
Correct |
5 ms |
2688 KB |
Output is correct |
14 |
Correct |
5 ms |
2688 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
5 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
5 ms |
2688 KB |
Output is correct |
5 |
Correct |
5 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2688 KB |
Output is correct |
7 |
Correct |
4 ms |
2688 KB |
Output is correct |
8 |
Correct |
4 ms |
2688 KB |
Output is correct |
9 |
Correct |
5 ms |
2688 KB |
Output is correct |
10 |
Correct |
5 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
5 ms |
2660 KB |
Output is correct |
13 |
Correct |
5 ms |
2688 KB |
Output is correct |
14 |
Correct |
5 ms |
2688 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
5 ms |
2688 KB |
Output is correct |
17 |
Correct |
5 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
5 ms |
2688 KB |
Output is correct |
22 |
Correct |
7 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
7 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
5 ms |
2688 KB |
Output is correct |
27 |
Correct |
5 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
11 ms |
2816 KB |
Output is correct |
30 |
Correct |
7 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
7 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
5 ms |
2688 KB |
Output is correct |
5 |
Correct |
5 ms |
2684 KB |
Output is correct |
6 |
Correct |
4 ms |
2688 KB |
Output is correct |
7 |
Correct |
4 ms |
2688 KB |
Output is correct |
8 |
Correct |
4 ms |
2688 KB |
Output is correct |
9 |
Correct |
5 ms |
2688 KB |
Output is correct |
10 |
Correct |
5 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
5 ms |
2660 KB |
Output is correct |
13 |
Correct |
5 ms |
2688 KB |
Output is correct |
14 |
Correct |
5 ms |
2688 KB |
Output is correct |
15 |
Correct |
5 ms |
2688 KB |
Output is correct |
16 |
Correct |
5 ms |
2688 KB |
Output is correct |
17 |
Correct |
5 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
5 ms |
2688 KB |
Output is correct |
22 |
Correct |
7 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
7 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
5 ms |
2688 KB |
Output is correct |
27 |
Correct |
5 ms |
2688 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
11 ms |
2816 KB |
Output is correct |
30 |
Correct |
7 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
7 ms |
2688 KB |
Output is correct |
33 |
Correct |
454 ms |
7976 KB |
Output is correct |
34 |
Correct |
173 ms |
7672 KB |
Output is correct |
35 |
Correct |
605 ms |
7124 KB |
Output is correct |
36 |
Correct |
837 ms |
11452 KB |
Output is correct |
37 |
Correct |
20 ms |
5116 KB |
Output is correct |
38 |
Correct |
883 ms |
12328 KB |
Output is correct |
39 |
Correct |
906 ms |
12308 KB |
Output is correct |
40 |
Correct |
1077 ms |
12388 KB |
Output is correct |
41 |
Correct |
1101 ms |
12408 KB |
Output is correct |
42 |
Correct |
1591 ms |
12392 KB |
Output is correct |
43 |
Correct |
1265 ms |
12392 KB |
Output is correct |
44 |
Correct |
1239 ms |
12288 KB |
Output is correct |
45 |
Correct |
2203 ms |
12396 KB |
Output is correct |
46 |
Correct |
1075 ms |
12392 KB |
Output is correct |
47 |
Correct |
928 ms |
12364 KB |
Output is correct |
48 |
Execution timed out |
3009 ms |
9536 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |