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
  • 相关阅读:
    docker 安装redis , 让宿主机可以访问
    实用工具集锦(持续更新)
    @Component, @Repository, @Service的区别
    (转) 消息队列使用的四种场景介绍
    (转) 分布式-微服务-集群的区别
    (05) SpringBoot开发RESTFull?
    (04) springboot 下的springMVC和jsp和mybatis
    oracle中delete、truncate、drop的区别 (转载)
    (03) spring Boot 的配置
    windows下用nginx配置https服务器
  • 原文地址:https://www.cnblogs.com/Macaulish/p/4358147.html
Copyright © 2011-2022 走看看