典型的找欧拉路径的题。先贴下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. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  2. WPF入门学习

    WPF基础知识 总结的学习WPF的几点基础知识: 1) C#基础语法知识(或者其他.NET支持的语言):这个是当然的了,虽然WPF是XAML配置的,但是总还是要写代码的,相信各位读者应该也都有这个基础 ...

  3. 单点登录(iwantmoon.com出品)

    早年便听到单点登录,一直因为感觉很简单,就没有动手去弄,正好现在公司有要求,那么OK,直接做了一个单机版的单点登录. 原理,可以参考SSO. Now,我们来看看我的实现吧.看下图 我们平时所做的登录, ...

  4. 临时文档9--dfs

    #include<iostream> #include<stdio.h> #include<math.h> #include<string.h> usi ...

  5. Spring与apache CXF结合实例

    本文的前提是已经有一个Spring的项目,在此基础上如何跟apache CXF进行结合,开发出WebService服务和调用WebService服务. 1.开发WebService   1.引入jar ...

  6. 【BZOJ】【1059】【ZJOI2007】矩阵游戏

    二分图完美匹配/匈牙利算法 如果a[i][j]为黑点,我们就连边 i->j ,然后跑二分图最大匹配,看是否有完美匹配. <_<我们先考虑行变换:对于第 i 行,如果它第 j 位是黑点 ...

  7. [工作积累] NDK通过Java获取package name 和version

    ////////////////////////////////////////////////////////////////////////// //Java code snippet //get ...

  8. google大赛 入围赛250分真题

    Problem StatementYou have a collection of music files with names formatted as "genre-artist-alb ...

  9. .NET设计模式(15):结构型模式专题总结(转)

    摘要:结构型模式,顾名思义讨论的是类和对象的结构,它采用继承机制来组合接口或实现(类结构型模式),或者通过组合一些对象,从而实现新的功能(对象结构型模式).这些结构型模式,它们在某些方面具有很大的相似 ...

  10. 比较IE6的不同之处,与IE8 IE11 比较

    文档申明为  <!DOCTYPE html> IE6或者IE特有的一些东西 1.盒子模型 IE6:(使用 !DOCTYPE 声明指定 standards-compliant 模式) mar ...