USACO-Section 3.3-PROB Riding The Fences
2016-01-27 14:23
363 查看
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.
many possible answers to any given input set, but only one is ordered correctly.
欧拉回路
深搜,对于当前的点,把所有点从小到大搜索,找到和它相连的,找到一个之后删除它们之间的连线,并去搜索新的那个点,如果没有找到点和它相连,那么就把这个点加入输出队列。
输出时反着输出即可。
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 intersection on the last line. There might bemany 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
欧拉回路
深搜,对于当前的点,把所有点从小到大搜索,找到和它相连的,找到一个之后删除它们之间的连线,并去搜索新的那个点,如果没有找到点和它相连,那么就把这个点加入输出队列。
输出时反着输出即可。
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #define name "fence" using namespace std; int edge[505][505],du[255]; bool have[505]; int m,s,maxn,minn=1e9; int ans[255],anst; void dfs(int u) { for (int v=minn;v<=maxn;v++) { if (edge[u][v]!=0) { edge[u][v]--; edge[v][u]--; dfs(v); } } ans[++anst]=u; } int main() { freopen(name ".in","r",stdin); freopen(name ".out","w",stdout); int i,j; cin>>m; int u,v; for (i=1;i<=m;i++) { scanf("%d%d",&u,&v); du[u]++;du[v]++; have[u]=1;have[v]=1; maxn=max(maxn,u);maxn=max(maxn,v); minn=min(minn,u);minn=min(minn,v); edge[u][v]++;edge[v][u]++; } for (i=1;i<=maxn;i++) if (have[i]&&du[i]%2==1) {s=i;break;} if (s==0) s=minn; dfs(s); for (i=anst;i>=1;i--) cout<<ans[i]<<endl; return 0; }
相关文章推荐
- 初识HTML 5:关于它的三个三
- JS 截取字符串substr 和 substring方法的区别
- JSON与字符串互相转换的几种方法
- jquery indexOf使用方法
- JSP中的注释操作及JSP中的三种Scriptlet
- IE=edge,chrome=1的含义
- Bootstrap树形控件使用方法详解
- AngularJS select详细用法
- ConvNetJS源代码解析第二篇
- js页面跳转整理
- js入门——Dom基础
- 前端工程师使用node.js打造日记社区教程之一
- 服务端嵌套json
- 自定义复选框 checkbox 样式
- React Native-4.React Native布局属性练习之flexBox模型实战
- jquery 学习随笔 ui tabs选项卡
- css盒模型和块级、行内元素深入理解
- 杭电1015——Safecracker(dfs、递归枚举排列)
- erlang 用rfc4627 带中文的json格式
- react学习笔记2--练习Demos