用广度优先搜索建立图的邻接矩阵。
state:array[0..100] of longint;
v:array[0..100] of boolean;
g:array[0..100,0..100] of longint;
n,e:longint;
var
head,tail,i,ans:longint;
begin
head:=0;
tail:=1;
state[1]:=1;
v[1]:=false;
ans:=1;
repeat
inc(head);
for i:=1 to n do
if (g[state[head],i]=1)and(v[i]) then
begin
g[state[head],i]:=0;
g[i,state[head]]:=0;
inc(tail);
state[tail]:=i;
v[i]:=false;
end;
until head>=tail;
for i:=1 to head do
write(state[i],' ');
var
x,y,i:longint;
begin
readln(n,e);
for i:=1 to e do
begin
readln(x,y);
g[x,y]:=1;
g[y,x]:=1;
end;
fillchar(v,sizeof(v),true);
init;
bfs;
程序:
var
procedure bfs;
end;
procedure init;
end;
begin
end.