Aizu 2170 Marked Ancestor

Source: Internet
Author: User
Tags define local

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.