#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<long long, long long>
#define plll pair<long long, pll>
#define tiii tuple<int, int, int>
#define tiiii tuple<int, int, int, int>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss
//#define DEBUG
const int INF = (int)1e9 + 7;
using namespace std;
int n;
int A[101010];
vector<int> gph[101010];
int par[101010];
int I[101010];
int sz[101010];
int dep[101010];
int in[101010];
int inv[101010];
int cnt;
int up[101010];
int ans;
void dfs1(int x, int p, int d)
{
par[x] = p;
sz[x] = 1;
dep[x] = d;
vector<int> tmp;
for(auto y : gph[x]) if(y != p)
{
dfs1(y, x, d + 1);
I[x] += A[y];
sz[x] += sz[y];
tmp.push_back(y);
}
gph[x].swap(tmp);
}
void dfs2(int x, int u)
{
in[x] = cnt++;
up[x] = u;
for(int i = 1; i < (int)gph[x].size(); ++i)
if(sz[gph[x][0]] < sz[gph[x][i]]) swap(gph[x][0], gph[x][i]);
if(gph[x].size()) dfs2(gph[x][0], u);
for(int i = 1; i < (int)gph[x].size(); ++i) dfs2(gph[x][i], gph[x][i]);
}
pii seg[808080];
int lazy[808080];
void prop(int ind)
{
seg[ind << 1].ff += lazy[ind];
seg[ind << 1].ss += lazy[ind];
lazy[ind << 1] += lazy[ind];
seg[ind << 1 | 1].ff += lazy[ind];
seg[ind << 1 | 1].ss += lazy[ind];
lazy[ind << 1 | 1] += lazy[ind];
lazy[ind] = 0;
}
void mrge(int ind)
{
seg[ind].ff = min(seg[ind << 1].ff, seg[ind << 1 | 1].ff);
seg[ind].ss = max(seg[ind << 1].ss, seg[ind << 1 | 1].ss);
}
void init(int ind, int s, int e)
{
if(s + 1 == e)
{
seg[ind] = { I[inv[s]] + INF * A[inv[s]], I[inv[s]] + INF * A[inv[s]] };
return;
}
int mid = s + e >> 1;
init(ind << 1, s, mid);
init(ind << 1 | 1, mid, e);
mrge(ind);
}
int upd(int ind, int s, int e, int l, int r, int lb, int ub, int x)
{
if(e <= l || r <= s) return -1;
if(l <= s && e <= r && lb <= seg[ind].ff && seg[ind].ss <= ub)
{
seg[ind].ff += x;
seg[ind].ss += x;
lazy[ind] += x;
return -1;
}
if(s + 1 == e) return s;
prop(ind);
int mid = s + e >> 1;
int t = upd(ind << 1 | 1, mid, e, l, r, lb, ub, x);
if(t == -1) t = upd(ind << 1, s, mid, l, r, lb, ub, x);
mrge(ind);
return t;
}
void pupd(int ind, int s, int e, int pos, int x, bool f)
{
if(s + 1 == e)
{
if(f) ans += max({1, seg[ind].ff, -seg[ind].ff});
seg[ind].ff = x + INF * A[inv[s]];
seg[ind].ss = x + INF * A[inv[s]];
if(f) ans -= max({1, seg[ind].ff, -seg[ind].ff});
return;
}
prop(ind);
int mid = s + e >> 1;
if(pos < mid) pupd(ind << 1, s, mid, pos, x, f);
else pupd(ind << 1 | 1, mid, e, pos, x, f);
mrge(ind);
}
int pqry(int ind, int s, int e, int pos)
{
if(s + 1 == e) return seg[ind].ff - INF * A[inv[s]];
prop(ind);
int mid = s + e >> 1;
if(pos < mid) return pqry(ind << 1, s, mid, pos);
else return pqry(ind << 1 | 1, mid, e, pos);
}
void qry(int pos, int x)
{
if(pos == -1 || x == 0) return;
int lb, ub;
if(x == 2) lb = -1, ub = -1;
if(x == 1) lb = -1, ub = 0;
if(x == -1) lb = 0, ub = 1;
if(x == -2) lb = 1, ub = 1;
int nxt = upd(1, 0, n, in[up[pos]], in[pos], lb, ub, x);
if(nxt == -1 && par[up[pos]] != -1)
{
int t = pqry(1, 0, n, in[par[up[pos]]]);
pupd(1, 0, n, in[par[up[pos]]], t + x, true);
t += INF * A[par[up[pos]]];
qry(par[up[pos]], (t + x > 0) - (t + x < 0) - (t > 0) + (t < 0));
}
else if(nxt != -1)
{
int t = pqry(1, 0, n, nxt);
pupd(1, 0, n, nxt, t + x, true);
t += INF * A[inv[nxt]];
qry(inv[nxt], (t + x > 0) - (t + x < 0) - (t > 0) + (t < 0));
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 1; i < n; ++i)
{
int x, y; cin >> x >> y; --x; --y;
gph[x].push_back(y);
gph[y].push_back(x);
}
dfs1(0, -1, 0);
dfs2(0, 0);
ans = -1;
for(int i = 0; i < n; ++i) inv[in[i]] = i;
#ifdef DEBUG
cout << endl;
cout << "up" << endl;
for(int i = 0; i < n; ++i) cout << up[i] << ' ';
cout << endl;
#endif
#ifdef DEBUG
cout << endl;
cout << "in" << endl;
for(int i = 0; i < n; ++i) cout << in[i] << ' ';
cout << endl;
#endif
int T; cin >> T;
while(T--)
{
int c; cin >> c;
if(c == 1) c = 1;
else if(c == 2) c = -1;
else c = 0;
int x; cin >> x; --x;
int t = pqry(1, 0, n, in[x]);
int del = 0;
del -= (t + INF * A[x] > 0) - (t + INF * A[x] < 0);
if(A[x] == 0) ans += max({1, t, -t});
else if(A[x] == 1) ans += t;
else ans -= t;
A[x] = c;
del += (t + INF * A[x] > 0) - (t + INF * A[x] < 0);
if(A[x] == 0) ans -= max({1, t, -t});
else if(A[x] == 1) ans -= t;
else ans += t;
pupd(1, 0, n, in[x], t, false);
qry(x, del);
#ifdef DEBUG
cout << endl;
cout << "pqry" << endl;
for(int i = 0; i < n; ++i) cout << pqry(1, 0, n, in[i]) << ' ';
cout << endl;
#endif
#ifdef DEBUG
cout << endl;
cout << "A" << endl;
for(int i = 0; i < n; ++i) cout << A[i] << ' ';
cout << endl;
#endif
int tmp = ans + ((A[0] || pqry(1, 0, n, in[0])) ? 0 : 1);
if(tmp & 1) return 0;
cout << (ans + ((A[0] || pqry(1, 0, n, in[0])) ? 0 : 1)) / 2 << '\n';
}
return 0;
}
Compilation message
catdog.cpp: In function 'void init(int, int, int)':
catdog.cpp:89:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
89 | int mid = s + e >> 1;
| ~~^~~
catdog.cpp: In function 'int upd(int, int, int, int, int, int, int, int)':
catdog.cpp:108:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
108 | int mid = s + e >> 1;
| ~~^~~
catdog.cpp: In function 'void pupd(int, int, int, int, int, bool)':
catdog.cpp:127:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
127 | int mid = s + e >> 1;
| ~~^~~
catdog.cpp: In function 'int pqry(int, int, int, int)':
catdog.cpp:138:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
138 | int mid = s + e >> 1;
| ~~^~~
/usr/bin/ld: /tmp/ccCLUDds.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccTVlsBq.o:catdog.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccCLUDds.o: in function `main':
grader.cpp:(.text.startup+0x1e2): undefined reference to `initialize(int, std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> >)'
/usr/bin/ld: grader.cpp:(.text.startup+0x219): undefined reference to `neighbor(int)'
/usr/bin/ld: grader.cpp:(.text.startup+0x25e): undefined reference to `dog(int)'
/usr/bin/ld: grader.cpp:(.text.startup+0x269): undefined reference to `cat(int)'
collect2: error: ld returned 1 exit status