/*input
3
1 2 1
2 3 1
3 1 2
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int V[101010], C[101010], D[101010];
vector<int>adj[101010];
int pa[101010];
void dfs(int i, int p)
{
auto it = find(adj[i].begin(), adj[i].end(), p);
if (it != adj[i].end())
adj[i].erase(it);
pa[i] = p;
for (int j : adj[i])
{
dfs(j, i);
}
}
void initialize(int N, vector<int> A, vector<int> B)
{
for (int i = 0; i < N - 1; i++)
{
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
dfs(1, 1);
}
void recalc(int v)
{
int C_ = C[v];
int D_ = D[v];
C[v] = D[v] = 0;
for (int w : adj[v])
{
C[v] += min(C[w], D[w] + 1);
D[v] += min(D[w], C[w] + 1);
}
if (V[v] == -1)
{
D[v] = 101010;
}
if (V[v] == 1)
{
C[v] = 101010;
}
if (v != 1 && (C_ != C[v] || D_ != D[v]))
recalc(pa[v]);
}
int cat(int v)
{
V[v] = -1;
recalc(v);
return min(C[1], D[1]);
}
int dog(int v)
{
V[v] = 1;
recalc(v);
return min(C[1], D[1]);
}
int neighbor(int v)
{
V[v] = 0;
recalc(v);
return min(C[1], D[1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2812 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2680 KB |
Output is correct |
26 |
Correct |
4 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
4 ms |
2808 KB |
Output is correct |
29 |
Correct |
6 ms |
2808 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
4 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2680 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2812 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2680 KB |
Output is correct |
26 |
Correct |
4 ms |
2680 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
4 ms |
2808 KB |
Output is correct |
29 |
Correct |
6 ms |
2808 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2680 KB |
Output is correct |
33 |
Correct |
160 ms |
7776 KB |
Output is correct |
34 |
Correct |
78 ms |
7544 KB |
Output is correct |
35 |
Correct |
192 ms |
6896 KB |
Output is correct |
36 |
Correct |
279 ms |
11132 KB |
Output is correct |
37 |
Correct |
22 ms |
4984 KB |
Output is correct |
38 |
Correct |
289 ms |
11904 KB |
Output is correct |
39 |
Correct |
286 ms |
12004 KB |
Output is correct |
40 |
Correct |
339 ms |
12132 KB |
Output is correct |
41 |
Correct |
328 ms |
12040 KB |
Output is correct |
42 |
Correct |
424 ms |
11900 KB |
Output is correct |
43 |
Correct |
308 ms |
11880 KB |
Output is correct |
44 |
Correct |
355 ms |
12004 KB |
Output is correct |
45 |
Correct |
440 ms |
11920 KB |
Output is correct |
46 |
Correct |
335 ms |
12004 KB |
Output is correct |
47 |
Correct |
289 ms |
11896 KB |
Output is correct |
48 |
Execution timed out |
3013 ms |
9120 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |