[USACO 3.3.1 Riding The Fences]

【题目】:Riding The Fences

【来源】:USACO 3.3.1

【关键字】:图论 搜索 欧拉

//=====================================================================================================

【分析】:就是找到一条欧拉路.有个问题一直不解:对于任意一个无向图,保证一定存在欧拉路.从随意一个可以做起点的点开始,任意走.是否可以走出一条欧拉路?  因该是这样,因为每条路都要走,只是顺序问题而已。

【代码】:

View Code
 1 {
2 ID:x yr
3 PROB:fence
4 LANG:PASCAL
5 }
6 var
7 n, m, st, now: longint;
8 r, p: array[0..600] of longint;
9 a: array[0..600,0..600] of longint;
10 procedure init;
11 var
12 i, x, y, min: longint;
13 begin
14 read(m);
15 n := 0;
16 fillchar(r,sizeof(r),0);
17 fillchar(a,sizeof(a),0);
18 min := maxlongint;
19 for i := 1 to m do
20 begin
21 read(x,y);
22 if x > n then n := x;
23 if y > n then n := y;
24 inc(a[x,y]);
25 inc(a[y,x]);
26 if min > x then min := x;
27 if min > y then min := y;
28 inc(r[x]);
29 inc(r[y]);
30 end;
31 for i := 1 to n do if r[i] <> 0 then if r[i] mod 2 = 1 then begin st := i; break; end;
32 if st = 0 then st := min;
33 end;
34 procedure print;
35 var
36 i: longint;
37 begin
38 for i := now downto 1 do writeln(p[i]);
39 // if p[now+1] <> p[1] then writeln(p[now]);
40 // read(n);
41 close(input);
42 close(output);
43 halt;
44 end;
45 procedure dfs(k: longint);
46 var
47 i: longint;
48 begin
49 for i := 1 to n do
50 if a[k,i] > 0 then
51 begin
52 dec(a[k,i]);
53 dec(a[i,k]);
54 dfs(i);
55 end;
56 inc(now);
57 p[now] := k;
58 end;
59 begin
60 assign(input,'fence.in');reset(input);
61 assign(output,'fence.out');rewrite(output);
62 init;
63 now := 0;
64 dfs(st);
65 print;
66 end.



你可能感兴趣的:(USACO)