#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
int n;
vector<int> graph[500000];
int x[500000];
pair<int, int> calc(int v, int p)
{
pair<int, int> ans0 = {0, 0};
for (auto u : graph[v]) if (u != p)
{
pair<int, int> ans1 = calc(u, v);
ans0.first += min(ans1.first, ans1.second + 1);
ans0.second += min(ans1.first + 1, ans1.second);
}
if (x[v] == 0) ans0.second = INF;
if (x[v] == 1) ans0.first = INF;
return ans0;
}
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;
}
}
int cat(int v)
{
v--;
x[v] = 0;
return min(calc(0, 0).first, calc(0, 0).second);
}
int dog(int v)
{
v--;
x[v] = 1;
return min(calc(0, 0).first, calc(0, 0).second);
}
int neighbor(int v)
{
v--;
x[v] = -1;
return min(calc(0, 0).first, calc(0, 0).second);
}
#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
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
5 |
Correct |
9 ms |
12032 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
8 ms |
12120 KB |
Output is correct |
9 |
Correct |
8 ms |
12032 KB |
Output is correct |
10 |
Correct |
7 ms |
12032 KB |
Output is correct |
11 |
Correct |
9 ms |
12032 KB |
Output is correct |
12 |
Correct |
9 ms |
12032 KB |
Output is correct |
13 |
Correct |
7 ms |
12032 KB |
Output is correct |
14 |
Correct |
9 ms |
12032 KB |
Output is correct |
15 |
Correct |
8 ms |
12032 KB |
Output is correct |
16 |
Correct |
9 ms |
12032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
5 |
Correct |
9 ms |
12032 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
8 ms |
12120 KB |
Output is correct |
9 |
Correct |
8 ms |
12032 KB |
Output is correct |
10 |
Correct |
7 ms |
12032 KB |
Output is correct |
11 |
Correct |
9 ms |
12032 KB |
Output is correct |
12 |
Correct |
9 ms |
12032 KB |
Output is correct |
13 |
Correct |
7 ms |
12032 KB |
Output is correct |
14 |
Correct |
9 ms |
12032 KB |
Output is correct |
15 |
Correct |
8 ms |
12032 KB |
Output is correct |
16 |
Correct |
9 ms |
12032 KB |
Output is correct |
17 |
Correct |
20 ms |
12160 KB |
Output is correct |
18 |
Correct |
27 ms |
12160 KB |
Output is correct |
19 |
Correct |
15 ms |
12160 KB |
Output is correct |
20 |
Correct |
10 ms |
12032 KB |
Output is correct |
21 |
Correct |
9 ms |
12032 KB |
Output is correct |
22 |
Correct |
10 ms |
12144 KB |
Output is correct |
23 |
Correct |
29 ms |
12160 KB |
Output is correct |
24 |
Correct |
24 ms |
12180 KB |
Output is correct |
25 |
Correct |
14 ms |
12032 KB |
Output is correct |
26 |
Correct |
12 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12032 KB |
Output is correct |
28 |
Correct |
12 ms |
12160 KB |
Output is correct |
29 |
Correct |
29 ms |
12160 KB |
Output is correct |
30 |
Correct |
10 ms |
12032 KB |
Output is correct |
31 |
Correct |
12 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
8 ms |
12032 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
8 ms |
12032 KB |
Output is correct |
5 |
Correct |
9 ms |
12032 KB |
Output is correct |
6 |
Correct |
8 ms |
12032 KB |
Output is correct |
7 |
Correct |
8 ms |
12032 KB |
Output is correct |
8 |
Correct |
8 ms |
12120 KB |
Output is correct |
9 |
Correct |
8 ms |
12032 KB |
Output is correct |
10 |
Correct |
7 ms |
12032 KB |
Output is correct |
11 |
Correct |
9 ms |
12032 KB |
Output is correct |
12 |
Correct |
9 ms |
12032 KB |
Output is correct |
13 |
Correct |
7 ms |
12032 KB |
Output is correct |
14 |
Correct |
9 ms |
12032 KB |
Output is correct |
15 |
Correct |
8 ms |
12032 KB |
Output is correct |
16 |
Correct |
9 ms |
12032 KB |
Output is correct |
17 |
Correct |
20 ms |
12160 KB |
Output is correct |
18 |
Correct |
27 ms |
12160 KB |
Output is correct |
19 |
Correct |
15 ms |
12160 KB |
Output is correct |
20 |
Correct |
10 ms |
12032 KB |
Output is correct |
21 |
Correct |
9 ms |
12032 KB |
Output is correct |
22 |
Correct |
10 ms |
12144 KB |
Output is correct |
23 |
Correct |
29 ms |
12160 KB |
Output is correct |
24 |
Correct |
24 ms |
12180 KB |
Output is correct |
25 |
Correct |
14 ms |
12032 KB |
Output is correct |
26 |
Correct |
12 ms |
12160 KB |
Output is correct |
27 |
Correct |
11 ms |
12032 KB |
Output is correct |
28 |
Correct |
12 ms |
12160 KB |
Output is correct |
29 |
Correct |
29 ms |
12160 KB |
Output is correct |
30 |
Correct |
10 ms |
12032 KB |
Output is correct |
31 |
Correct |
12 ms |
12160 KB |
Output is correct |
32 |
Correct |
11 ms |
12032 KB |
Output is correct |
33 |
Execution timed out |
3059 ms |
16376 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |