# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1086029 | byunjaewoo | Cats or Dogs (JOI18_catdog) | C++17 | 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>
#define int long long
using namespace std;
const int N=100010, INF=1e9;
int n, q, a[N], d1[N], d2[N];
vector<int> adj[N];
void dfs(int curr, int prev) {
d1[curr]=d2[curr]=0;
for(int next:adj[curr]) if(next!=prev) {
dfs(next, curr);
d1[curr]+=min(d1[next], d2[next]+1), d2[curr]+=min(d2[next], d1[curr]+1);
}
if(a[curr]==1) d2[curr]=INF;
if(a[curr]==2) d1[curr]=INF;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for(int i=1; i<n; i++) {
int u, v; cin>>u>>v;
adj[u].push_back(v), adj[v].push_back(u);
}
cin>>q;
while(q--) {
int o, k; cin>>o>>k;
a[k]=o%3;
dfs(1, 0);
cout<<min(d1[1], d2[1])<<"\n";
}
return 0;
}