POJ 2230 无向图欧拉环

本文介绍了一种解决无向图欧拉环问题的方法,通过深度优先搜索(DFS)实现遍历所有边恰好一次的路径。适用于竞赛编程等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意简述

打印能遍历无向图所有边恰一次的一种方案。


分析

无向图欧拉环模板题,DFS过程中直接倒序输出已访问顶点的编号,最后再加上起点即可。


代码实现

type
  edge=record
  re,next:longint
end;
var
n,m,cnt,i,x,y:longint;
f:array[0..10010] of longint;
e:array[0..100050] of edge;
vs:array[0..100050] of boolean;
procedure add(x,y:longint);
begin
  inc(cnt);
  with e[cnt] do
  begin
    re:=y;next:=f[x];
  end;
  f[x]:=cnt;
end;

procedure dfs(x:longint);
var tmp:longint;
begin
  tmp:=f[x];
  while tmp<>0 do
  begin
    if not vs[tmp] then
    begin
      vs[tmp]:=true;
      dfs(e[tmp].re);
      writeln(e[tmp].re);
    end;
    tmp:=e[tmp].next;
  end;
end;

begin
  readln(n,m);cnt:=1;
  for i:=1 to m do
  begin
    readln(x,y);
    add(x,y);add(y,x);
  end;
  dfs(1);
  writeln(1);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值