典型的找欧拉路径的题。先贴下USACO上找欧拉路径的法子:

  • Pick a starting node and recurse on that node. At each step:
    • If the node has no neighbors, then append the node to the circuit and return
    • If the node has a neighbor, then make a list of the neighbors and process them (which includes deleting them from the list of nodes on which to work) until the node has no more neighbors
    • To process a node, delete the edge between the current node and its neighbor, recurse on the neighbor, and postpend the current node to the circuit。

但实际写的时候其实用DFS起来更加方便。这道题我一开始用stack,但是始终不能找到输出的序列在500位制情况下最小的解,于是从网上看到有人用dfs轻松简单方便地就解决了。。。

 /*
 ID: yingzho1
 LANG: C++
 TASK: fence
 */
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <map>
 #include <vector>
 #include <set>
 #include <algorithm>
 #include <stdio.h>
 #include <queue>
 #include <cstring>
 #include <cmath>
 #include <list>
 #include <cstdio>
 #include <cstdlib>
 #include <limits>
 #include <stack>

 using namespace std;

 ifstream fin("fence.in");
 ofstream fout("fence.out");

 ;

 };
 ];
 int F, st, ans_tot;

 void dfs(int st) {
     ; i < MAX; i++) {
         if (edge[st][i]) {
             edge[st][i]--, edge[i][st]--;
             du[st]--, du[i]--;
             dfs(i);
         }
     }
     ans[++ans_tot] = st;
 }

 int main()
 {
     fin >> F;
     int x, y;
     ; i < F; i++) {
         fin >> x >> y;
         edge[x][y]++, edge[y][x]++;
         du[x]++, du[y]++;
     }
     st = ;
     ; i < MAX; i++) {
         ) {
             st = i;
             break;
         }
     }
     ) {
         ; i < MAX; i++) {
             if (du[i]) {
                 st = i;
                 break;
             }
         }
     }
     dfs(st);
     ; i--) fout << ans[i] << endl;

     ;
 }

USACO Section 3.3: Riding the Fences的更多相关文章

  1. 【USACO 3.3】Riding The Fences(欧拉路径)

    题意: 给你每个fence连接的两个点的编号,输出编号序列的字典序最小的路径,满足每个fence必须走且最多走一次. 题解: 本题就是输出欧拉路径. 题目保证给出的图是一定存在欧拉路径,因此找到最小的 ...

  2. USACO 3.3.1 Riding the Fences 骑马修栅栏(欧拉回路)

    Description 农民John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个一个栅栏.你必须编一个程 ...

  3. [USACO]骑马修栅栏 Riding the Fences

    题目链接 题目简述:欧拉回路,字典序最小.没什么好说的. 解题思路:插入边的时候,使用multiset来保证遍历出出答案的字典序最小. 算法模板:for(枚举边) 删边(无向图删两次) 遍历到那个点 ...

  4. USACO 3.3 Riding the Fences

    Riding the Fences Farmer John owns a large number of fences that must be repaired annually. He trave ...

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

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

  6. 洛谷 P2731 骑马修栅栏 Riding the Fences 解题报告

    P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...

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

    P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...

  8. 深搜解Riding the Fences

    Riding the Fences Farmer John owns a large number of fences that must be repairedannually. He traver ...

  9. P2731 骑马修栅栏 Riding the Fences 题解(欧拉回路)

    题目链接 P2731 骑马修栅栏 Riding the Fences 解题思路 存图+简单\(DFS\). 坑点在于两种不同的输出方式. #include<stdio.h> #define ...

随机推荐

  1. HSSF,XSSF和SXSSF的区别

    HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 从POI 3.8版本开始, ...

  2. android开发支付宝接口开发流程(密钥篇)

    参考博客:http://blog.it985.com/12276.html 官方下载地址:http://download.alipay.com/public/api/base/WS_MOBILE_PA ...

  3. mongo的insert和save比较

    insert仅仅是插入文档到集合中,如果记录不存在则插入,如果记录存在则忽略 save是在文档不存在时插入,存在时则是更新 下面代码不是为了演示insert和save的: foreach ($mens ...

  4. C实现面向对象封装、继承、多态

    参考资料:      http://blog.chinaunix.net/uid-26750235-id-3102371.html      http://www.eventhelix.com/rea ...

  5. 如何用javascript正则表达式验证身份证号码是否合法

    在用户注册页面有些需求要求的比较严格,需要对身份证js验证是否合法,通过此功能严格此系统软件,从而过滤到很多水客.下面就此实现方法给大家讲解下. 很多时候我们都是通过一组正则表达式来判断用户输入的身份 ...

  6. 2014 ACM/ICPC Asia Regional Guangzhou Online

    Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...

  7. 17+个ASP.NET MVC扩展点,含源码{转}

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.在自定义的HttpMo ...

  8. POJ 3484

    Showstopper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1060   Accepted: 303 Descri ...

  9. 【剑指offer】连续子数组最大和

    思路dp很清楚,就是要注意细节. int FindGreatestSumOfSubArray(vector<int> array) { ; ], tempsum = array[]; // ...

  10. line-height 与垂直居中!

    在此之前,对于line-height 与垂直居中的问题,经常碰到. 比如,图片与span在同一个box中的时候,竟然会各种偏移.要想达到理想的效果真的是各种难. 有时间,决定认真的啃一啃. 一 lin ...