link/cut tree
[kudsource.git] / POJ / 2568.pas
blob29f4605e384fb6a369ac781625929a8ed726be92
1 var
2 data,father,son:array [0..100] of longint;
3 flag:array [0..100] of boolean;
4 n,i,p:longint;
6 procedure dfs(root:longint);
7 var
8 i:longint;
10 begin
11 if root=0 then exit;
12 write('(',root);
13 for i:=1 to n do
14 if father[i]=root then
15 begin
16 write(' ');
17 dfs(i);
18 end;
19 write(')');
20 end;
22 begin
23 while not eof do
24 begin
25 n:=1;
26 while not eoln do
27 begin
28 read(data[n]);
29 inc(son[data[n]]);
30 inc(n);
31 end;
32 readln;
33 fillchar(father,sizeof(father),0);
34 fillchar(flag,sizeof(flag),false);
35 p:=0;
36 for i:=1 to n-1 do
37 begin
38 for p:=1 to n do
39 if (father[p]=0) and (son[p]=0) then break;
40 dec(son[data[i]]);
41 father[p]:=data[i];
42 end;
43 dfs(n);
44 writeln;
45 end;
46 end.