Riding the Fences

Farmer John owns a large number of fences that must be repairedannually. He traverses the fences by riding a horse along each andevery 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 samefence twice. Your program must read in a description of a network offences and tell Farmer John a path to traverse each fence length exactlyonce, if possible. Farmer J can, if he wishes,
start and finish at anyfence intersection.

Every fence connects two fence intersections, which are numberedinclusively from 1 through 500 (though some farms have far fewer than500 intersections). Any number of fences (>=1) can meet at a fenceintersection. It is always possible to ride from any fence
to any otherfence (i.e., all fences are "connected").

Your program must output the path of intersections that, if interpretedas a base 500 number, would have the smallest magnitude.

There will always be at least one solution for each set of inputdata 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, thenext intersection's number on the next line, and so on, until the finalintersection on the last line. There might be many
possible answers toany given input set, but only one is ordered correctly.

SAMPLE OUTPUT (file fence.out)

1
2
3
4
2
5
4
6
5
7

解决方式

找出起始点,然后用从起始点開始用深搜寻找欧拉路径。唯一须要注意的是两个点之间可能有多条边连接。

执行结果

Executing...
Test 1: TEST OK [0.003 secs, 4480 KB]
Test 2: TEST OK [0.003 secs, 4480 KB]
Test 3: TEST OK [0.005 secs, 4480 KB]
Test 4: TEST OK [0.003 secs, 4480 KB]
Test 5: TEST OK [0.005 secs, 4480 KB]
Test 6: TEST OK [0.008 secs, 4480 KB]
Test 7: TEST OK [0.011 secs, 4480 KB]
Test 8: TEST OK [0.014 secs, 4480 KB] All tests OK.

通过代码

/*
ID: c1033311
LANG: C++
TASK: fence
*/ #include<stdio.h>
#include<string.h> #define MAX 501
#define MAXP 1030 int point[MAXP],deg[MAX],G[MAX][MAX];
int n=0; void euler(int u)
{
int v;
for(v=1;v<MAX;++v)
{
if(G[u][v])
{
G[u][v]--;
G[v][u]--;
euler(v);
point[n++]=v;
}
}
} int main(){
FILE *fin=fopen("fence.in","r");
FILE *fout=fopen("fence.out","w"); int F,i,a,b;
int start; fscanf(fin,"%d",&F); //输入 for(i=0;i<F;++i)
{
fscanf(fin,"%d%d",&a,&b);
G[a][b]++; //两个点之间可能有多条边
G[b][a]++;
deg[a]++;
deg[b]++;
} start=1;
for(i=1;i<MAX;++i) //寻找起点
if(deg[i]%2)
{
start=i;
break;
} euler(start); //寻找欧拉路径
point[F]=start; for(i=F;i>=0;--i) //输出
fprintf(fout,"%d\n",point[i]); return 0;
}
 

深搜解Riding the Fences的更多相关文章

  1. NYOJ 10 skiing (深搜和动归)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且 ...

  2. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  3. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  4. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

  5. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  6. 洛谷P2731 骑马修栅栏 Riding the Fences

    P2731 骑马修栅栏 Riding the Fences• o 119通过o 468提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论 • 数据有问题题 ...

  7. Hdu3812-Sea Sky(深搜+剪枝)

    Sea and Sky are the most favorite things of iSea, even when he was a small child.  Suzi once wrote: ...

  8. 什么时候用深搜(dfs)什么时候用广搜(bfs)(转)

    1.BFS是用来搜索最短径路的解是比较合适的,比如求最少步数的解,最少交换次数的解,因为BFS搜索过程中遇到的解一定是离根最近的,所以遇到一个解,一定就是最优解,此时搜索算法可以终止.这个时候不适宜使 ...

  9. POJ 1128 拓扑排序 + 深搜

    /* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对2 ...

随机推荐

  1. Spring MVC学习------------核心类与接口

    核心类与接口: 先来了解一下,几个重要的接口与类. 如今不知道他们是干什么的没关系,先混个脸熟,为以后认识他们打个基础. DispatcherServlet   -- 前置控制器 HandlerMap ...

  2. 关于Win 10的隐私保护政策

    近日.有人责备Win10收集用户信息,事实上这样的指责并不公平,比方:"Privacy Groups Claim Microsoft Uses Windows 10 as Big Broth ...

  3. Android中添加自己的模块 【转】

    本文转载自:http://wallage.blog.163.com/blog/static/17389624201021791333695/ 转:http://blog.csdn.net/yili_x ...

  4. Anaconda安装及PyCharm环境配置

    1. Anaconda下载 Anaconda 官方下载链接: https://www.continuum.io/downloads 根据自己的系统选择下载32位还是64位. 2. 进入下载目录 如果没 ...

  5. TLP电源管理

    笔记本电脑电池坏了, 换了块电池, 顺手装了一下这个电源管理软件.   https://linrunner.de/en/tlp/docs/tlp-linux-advanced-power-manage ...

  6. QT-项目文件说明

    前言:如题. 一.项目文件概述 文件 功能 helloworld.pro 包含了项目信息 helloworld.pro.user 用户信息 hellodialog.h 自定义类hellodialog的 ...

  7. tomcat指定JDK版本

    在windows环境下以批处理文件方式启动tomcat,只要运行<CATALINA_HOME>/bin/startup.bat这个文件,就可以启动Tomcat. 在启动时,startup. ...

  8. tomcat映射路径(应用程序基本目录)的配置方法

    tomcat映射路径的配置方法 一.默认配置 位置:/conf 文件夹里的server.xml文件 <Host appBase="webapps"> appBase:可 ...

  9. java+jxls利用excel模版进行导出

    大多时候会出现需要导出excel的功能,利用poi可以实现简单的导出,可以说poi的功能非常强大可以做到细节的定制化操作,但相对于在office操作excel,利用poi完全生成excel会显得非常复 ...

  10. MyEclipse背景与字体大小和xml文件中字体大小调整

    1.打开window / Preference,弹出Preference面板 2.展开General标签,选中Editors选项,展开. 3.选中 Text Editors,右边出现TestEdito ...