/*input
3
1 2 1
2 3 1
3 1 2
*/
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int V[101010], C[101010], D[101010];
int Sc[101010], Sd[101010];
vector<int>adj[101010];
int pa[101010];
void dfs(int i, int p)
{
pa[i] = p;
for (int j : adj[i])
{
if (j != p)
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, 0);
}
int ans = 0;
void recalc(int v)
{
int c = C[v];
int d = D[v];
Sc[pa[v]] -= C[v];
Sd[pa[v]] -= D[v];
C[v] = Sc[v];
D[v] = Sd[v];
if (V[v] == -1)
{
D[v] = C[v] + 1;
}
if (V[v] == 1)
{
C[v] = D[v] + 1;
}
C[v] = min(C[v], D[v] + 1);
D[v] = min(D[v], C[v] + 1);
Sc[pa[v]] += C[v];
Sd[pa[v]] += D[v];
if (v != 1 && (C[v] - D[v] != c - d))
recalc(pa[v]);
else if (v != 1)
{
ans += C[v] - c;
Sc[pa[v]] -= C[v] - c;
Sd[pa[v]] -= C[v] - c;
}
}
int cat(int v)
{
V[v] = -1;
recalc(v);
return ans + min(C[1], D[1]);
}
int dog(int v)
{
V[v] = 1;
recalc(v);
return ans + min(C[1], D[1]);
}
int neighbor(int v)
{
V[v] = 0;
recalc(v);
return ans + 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 |
5 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 |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
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 |
5 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 |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
6 ms |
2680 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2872 KB |
Output is correct |
29 |
Correct |
6 ms |
2812 KB |
Output is correct |
30 |
Correct |
4 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2780 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 |
5 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 |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
5 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
6 ms |
2680 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2872 KB |
Output is correct |
29 |
Correct |
6 ms |
2812 KB |
Output is correct |
30 |
Correct |
4 ms |
2680 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2780 KB |
Output is correct |
33 |
Correct |
67 ms |
7072 KB |
Output is correct |
34 |
Correct |
51 ms |
7160 KB |
Output is correct |
35 |
Correct |
59 ms |
6128 KB |
Output is correct |
36 |
Correct |
106 ms |
10044 KB |
Output is correct |
37 |
Correct |
22 ms |
4984 KB |
Output is correct |
38 |
Correct |
123 ms |
10728 KB |
Output is correct |
39 |
Correct |
124 ms |
10704 KB |
Output is correct |
40 |
Correct |
116 ms |
10724 KB |
Output is correct |
41 |
Correct |
114 ms |
10724 KB |
Output is correct |
42 |
Correct |
113 ms |
10704 KB |
Output is correct |
43 |
Correct |
116 ms |
10724 KB |
Output is correct |
44 |
Correct |
119 ms |
10776 KB |
Output is correct |
45 |
Correct |
121 ms |
10900 KB |
Output is correct |
46 |
Correct |
121 ms |
10728 KB |
Output is correct |
47 |
Correct |
112 ms |
10812 KB |
Output is correct |
48 |
Correct |
64 ms |
8156 KB |
Output is correct |
49 |
Correct |
74 ms |
9248 KB |
Output is correct |
50 |
Correct |
24 ms |
4344 KB |
Output is correct |
51 |
Correct |
32 ms |
5340 KB |
Output is correct |
52 |
Correct |
17 ms |
4092 KB |
Output is correct |
53 |
Correct |
93 ms |
9596 KB |
Output is correct |
54 |
Correct |
46 ms |
5936 KB |
Output is correct |
55 |
Correct |
97 ms |
8560 KB |
Output is correct |
56 |
Correct |
64 ms |
6688 KB |
Output is correct |
57 |
Correct |
129 ms |
9452 KB |
Output is correct |
58 |
Correct |
25 ms |
5292 KB |
Output is correct |
59 |
Correct |
31 ms |
5240 KB |
Output is correct |
60 |
Correct |
68 ms |
8572 KB |
Output is correct |
61 |
Correct |
74 ms |
8800 KB |
Output is correct |
62 |
Correct |
51 ms |
7376 KB |
Output is correct |
63 |
Execution timed out |
3023 ms |
6984 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |