USACO Section 3.3: Riding the Fences
典型的找欧拉路径的题。先贴下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的更多相关文章
- 【USACO 3.3】Riding The Fences(欧拉路径)
题意: 给你每个fence连接的两个点的编号,输出编号序列的字典序最小的路径,满足每个fence必须走且最多走一次. 题解: 本题就是输出欧拉路径. 题目保证给出的图是一定存在欧拉路径,因此找到最小的 ...
- USACO 3.3.1 Riding the Fences 骑马修栅栏(欧拉回路)
Description 农民John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个一个栅栏.你必须编一个程 ...
- [USACO]骑马修栅栏 Riding the Fences
题目链接 题目简述:欧拉回路,字典序最小.没什么好说的. 解题思路:插入边的时候,使用multiset来保证遍历出出答案的字典序最小. 算法模板:for(枚举边) 删边(无向图删两次) 遍历到那个点 ...
- USACO 3.3 Riding the Fences
Riding the Fences Farmer John owns a large number of fences that must be repaired annually. He trave ...
- 洛谷P2731 骑马修栅栏 Riding the Fences
P2731 骑马修栅栏 Riding the Fences• o 119通过o 468提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论 • 数据有问题题 ...
- 洛谷 P2731 骑马修栅栏 Riding the Fences 解题报告
P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...
- 洛谷 P2731 骑马修栅栏 Riding the Fences
P2731 骑马修栅栏 Riding the Fences 题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样 ...
- 深搜解Riding the Fences
Riding the Fences Farmer John owns a large number of fences that must be repairedannually. He traver ...
- P2731 骑马修栅栏 Riding the Fences 题解(欧拉回路)
题目链接 P2731 骑马修栅栏 Riding the Fences 解题思路 存图+简单\(DFS\). 坑点在于两种不同的输出方式. #include<stdio.h> #define ...
随机推荐
- python 交换变量值
在其他语言中,交换两个变量值的时候,可以这样写: temp = a a = b b = temp 在Python中,我们可以简单的这样写: a,b=b,a 实验如下: >>> a=1 ...
- HDU 5592 ZYB's Premutation
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 题意: http://bestcoder.hdu.edu.cn/contests/contes ...
- hdu 1285 确定比赛名次 拓扑排序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...
- SQL 基本(Head First)
CREATE TABLE my_contacts( last_name VARCHAR(30), first_name VARCHAR(30), email VARCHAR(50), gender C ...
- SOA之(3)——面向服务计算基础
面向服务计算基础(Service-Oriented Computing Fundamentals) 面向服务的计算(Service-Oriented Computing) 面向服务的计算是一个伞状术语 ...
- Ckeditor 的加载顺序
我们的只用在文件里面引用一个CKEditor的js文件--CKEditor目录下的ckeditor.js文件, 该文件会完成后续的所有的CKEidtor依赖的js文件的加载. 所依赖的js文件加载顺序 ...
- Unity3D 错误,nativeVideoFrameCallback解决方法。
原地址:http://blog.csdn.net/alking_sun/article/details/23684733 Unity3D在打包安卓应用的时候,一打开游戏就闪退,接入LogCat之后发现 ...
- 全自动化的 Android 编译管线
[编者按]Nicolas Frankel 是 hybris 的高级顾问, 在Java / J2EE 领域拥有超过10年的管理经验,本文阐述了他在使用自动化工序去构建 Android 应用程序遇到的一些 ...
- codeforces 430A Points and Segments (easy)(理解能力有待提高……)
题目 //终于看懂题目了,,,, //一条线段里面不是每个坐标上都有要染色的点,所以为了满足条件,只能考虑那些给出坐标的点 //所以就要排序一下了,不能直接根据坐标0 1 0 1…… #include ...
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...