Finding the root node is easy to associate with Disjointset, but Disjointset only merges,
So ask for offline upside down, the tag will be eliminated one at a time, and then it will become merged.
Because the time of the inquiry and query, and the time the tag is in effect, records the time of the query and the mark recorded on the tree. (the default for no marking is the maximum query time +1, the root node is 0
At Find, answer the question at the time of the inquiry.
Does path compression affect the answer? It does not, because when compressed, the mark must have been invalidated.
(The answer will explode int
#include <cstdio>#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<stack>#include<vector>#include<map>#include<Set>#include<algorithm>//#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5+5;intPA[MAXN];intMRK[MAXN];intQT[MAXN],QV[MAXN];intQt;intFDST (intx) { returnMRK[X] < Qt? X:p A[x] =FDST (pa[x]);}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif intN, Q; while(SCANF ("%d%d",&n,&Q), N) { for(inti =2; I <= N; i++) {scanf ("%d", pa+H); Mrk[i]= q+1; } intc =0; for(inti =1; I <= Q; i++){ Charop[2]; intv; scanf ("%s%d",op,&v); if(*op = ='Q') {Qt[c]=i; Qv[c++] =v; }Else{Mrk[v]= min (mrk[v],i);//take the earliest effective mark } } Long LongAns =0; while(c--) {Qt=Qt[c]; Ans+=FDST (Qv[c]); } printf ("%lld\n", ans); } return 0;}
Aizu 2170 Marked Ancestor