This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |