zoukankan      html  css  js  c++  java
  • bzoj 1103: [POI2007]大都市meg (dfs序)

    dfs序,加个bit维护前缀和就行了

    type
      arr=record
        toward,next:longint;
      end;
    const
      maxn=500005;
    var
      edge:array[0..maxn]of arr;
      bit,numin,numout,first,deep:array[0..maxn]of longint;
      chose:array[0..maxn]of boolean;
      esum,tot,n,time:longint;
     
    procedure addedge(j,k:longint);
    begin
      inc(esum);
      edge[esum].toward:=k;
      edge[esum].next:=first[j];
      first[j]:=esum;
    end;
     
    procedure dfs(x:longint);
    var
      i,too:longint;
    begin
      inc(time);
      numin[x]:=time;
      chose[x]:=false;
      i:=first[x];
      while i>0 do begin
        too:=edge[i].toward;
        if chose[too] then begin
          deep[too]:=deep[x]+1;
          dfs(too);
        end;
        i:=edge[i].next;
      end;
      inc(time);
      numout[x]:=time;
    end;
     
    function lowbit(x:longint):longint;
    begin
      exit(x and (-x));
    end;
     
    procedure add(x,y:longint);
    begin
      while x<=n<<1 do begin
        inc(bit[x],y);
        inc(x,lowbit(x));
      end;
    end;
     
    function ask(x:longint):longint;
    var
      ans:longint;
    begin
      ans:=0;
      while x>=1 do begin
        inc(ans,bit[x]);
        dec(x,lowbit(x));
      end;
      exit(ans);
    end;
     
    procedure into;
    var
      i,j,k:longint;
    begin
      readln(n);
      for i:=1 to n-1 do begin
        readln(j,k);
        addedge(j,k);
        addedge(k,j);
      end;
      fillchar(chose,sizeof(chose),true);
      deep[1]:=1;
      dfs(1);
      //for i:=1 to n do writeln(i,' ',numin[i],' ',numout[i]);
      for i:=2 to n do begin
        add(numin[i],1);
        add(numout[i],-1);
      end;
    end;
     
    procedure work;
    var
      i,j,m,k:longint;
      ch:char;
    begin
      readln(m);
      while m>0 do begin
        read(ch);
        if ch='W' then begin
          dec(m);
          readln(j);
          writeln(ask(numin[j]));
        end
        else begin
          readln(j,k);
          if deep[j]<deep[k] then j:=k;
          add(numin[j],-1);
          add(numout[j],1);
        end;
      end;
    end;
     
     
    begin
      into;
      work;
    end.
    View Code
  • 相关阅读:
    单例模式
    抽象工厂模式
    工厂方法模式
    并发编程:等待/通知机制
    并发编程:深入理解synchronized
    并发编程:为何线程的等待方法都会抛出InterruptException
    并发编程:基础知识
    Mybatis:整合Spring
    Mybatis:插件原理
    Mybatis:体系结构和工作原理
  • 原文地址:https://www.cnblogs.com/Macaulish/p/4358147.html
Copyright © 2011-2022 走看看