#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
#define vt vector
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
#define debug(x) do{auto _x = x; cerr << #x << " = " << _x << endl;} while(0)
const ll INF = 1e18;
int n;
vt<vt<int>> adj;
vt<int> state;
pl dfs(int u, int par = -1) {
ll cat = 0, dog = 0;
for (int v : adj[u]) {
if (v == par) continue;
pl res = dfs(v, u);
cat += res.f;
dog += res.s;
}
pl res;
if (state[u] == 1) dog = INF;
if (state[u] == 2) cat = INF;
res = {min(cat, dog + 1), min(cat + 1, dog)};
return res;
}
int solve() {
pl res = dfs(0);
return min(res.f, res.s);
}
void initialize(int N, vt<int> A, vt<int> B) {
n = N;
adj.resize(n);
state.resize(n);
F0R (i, n - 1) {
adj[A[i] - 1].pb(B[i] - 1);
adj[B[i] - 1].pb(A[i] - 1);
}
}
int cat(int v) {
v--;
state[v] = 1;
return solve();
}
int dog(int v) {
v--;
state[v] = 2;
return solve();
}
int neighbor(int v) {
v--;
state[v] = 0;
return solve();
}
main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
vt<int> a(n - 1), b(n - 1);
F0R (i, n - 1) {
cin >> a[i] >> b[i];
}
initialize(n, a, b);
int q; cin >> q;
F0R (i, q) {
int t, u; cin >> t >> u;
int res;
if (t == 1) res = cat(u);
else if (t == 2) res = dog(u);
else if (t == 3) res = neighbor(u);
cout << res << endl;
}
}
Compilation message
catdog.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main() {
| ^~~~
catdog.cpp: In function 'int main()':
catdog.cpp:16:14: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
16 | #define endl '\n'
| ^~~~
catdog.cpp:84:13: note: 'res' was declared here
84 | int res;
| ^~~
/usr/bin/ld: /tmp/ccgiECbh.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccA0xuXi.o:catdog.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status