2 a
,b
,group
:array [0..500,0..500] of longint;
3 f
:array [0..500] of boolean;
4 g
:array [0..500,0..2] of longint;
5 n
,i
,j
,k
,j1
,t
,t1
,numofgroup
,p
,q
,ans
:longint;
7 function min(a
,b
:longint):longint;
9 if a
<b
then exit(a
) else exit(b
);
12 function max(a
,b
:longint):longint;
14 if a
>b
then exit(a
) else exit(b
);
25 b
[i
,j
]:=min(b
[i
,j
],b
[i
,k
]+b
[k
,j
]);
28 procedure floodfill(k
,num
,sum
:longint);
33 f
[k
]:=true; inc(group
[num
,0]); group
[num
,sum
]:=k
;
35 if (i
<>k
) and (f
[i
]=false) and (a
[k
,i
]=1) then
37 floodfill(i
,num
,sum
+1);
46 for i
:=1 to numofgroup
-1 do
47 for j
:=i
+1 to numofgroup
do
50 g
[0]:=g
[i
]; g
[i
]:=g
[j
]; g
[j
]:=g
[0];
51 group
[0]:=group
[i
]; group
[i
]:=group
[j
]; group
[j
]:=group
[0];
55 procedure insertsort(k
:longint);
63 while (g
[j
,1]<g
[0,1]) and (j
<numofgroup
) do inc(j
);
64 for i
:=1 to j
-2 do g
[i
]:=g
[i
+1];
71 while (p
<>0) and (q
<>0) do
80 if b
[i
,j
]=0 then b
[i
,j
]:=maxint
;
81 for i
:=1 to n
do b
[i
,i
]:=0;
87 floodfill(i
,numofgroup
,1);
89 for i
:=1 to numofgroup
do
91 t1
:=maxint
; j1
:=group
[i
,1];
92 for j
:=1 to group
[i
,0] do
95 for k
:=1 to group
[i
,0] do
96 t
:=max(b
[group
[i
,j
],group
[i
,k
]],t
);
97 if t
<t1
then begin t1
:=t
; j1
:=j
; end;
99 g
[i
,1]:=t1
; g
[i
,2]:=group
[i
,j1
];
102 for i
:=2 to numofgroup
do
104 writeln(g
[i
-1,2],' ',g
[i
,2],' ',n
+i
-1);
105 g
[i
,1]:=max(g
[i
,1],g
[i
-1,1])+1; g
[i
,2]:=n
+i
-1;
106 if i
<>numofgroup
then insertsort(i
);
108 writeln(g
[numofgroup
,1]);