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.
因疲惫而轻易入眠,是对自己一天努力的最好褒奖。 要和泪子一起努力怀抱梦想对抗残酷的现实……