#include "catdog.h"
#include <bits/stdc++.h>
using namespace std;
int x;
const int MAXN = 1e5 + 15;
const int INF = 2139062143;
vector <int> adj[MAXN];
int type[MAXN];
int dp[MAXN][3];
int mn[MAXN];
void initialize(int N, std::vector<int> A, std::vector<int> B) {
x = A.size();
memset(type, 0, sizeof(type));
for (int i = 0; i < x; ++i) {
int a = A[i], b = B[i];
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
}
void DFS(int v, int p = -1) {
for (auto u : adj[v]) {
if (u != p) {
DFS(u, v);
}
}
for (int myType = 0; myType < 3; ++myType) {
if (type[v] != 0 && myType != type[v]) {continue;}
dp[v][myType] = 0;
for (auto u : adj[v]) {
if (u == p) {continue;}
//удаляю ребро
int now = mn[u] + 1;
for (int nxtType = 0; nxtType < 3; ++nxtType) {
if (dp[u][nxtType] == INF) { continue; }
// оставляю ребро
if (nxtType != 0 && myType != nxtType) { continue; }
now = min(now, dp[u][nxtType]);
}
dp[v][myType] += now;
}
}
mn[v] = min({dp[v][0], dp[v][1], dp[v][2]});
}
int Calc() {
memset(dp, 127, sizeof(dp));
memset(mn, 127, sizeof(mn));
DFS(1);
return mn[1];
}
int cat(int v) {
type[v] = 1;
return Calc();
}
int dog(int v) {
type[v] = 2;
return Calc();
}
int neighbor(int v) {
type[v] = 0;
return Calc();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4588 KB |
Output is correct |
2 |
Correct |
5 ms |
4588 KB |
Output is correct |
3 |
Correct |
5 ms |
4588 KB |
Output is correct |
4 |
Correct |
9 ms |
4588 KB |
Output is correct |
5 |
Correct |
8 ms |
4588 KB |
Output is correct |
6 |
Correct |
9 ms |
4588 KB |
Output is correct |
7 |
Correct |
9 ms |
4588 KB |
Output is correct |
8 |
Correct |
9 ms |
4588 KB |
Output is correct |
9 |
Correct |
9 ms |
4588 KB |
Output is correct |
10 |
Correct |
9 ms |
4588 KB |
Output is correct |
11 |
Correct |
8 ms |
4588 KB |
Output is correct |
12 |
Correct |
3 ms |
4588 KB |
Output is correct |
13 |
Correct |
6 ms |
4588 KB |
Output is correct |
14 |
Correct |
7 ms |
4588 KB |
Output is correct |
15 |
Correct |
7 ms |
4588 KB |
Output is correct |
16 |
Correct |
9 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4588 KB |
Output is correct |
2 |
Correct |
5 ms |
4588 KB |
Output is correct |
3 |
Correct |
5 ms |
4588 KB |
Output is correct |
4 |
Correct |
9 ms |
4588 KB |
Output is correct |
5 |
Correct |
8 ms |
4588 KB |
Output is correct |
6 |
Correct |
9 ms |
4588 KB |
Output is correct |
7 |
Correct |
9 ms |
4588 KB |
Output is correct |
8 |
Correct |
9 ms |
4588 KB |
Output is correct |
9 |
Correct |
9 ms |
4588 KB |
Output is correct |
10 |
Correct |
9 ms |
4588 KB |
Output is correct |
11 |
Correct |
8 ms |
4588 KB |
Output is correct |
12 |
Correct |
3 ms |
4588 KB |
Output is correct |
13 |
Correct |
6 ms |
4588 KB |
Output is correct |
14 |
Correct |
7 ms |
4588 KB |
Output is correct |
15 |
Correct |
7 ms |
4588 KB |
Output is correct |
16 |
Correct |
9 ms |
4588 KB |
Output is correct |
17 |
Correct |
80 ms |
4716 KB |
Output is correct |
18 |
Correct |
85 ms |
4716 KB |
Output is correct |
19 |
Correct |
47 ms |
4716 KB |
Output is correct |
20 |
Correct |
5 ms |
4588 KB |
Output is correct |
21 |
Correct |
29 ms |
4588 KB |
Output is correct |
22 |
Correct |
38 ms |
4588 KB |
Output is correct |
23 |
Correct |
107 ms |
4716 KB |
Output is correct |
24 |
Correct |
91 ms |
4716 KB |
Output is correct |
25 |
Correct |
60 ms |
4588 KB |
Output is correct |
26 |
Correct |
27 ms |
4588 KB |
Output is correct |
27 |
Correct |
24 ms |
4588 KB |
Output is correct |
28 |
Correct |
22 ms |
4844 KB |
Output is correct |
29 |
Correct |
93 ms |
4716 KB |
Output is correct |
30 |
Correct |
40 ms |
4588 KB |
Output is correct |
31 |
Correct |
12 ms |
4716 KB |
Output is correct |
32 |
Correct |
51 ms |
4588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4588 KB |
Output is correct |
2 |
Correct |
5 ms |
4588 KB |
Output is correct |
3 |
Correct |
5 ms |
4588 KB |
Output is correct |
4 |
Correct |
9 ms |
4588 KB |
Output is correct |
5 |
Correct |
8 ms |
4588 KB |
Output is correct |
6 |
Correct |
9 ms |
4588 KB |
Output is correct |
7 |
Correct |
9 ms |
4588 KB |
Output is correct |
8 |
Correct |
9 ms |
4588 KB |
Output is correct |
9 |
Correct |
9 ms |
4588 KB |
Output is correct |
10 |
Correct |
9 ms |
4588 KB |
Output is correct |
11 |
Correct |
8 ms |
4588 KB |
Output is correct |
12 |
Correct |
3 ms |
4588 KB |
Output is correct |
13 |
Correct |
6 ms |
4588 KB |
Output is correct |
14 |
Correct |
7 ms |
4588 KB |
Output is correct |
15 |
Correct |
7 ms |
4588 KB |
Output is correct |
16 |
Correct |
9 ms |
4588 KB |
Output is correct |
17 |
Correct |
80 ms |
4716 KB |
Output is correct |
18 |
Correct |
85 ms |
4716 KB |
Output is correct |
19 |
Correct |
47 ms |
4716 KB |
Output is correct |
20 |
Correct |
5 ms |
4588 KB |
Output is correct |
21 |
Correct |
29 ms |
4588 KB |
Output is correct |
22 |
Correct |
38 ms |
4588 KB |
Output is correct |
23 |
Correct |
107 ms |
4716 KB |
Output is correct |
24 |
Correct |
91 ms |
4716 KB |
Output is correct |
25 |
Correct |
60 ms |
4588 KB |
Output is correct |
26 |
Correct |
27 ms |
4588 KB |
Output is correct |
27 |
Correct |
24 ms |
4588 KB |
Output is correct |
28 |
Correct |
22 ms |
4844 KB |
Output is correct |
29 |
Correct |
93 ms |
4716 KB |
Output is correct |
30 |
Correct |
40 ms |
4588 KB |
Output is correct |
31 |
Correct |
12 ms |
4716 KB |
Output is correct |
32 |
Correct |
51 ms |
4588 KB |
Output is correct |
33 |
Execution timed out |
3068 ms |
7516 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |