Test Instructions
The root node of the tree is the water source, numbered 1. The parent node of the point given number 2, 3, 4, ..., N. All leaf nodes are known to be houses.
There are Q operations, each of which can be one of the following: + V, which indicates that the house numbered V is occupied by gangsters. -V, which indicates the gangster exits the house numbered v.
All the original houses were not gangsters. For each change, the need to remove the fewest edges, so that all the houses have no gangsters to connect with the water, and on this basis, the number of affected ordinary houses are required to be the least. Thinking of solving problems
Make U1,U2,..., UK u_1, u_2,..., u_k The point that is directly connected to the water source. Obviously, the maximum value of the answer to the cut-off will not exceed K.
Consider the case of a subtree with UI u_i as the root, and if there are P gangsters (p>0), the deleted edges are always 1. To minimize the number of ordinary houses affected, it is possible to find the nearest public ancestor of P-points.
The nearest common ancestor (LCA) about how to quickly find p points, as long as the sub-tree is occupied by the rogue point of the DFS sequence the smallest point and the largest point of the LCA.
The other is to preprocess the number of houses affected by each point, each point in the corresponding subtree of the DFS sequence.
Maintains the set of points that each subtree is occupied by gangsters and the number of ordinary houses affected by each subtree in a dynamic query. Code
#include <bits/stdc++.h> using namespace std;
const int N = 100000 + 10;
const int MAXH = 20;
struct Edge {int nxt, to;} E[n*2];
int Head[n], cnt = 0;
void Addedge (int u, int v) {e[++cnt].nxt = Head[u];
E[cnt].to = v;
Head[u] = cnt; } int in[n]; Point belongs to in[i] subtrees tree int dfsq[n];
Point I in the subtree of DFS sequence int house[n];
int son[n];
Set<pair<int, int> > St[n];
Set<pair<int, int> >::iterator it; int dep[n];
Point of depth int anc[n][maxh];
void DFS (int rt) {static int stack[n];
int top = 0;
DEP[RT] = 1;
for (int i=0;i<maxh;i++) anc[rt][i] = RT;
STACK[++TOP] = RT;
while (top) {int x = Stack[top];
for (int &i=head[x];i;i=e[i].nxt) {int y = e[i].to;
Dep[y] = dep[x] + 1;
Anc[y][0] = x;
for (int i=1, z;i<maxh;i++) z = anc[y][i-1], anc[y][i] = anc[z][i-1];
Stack[++top] = y; } while (Top && head[stack[Top]]
= = 0) top--; }} void Swim (int &x, int H) {for (int i=0;
h;i++) {if (H & 1) x = Anc[x][i];
H/= 2;
}} int LCA (int x, int y) {//return value is the node number of the nearest public ancestor int i;
if (Dep[x] > Dep[y]) swap (x, y);
Swim (y, dep[y]-dep[x]);
if (x = = y) return x; for (;;)
{for (i=0;anc[x][i]! = anc[y][i];i++);
if (i = = 0) return anc[x][0];
x = Anc[x][i-1];
y = anc[y][i-1];
} return-1;
} int dfs_sequence = 0;
int init (int rt, int belong) {Dfsq[rt] = ++dfs_sequence;
IN[RT] = belong;
for (int i=head[rt];i;i=e[i].nxt) SON[RT] + = init (e[i].to, belong);
return SON[RT];
} int main () {freopen ("gangsters.in", "R", stdin);
Freopen ("Gangsters.out", "w", stdout);
int n, q;
scanf ("%d%d", &n, &q);
for (int i=1;i<=n;i++) son[i] = 1;
for (int i=2, fa;i<=n;i++) {scanf ("%d", &FA);
SON[FA] = 0;
Addedge (FA, I); } for (int i=head[1], IDX=1;I;I=E[I].NXT, idx++) init (e[i].to, IDX);
for (int i=head[1];i;i=e[i].nxt) DFS (e[i].to);
Char op;
int cntpipes = 0, cnthouses = 0;
for (int i=1, v;i<=q;i++) {scanf ("%c%d", &op, &v);
if (op = = ' + ') {cnthouses-= house[In[v]];
if (st[In[v]].size () = = 0) cntpipes++;
st[In[v]].insert (Make_pair (dfsq[v], v));
int lft = st[In[v]].begin ()->second;
int RGT = (--(it=st[In[v]].end ()))->second;
house[In[v]] = son[LCA (LFT, RGT)]-st[in[v]].size ();
Cnthouses + = house[In[v]];
} else {cnthouses-= house[In[v]];
st[In[v]].erase (Make_pair (dfsq[v], v));
if (st[In[v]].size () = = 0) {cntpipes--;
house[In[v]] = 0;
} else {int lft = st[In[v]].begin ()->second; IntRGT = (--(it=st[In[v]].end ()))->second;
house[In[v]] = son[LCA (LFT, RGT)]-st[in[v]].size ();
Cnthouses + = house[In[v]];
}} printf ("%d%d\n", Cntpipes, cnthouses); }
}