算法:搜索
开始想错了,用的拓扑排序+搜索,其实直接用搜索然后枚举删点就可以。
枚举删点,然后再从1开始走到n,验证一下能否到达n,若不能到达,则这个点就一定是必须经过的点。
program busstop;
const
maxn=2000;
maxm=7000;
type
atp=record
y,next:longint;
end;
var
n,m,tot,tot1:longint;
map:array [0..maxm*2] of atp;
bb:array [0..maxn] of boolean;
first,ans:array [0..maxn] of longint;
procedure init;
var
i,x,y:longint;
begin
readln(n,m);
for i:=1 to m do
begin
readln(x,y);
inc(tot1);
map[tot1].y:=y;
map[tot1].next:=first[x];
first[x]:=tot1;
inc(tot1);
map[tot1].y:=x;
map[tot1].next:=first[y];
first[y]:=tot1;
end;
end;
procedure dfs(st,ca:longint);
var
t:longint;
begin
t:=first[st];
while t>0 do
begin
if (not bb[map[t].y]) and (map[t].y<>ca) then
begin
bb[map[t].y]:=true;
dfs(map[t].y,ca);
end;
t:=map[t].next;
end;
end;
procedure main;
var
i:longint;
begin
for i:=2 to n-1 do
begin
fillchar(bb,sizeof(bb),false);
dfs(1,i);
if not bb[n] then
begin
inc(tot);
ans[tot]:=i;
end;
end;
writeln(tot);
for i:=1 to tot do write(ans[i],' ');
end;
begin
assign(input,'busstop.in'); reset(input);
assign(output,'busstop.out'); rewrite(output);
init;
main;
close(input); close(output);
end.
本文讨论了如何在图论中应用搜索算法解决拓扑排序问题,并通过实例展示了枚举删除节点的方法来确定必经节点的过程。通过具体代码实现,文章详细介绍了算法步骤和流程,有助于理解在实际场景中如何高效地处理此类问题。
3383

被折叠的 条评论
为什么被折叠?



