bzoj 1103: [POI2007]大都市meg (dfs序)

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

type
arr=record
toward,next:longint;
end;
const
maxn=;
var
edge:array[..maxn]of arr;
bit,numin,numout,first,deep:array[..maxn]of longint;
chose:array[..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> do begin
too:=edge[i].toward;
if chose[too] then begin
deep[too]:=deep[x]+;
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<< do begin
inc(bit[x],y);
inc(x,lowbit(x));
end;
end; function ask(x:longint):longint;
var
ans:longint;
begin
ans:=;
while x>= 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:= to n- do begin
readln(j,k);
addedge(j,k);
addedge(k,j);
end;
fillchar(chose,sizeof(chose),true);
deep[]:=;
dfs();
//for i:= to n do writeln(i,' ',numin[i],' ',numout[i]);
for i:= to n do begin
add(numin[i],);
add(numout[i],-);
end;
end; procedure work;
var
i,j,m,k:longint;
ch:char;
begin
readln(m);
while m> 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],-);
add(numout[j],);
end;
end;
end; begin
into;
work;
end.
上一篇:[教程]怎么用百度云观看和下载"磁力链接"无需下载直接观看.


下一篇:Java程序员的日常 —— 《编程思想》持有对象