Untitled
unknown
pascal
4 years ago
2.0 kB
18
Indexable
type mang=array[1..1000005] of longint;
var f1,f2:text;
n,k,i,j,x,c,ans:longint;
a:array[1..1000005] of longint; //Danh sach ke
cs:array[0..100000] of longint;
check:array[0..100000] of boolean;
kq:array[1..1000005] of longint;
procedure sort(var a:mang; left,right:longint) ;
var i,j,k,t:longint;
begin
i:=left;
j:=right;
k:=a[(i+j) div 2];
while (i<j) do
begin
while (a[i]<k) do inc(i);
while a[j]>k do dec(j);
if (i<=j) then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
inc(i);
dec(j);
end;
end;
if (left<j) then sort(a,left,j);
if (i<right) then sort(a,i,right);
end;
procedure Enter;
begin
assign(f1,'t.inp');
reset(f1);
readln(f1,n,k);
for i:=1 to n do
begin
read(f1,x);
cs[i]:=cs[i-1]+x;
for j:=1 to x do
begin
inc(c);
read(f1,a[c]);
end;
end;
close(f1);
end;
procedure Solve;
begin
check[k]:=true;
ans:=1;
kq[1]:=k;
i:=1;
while (i<=ans) do
begin
for j:=cs[kq[i]-1]+1 to cs[kq[i]] do
begin
if check[a[j]]=false then
begin
inc(ans);
kq[ans]:=a[j];
check[a[j]]:=true;
end;
end;
inc(i);
end;
end;
procedure Result;
begin
assign(f2,'t.out');
rewrite(f2);
writeln(f2,ans);
sort(kq,1,ans);
for i:=1 to ans do write(f2,kq[i],' ');
close(f2);
end;
begin
Enter;
Solve;
Result;
end.
Editor is loading...