公交线(busstop)

本文讨论了如何在图论中应用搜索算法解决拓扑排序问题,并通过实例展示了枚举删除节点的方法来确定必经节点的过程。通过具体代码实现,文章详细介绍了算法步骤和流程,有助于理解在实际场景中如何高效地处理此类问题。
算法:搜索

开始想错了,用的拓扑排序+搜索,其实直接用搜索然后枚举删点就可以。
枚举删点,然后再从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.


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值