bzoj1103 POI2007 Metropolitan Meg

Source: Internet
Author: User

See the next NOI water problem and then the first time I yy out of the DFS sequence this thing (of course, before listening to others have not written)

And then do a DFS-ordered water problem.

Practice:

Counts the DFS order of a point, becomes pos[i] and the maximum value of pos[i] in all points with that node as a subtree En[i]

And then modify the interval query processing with a tree-like array single point

You can think of the process of inquiry as a DFS, go to an edge ans++, return ans--, then for a point we add (pos[x],1) Add (en[x]+1,-1) to simulate this process is to remove the effect of the fork, it is possible to add a super-source connected to the number 1th point, So every time it comes through an edge, so query (Pos[i]) is the number of edges passed, but the edge of the super source cannot be calculated, so ans--。

For the modification, an edge will cause all points in his subtree to have an answer of 1, so Add (pos[x],-1) Add (en[x]+1,1)

Bzoj, however, does not have a handwritten stack

#include <iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<algorithm>using namespacestd;Const intmaxn=250000+Ten;intpos[maxn],en[maxn],clk=0;structedge{intto ; Edge*Next; Edge (intto=0, edge*next=0): To, Next (next) {}}POOL[MAXN*2],*fir[maxn],*pis=Pool;voidAddedge (int  from,intTo ) {    *pis=edge (to,fir[ from]); fir[ from]=pis++; *pis=edge ( from, Fir[to]); fir[to]=pis++;}intFA[MAXN];voidDfsintx) {pos[x]=++CLK;  for(Edge*p=fir[x];p;p=p->next) {        intV=p->to ; if(V==fa[x])Continue; FA[V]=x;    DFS (v); } En[x]=CLK;} intC[maxn],n;voidAddintXintd) { for(;0<x&&x<=n;x+=x&-x) c[x]+=D;}intQueryintx) {intret=0;  for(;0<x&&x<=n;x-=x&-x) ret+=C[x]; returnret;}intMain () {#ifdef DEBUG freopen ("In.txt","R", stdin);//freopen ("OUT.txt", "w", stdout);#endifscanf ("%d",&N);  for(intI=1; i<n;i++) {        intu,v; scanf ("%d%d",&u,&v);    Addedge (U,V); } DFS (1);  for(intI=1; i<=n;i++) {Add (Pos[i],1); Add (En[i]+1,-1); }    intm;  for(SCANF ("%d",&m); m;) {        Charopt; intx, y;  while(Opt=getchar (), opt!='A'&&opt!='W'); if(opt=='A') {scanf ("%d%d",&x,&y); if(fa[y]==×) swap (x, y); Add (Pos[x],-1); Add (En[x]+1,1); }Else{scanf ("%d",&x); printf ("%d\n", query (Pos[x])-1); M--; }    }        return 0;}
View Code

bzoj1103 POI2007 Metropolitan Meg

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.