虚树模板

稍微贴个建立虚树的子过程……我觉得我写的还挺清晰的

 1 procedure solve;
 2 var k,i,ff,x:longint;
 3 begin
 4   readln(k);
 5   for i:=1 to k do
 6   begin
 7     read(h[i]);bo[h[i]]:=true;
 8   end;
 9   qsort(1,k);
10   top:=1;st[top]:=1;ttt:=0;
11   for i:=1 to k do
12   begin
13     x:=h[i];
14     if x=st[top] then continue;
15     ff:=lca(x,st[top]);
16     while (dep[ff]<dep[st[top-1]])and(top>1) do
17     begin
18       add2(st[top-1],st[top]);
19       dec(top);
20     end;
21     if ff<>st[top] then add2(ff,st[top]);
22     if st[top-1]<>ff then st[top]:=ff else dec(top);
23     inc(top);st[top]:=x;
24   end;
25   while top>1 do
26   begin
27     add2(st[top-1],st[top]);
28     dec(top);
29   end;
30   for i:=1 to k do bo[h[i]]:=false;
31 end;

 

posted @ 2017-02-13 10:58  OldJang  阅读(210)  评论(0编辑  收藏  举报