C++&Pascal——【USACO 3.3.1】——Riding the Fences
2017-10-09 17:54
459 查看
Riding the Fences
Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts.
Farmer John is as lazy as the next farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly
once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.
Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection.
It is always possible to ride from any fence to any other fence (i.e., all fences are "connected").
Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.
There will always be at least one solution for each set of input data supplied to your program for testing.
on the last line. There might be many possible answers to any given input set, but only one is ordered correctly.
农民John每年有很多栅栏要修理。他总是骑着马穿过每一个栅栏并修复它破损的地方。
John是一个与其他农民一样懒的人。他讨厌骑马,因此从来不两次经过一个一个栅栏。你必须编一个程序,读入栅栏网络的描述,并计算出一条修栅栏的路径,使每个栅栏都恰好被经过一次。John能从任何一个顶点(即两个栅栏的交点)开始骑马,在任意一个顶点结束。
每一个栅栏连接两个顶点,顶点用 1 到 500 标号(虽然有的农场并没有 500 个顶点)。一个顶点上可连接任意多(>=1)个栅栏。所有栅栏都是连通的(也就是你可以从任意一个栅栏到达另外的所有栅栏)。
你的程序必须输出骑马的路径(用路上依次经过的顶点号码表示)。我们如果把输出的路径看成是一个500进制的数,那么当存在多组解的情况下,输出500进制表示法中最小的一个 (也就是输出第一个数较小的,如果还有多组解,输出第二个数较小的,等等)。 输入数据保证至少有一个解。
第 1 行: 一个整数 F(1 <= F <= 1024),表示栅栏的数目 。
第 2 到 F+1 行: 每行两个整数 i, j (1<=i,j<=500)表示这条栅栏连接 i 与 j 号顶点。
输出应当有 F+1 行,每行一个整数,依次表示路径经过的顶点号。
注意数据可能有多组解,但是只有上面题目要求的那一组解是认为正确的。
输入
9
1 2
2 3
3 4
4 2
4 5
2 5
5 6
5 7
4 6
输出
1
2
3
4
2
5
4
6
5
7
寻找“欧拉路”或“欧拉回路”问题。
欧拉回路:所有点连通,无奇点。
欧拉路(一笔画):所有点连通,有且只有2个奇点。
Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts.
Farmer John is as lazy as the next farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly
once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.
Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection.
It is always possible to ride from any fence to any other fence (i.e., all fences are "connected").
Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.
There will always be at least one solution for each set of input data supplied to your program for testing.
PROGRAM NAME: fence
INPUT FORMAT
Line 1: | The number of fences, F (1 <= F <= 1024) |
Line 2..F+1: | A pair of integers (1 <= i,j <= 500) that tell which pair of intersections this fence connects. |
SAMPLE INPUT (file fence.in)
9 1 2 2 3 3 4 4 2 4 5 2 5 5 6 5 7 4 6
OUTPUT FORMAT
The output consists of F+1 lines, each containing a single integer. Print the number of the starting intersection on the first line, the next intersection's number on the next line, and so on, until the final intersectionon the last line. There might be many possible answers to any given input set, but only one is ordered correctly.
SAMPLE OUTPUT (file fence.out)
1 2 3 4 2 5 4 6 5 7
农民John每年有很多栅栏要修理。他总是骑着马穿过每一个栅栏并修复它破损的地方。
John是一个与其他农民一样懒的人。他讨厌骑马,因此从来不两次经过一个一个栅栏。你必须编一个程序,读入栅栏网络的描述,并计算出一条修栅栏的路径,使每个栅栏都恰好被经过一次。John能从任何一个顶点(即两个栅栏的交点)开始骑马,在任意一个顶点结束。
每一个栅栏连接两个顶点,顶点用 1 到 500 标号(虽然有的农场并没有 500 个顶点)。一个顶点上可连接任意多(>=1)个栅栏。所有栅栏都是连通的(也就是你可以从任意一个栅栏到达另外的所有栅栏)。
你的程序必须输出骑马的路径(用路上依次经过的顶点号码表示)。我们如果把输出的路径看成是一个500进制的数,那么当存在多组解的情况下,输出500进制表示法中最小的一个 (也就是输出第一个数较小的,如果还有多组解,输出第二个数较小的,等等)。 输入数据保证至少有一个解。
输入格式
第 1 行: 一个整数 F(1 <= F <= 1024),表示栅栏的数目 。第 2 到 F+1 行: 每行两个整数 i, j (1<=i,j<=500)表示这条栅栏连接 i 与 j 号顶点。
输出格式
输出应当有 F+1 行,每行一个整数,依次表示路径经过的顶点号。注意数据可能有多组解,但是只有上面题目要求的那一组解是认为正确的。
样例
输入9
1 2
2 3
3 4
4 2
4 5
2 5
5 6
5 7
4 6
输出
1
2
3
4
2
5
4
6
5
7
寻找“欧拉路”或“欧拉回路”问题。
欧拉回路:所有点连通,无奇点。
欧拉路(一笔画):所有点连通,有且只有2个奇点。
/* ID: mcdonne1 PROG: fence LANG: C++ */ #include<cstdio> #define min(a,b,c) (a)<(b)?(a)<(c)?(a):(c):(b)<(c)?(b):(c) #define max(a,b,c) (a)>(b)?(a)>(c)?(a):(c):(b)>(c)?(b):(c) short f,x,y,b=500,e,cnt; short fx[501],r[1030],t[1030][1030]; inline short read() { short i=0; char c=getchar(); while(c<'0'||c>'9') c=getchar(); while(c>='0'&&c<='9') i=(i<<3)+(i<<1)+c-48,c=getchar(); return i; } void find(short x) { for(short i=b;i<=e;++i) if(t[x][i]) { --t[x][i]; --t[i][x]; find(i); } r[++cnt]=x; } int main() { freopen("fence.in","r",stdin); freopen("fence.out","w",stdout); f=read(); for(short i=1;i<=f;++i) { x=read(); y=read(); ++t[x][y]; ++t[y][x]; ++fx[x]; ++fx[y]; b=min(b,x,y); e=max(e,x,y); } for(short i=b;i<=e;++i) if(fx[i]&1) { find(i); break; } else if(i==e) find(b); for(short i=cnt;i>=1;--i) printf("%d\n",r[i]); return 0; }
(* ID : mcdonne1 PROG : fence LANG : PASCAL *) var f, x, y, b, e, i, cnt : integer; fx : array [0..501] of integer; r : array [0..1030] of integer; t : array [0..1030, 0.. 1030] of integer; function max(a, b, c : integer) : integer; begin if a > b then if a > c then exit (a) else if b > c then exit (b) else exit (c) else if b > c then exit (b) else if a > c then exit (a) else exit (c); end; function min(a, b, c : integer) : integer; begin if a < b then if a < c then exit (a) else if b < c then exit (b) else exit (c) else if b < c then exit (b) else if a < c then exit (a) else exit (c); end; procedure find(x : integer); var i : integer; begin for i := b to e do if t[x][i] > 0 then begin dec (t[x][i]); dec (t[i][x]); find (i); end; inc (cnt); r[cnt] := x; end; begin assign (input, 'fence.in'); assign (output, 'fence.out'); reset (input); rewrite (output); read (f); b := 500; for i := 1 to f do begin read (x, y); inc (t[x][y]); inc (t[y][x]); inc (fx[x]); inc (fx[y]); b := min (b, x, y); e := max (e, x, y); end; for i := b to e do if fx[i] mod 2 = 1 then begin find (i); break; end else if i = e then find (b); for i := cnt downto 1 do writeln (r[i]); close (input); close (output); end.
相关文章推荐
- C++&Pascal——【USACO 5.1.1】——Fencing the Cows
- C++&Pascal&Python——【USACO 4.2.2】——The Perfect Stall
- [USACO3.3.1]Riding the Fences
- USACO3.3.1--Riding the Fences
- 欧拉回路 USACO 3.3.1 Riding the Fences
- C++&Pascal&Python——【USACO 3.4.2】——Electric Fence
- USACO Riding the Fences 解题报告
- usaco Riding the Fences
- 欧拉路径, 欧拉回路 USACO Riding the Fences
- USACO Riding The Fences (点最小字典序欧拉路径 Fleury)
- C++&Pascal&Python——【USACO 5.3.4】——Big Barn
- USACO Riding the fences and C implement
- C++&Pascal——【USACO 3.3.2】——Shopping Offers
- usaco Riding the Fences
- USACO-Section 3.3-PROB Riding The Fences
- USACO: Chap3 Riding the Fences
- usaco 3.3 Riding the Fences
- USACO - Riding the Fences (Fleury算法找欧拉路径/欧拉回路模板)
- USACO 3.3 Riding The Fences
- USACO / Riding the Fences (欧拉路径)