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)

  1. 9
  2. 1 2
  3. 2 3
  4. 3 4
  5. 4 2
  6. 4 5
  7. 2 5
  8. 5 6
  9. 5 7
  10. 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. 1
  2. 2
  3. 3
  4. 4
  5. 2
  6. 5
  7. 4
  8. 6
  9. 5
  10. 7

解决方式

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

执行结果

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

通过代码

  1. /*
  2. ID: c1033311
  3. LANG: C++
  4. TASK: fence
  5. */
  6.  
  7. #include<stdio.h>
  8. #include<string.h>
  9.  
  10. #define MAX 501
  11. #define MAXP 1030
  12.  
  13. int point[MAXP],deg[MAX],G[MAX][MAX];
  14. int n=0;
  15.  
  16. void euler(int u)
  17. {
  18. int v;
  19. for(v=1;v<MAX;++v)
  20. {
  21. if(G[u][v])
  22. {
  23. G[u][v]--;
  24. G[v][u]--;
  25. euler(v);
  26. point[n++]=v;
  27. }
  28. }
  29. }
  30.  
  31. int main(){
  32. FILE *fin=fopen("fence.in","r");
  33. FILE *fout=fopen("fence.out","w");
  34.  
  35. int F,i,a,b;
  36. int start;
  37.  
  38. fscanf(fin,"%d",&F); //输入
  39.  
  40. for(i=0;i<F;++i)
  41. {
  42. fscanf(fin,"%d%d",&a,&b);
  43. G[a][b]++; //两个点之间可能有多条边
  44. G[b][a]++;
  45. deg[a]++;
  46. deg[b]++;
  47. }
  48.  
  49. start=1;
  50. for(i=1;i<MAX;++i) //寻找起点
  51. if(deg[i]%2)
  52. {
  53. start=i;
  54. break;
  55. }
  56.  
  57. euler(start); //寻找欧拉路径
  58. point[F]=start;
  59.  
  60. for(i=F;i>=0;--i) //输出
  61. fprintf(fout,"%d\n",point[i]);
  62.  
  63. return 0;
  64. }
  1.  

深搜解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. Android通过泛型简化findViewById类型转换

    曾经老用findViewById,每次使用还得add cast一下今天看到一个视频(依据视频中使用的IDE判断,应该是几年前的视频了..),使用了一个方法,能够不用每次使用findViewById都去 ...

  2. 四、基于HTTPS协议的12306抢票软件设计与实现--水平DNS并发查询分享

    一.基于HTTPS协议的12306抢票软件设计与实现--实现效果 二.基于HTTPS协议的12306抢票软件设计与实现--相关接口以及数据格式 三.基于HTTPS协议的12306抢票软件设计与实现-- ...

  3. DirectX11 学习笔记4 - 一个完整的封装框架

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3EzNjExMDYzMDY=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  4. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成逻辑计划

    淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成逻辑计划 SQL编译解析三部曲分为:构建语法树.生成逻辑计划.指定物理运行计划. 第一步骤,在我的上一篇博客淘宝数据库OceanBas ...

  5. Registry Connect failed,Windows服务诊断

    Message:Connection failed for 192.168.32.38_e-futrueserer. Details:Windows Registry Datasource: Regi ...

  6. iris中间件

    最近使用golang写的时候涉及到权限校验,用中间件(使用iris框架内的东西) 自己摸索出一种自己的方式 iris.UseFunc(MiddlewareFunc)使用这个方法,会在所有的请求之前执行 ...

  7. MyEclipse 安装svn 插件步骤详情

    方法一:在线安装 打开HELP- > MyEclipse Configuration Center.切换到SoftWare标签页. 点击Add Site 打开对话框,在对话框Name输入Svn, ...

  8. POJ 3764 DFS+trie树

    题意: 给你一棵树,求树中最长的xor路径.(n<=100000) 思路: 首先我们知道 A xor B =(A xor C) xor (B xor C) 我们可以随便选一个点DFS 顺便做出与 ...

  9. SQL Server 从字符串中提取中文、英文、数字

    --[提取中文] IF OBJECT_ID('dbo.fun_getCN') IS NOT NULL DROP FUNCTION dbo.fun_getCN GO create function db ...

  10. 利用hexo+github创建个人博客

    因为想拥有一个独属于自己的个人博客啊. 安装部署hexo 进入一个安全的目录,cd ~/Desktop 在 GitHub 上新建一个空 repo,repo 名称是「你的GitHub用户名.github ...