#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 100002;
ll n;
vector<int> graph[N];
ll x[N];
ll papa[N];
ll dp[N][2];
void dfs(int v, int p)
{
papa[v] = p;
for (auto u : graph[v]) if (u != p)
{
dfs(u, v);
}
}
int answer()
{
return min(dp[0][0], dp[0][1]);
}
void initialize(int n0, vector<int> a, vector<int> b)
{
n = n0;
for (int i = 0; i < n - 1; i++)
{
a[i]--, b[i]--;
graph[a[i]].push_back(b[i]);
graph[b[i]].push_back(a[i]);
}
for (int i = 0; i < n; i++)
{
x[i] = -1;
}
dfs(0, 0);
}
int cat(int v)
{
v--;
x[v] = 0;
pair<int, int> pdp = {dp[v][0], dp[v][1]};
dp[v][1] = INF;
while (papa[v] != v)
{
pair<int, int> sdp = {dp[papa[v]][0], dp[papa[v]][1]};
dp[papa[v]][0] -= min(pdp.first, pdp.second + 1);
dp[papa[v]][1] -= min(pdp.first + 1, pdp.second);
dp[papa[v]][0] += min(dp[v][0], dp[v][1] + 1);
dp[papa[v]][1] += min(dp[v][0] + 1, dp[v][1]);
pdp = sdp;
v = papa[v];
}
return answer();
}
int dog(int v)
{
v--;
x[v] = 1;
pair<int, int> pdp = {dp[v][0], dp[v][1]};
dp[v][0] = INF;
while (papa[v] != v)
{
pair<int, int> sdp = {dp[papa[v]][0], dp[papa[v]][1]};
dp[papa[v]][0] -= min(pdp.first, pdp.second + 1);
dp[papa[v]][1] -= min(pdp.first + 1, pdp.second);
dp[papa[v]][0] += min(dp[v][0], dp[v][1] + 1);
dp[papa[v]][1] += min(dp[v][0] + 1, dp[v][1]);
pdp = sdp;
v = papa[v];
}
return answer();
}
int neighbor(int v)
{
v--;
x[v] = -1;
pair<int, int> pdp = {dp[v][0], dp[v][1]};
dp[v][0] = 0, dp[v][1] = 0;
for (auto u : graph[v]) if (u != papa[v])
{
dp[v][0] += min(dp[u][0], dp[u][1] + 1);
dp[v][1] += min(dp[u][0] + 1, dp[u][1]);
}
while (papa[v] != v)
{
pair<int, int> sdp = {dp[papa[v]][0], dp[papa[v]][1]};
dp[papa[v]][0] -= min(pdp.first, pdp.second + 1);
dp[papa[v]][1] -= min(pdp.first + 1, pdp.second);
dp[papa[v]][0] += min(dp[v][0], dp[v][1] + 1);
dp[papa[v]][1] += min(dp[v][0] + 1, dp[v][1]);
pdp = sdp;
v = papa[v];
}
return answer();
}
#ifdef LOCAL
int main()
{
int n;
cin >> n;
vector<int> a(n - 1), b(n - 1);
for (int i = 0; i < n - 1; i++)
{
cin >> a[i] >> b[i];
}
initialize(n, a, b);
int q;
cin >> q;
while (q--)
{
int t, v;
cin >> t >> v;
if (t == 1) cout << cat(v) << endl;
if (t == 2) cout << dog(v) << endl;
if (t == 3) cout << neighbor(v) << endl;
}
}
#endif // LOCAL
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
3 ms |
2816 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
2 ms |
2816 KB |
Output is correct |
24 |
Correct |
3 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
4 ms |
2816 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2688 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
3 ms |
2816 KB |
Output is correct |
19 |
Correct |
2 ms |
2688 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
21 |
Correct |
2 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
2 ms |
2816 KB |
Output is correct |
24 |
Correct |
3 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
4 ms |
2816 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
2 ms |
2688 KB |
Output is correct |
33 |
Correct |
118 ms |
8672 KB |
Output is correct |
34 |
Correct |
63 ms |
8440 KB |
Output is correct |
35 |
Correct |
151 ms |
7468 KB |
Output is correct |
36 |
Correct |
200 ms |
12480 KB |
Output is correct |
37 |
Correct |
18 ms |
5368 KB |
Output is correct |
38 |
Correct |
233 ms |
13456 KB |
Output is correct |
39 |
Correct |
237 ms |
13416 KB |
Output is correct |
40 |
Correct |
249 ms |
13544 KB |
Output is correct |
41 |
Correct |
229 ms |
13416 KB |
Output is correct |
42 |
Correct |
291 ms |
13456 KB |
Output is correct |
43 |
Correct |
245 ms |
13544 KB |
Output is correct |
44 |
Correct |
281 ms |
13540 KB |
Output is correct |
45 |
Correct |
384 ms |
13424 KB |
Output is correct |
46 |
Correct |
245 ms |
13544 KB |
Output is correct |
47 |
Correct |
213 ms |
13540 KB |
Output is correct |
48 |
Correct |
62 ms |
10560 KB |
Output is correct |
49 |
Correct |
73 ms |
12192 KB |
Output is correct |
50 |
Correct |
23 ms |
4992 KB |
Output is correct |
51 |
Correct |
29 ms |
6492 KB |
Output is correct |
52 |
Correct |
15 ms |
4608 KB |
Output is correct |
53 |
Correct |
126 ms |
11760 KB |
Output is correct |
54 |
Correct |
71 ms |
6960 KB |
Output is correct |
55 |
Correct |
184 ms |
10736 KB |
Output is correct |
56 |
Correct |
112 ms |
7844 KB |
Output is correct |
57 |
Correct |
175 ms |
11704 KB |
Output is correct |
58 |
Correct |
387 ms |
6572 KB |
Output is correct |
59 |
Correct |
1252 ms |
6264 KB |
Output is correct |
60 |
Execution timed out |
3063 ms |
11128 KB |
Time limit exceeded |
61 |
Halted |
0 ms |
0 KB |
- |