# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
847004 | LeonaRaging | Cats or Dogs (JOI18_catdog) | C++14 | 0 ms | 0 KiB |
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;
#define pb emplace_back
const int maxn = 1e5 + 4;
const int INF = 1e9;
int n, val[maxn], dp[maxn][3];
vector<int> adj[maxn];
void dfs(int u, int p) {
for (int j = 0; j < 3; j++)
dp[u][j] = 0;
for (int v : adj[u]) if (v != p) {
dfs(v, u);
dp[u][0] += min(dp[v][0], min(dp[v][1], dp[v][2]) + 1);
dp[u][1] += min(min(dp[v][0], dp[v][1]), dp[v][2] + 1);
dp[u][2] += min(min(dp[v][0], dp[v][2]), dp[v][1] + 1);
}
if (val[u] == 1) dp[u][0] = dp[u][2] = INF;
if (val[u] == 2) dp[u][0] = dp[u][1] = INF;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
int q; cin >> q;
while (q--) {
int t, u; cin >> t >> u;
if (t == 1) val[u] = 1;
if (t == 2) val[u] = 2;
if (t == 3) val[u] = 0;
dfs(1, 0);
cout << min({dp[1][0], dp[1][1], dp[1][2]}) << '\n';
}
}