USACO 3.3 fence 欧拉回路
题意:求给定图的欧拉回路(每条边只走一次)
若欧拉回路存在,图中只可能有0个or2个奇数度的点。
求解时,若有奇数度的点,则必须从该点开始。否则可以从任一点开始
求解过程:dfs
//主程序部分
# circuit is a global array
find_euler_circuit
circuitpos =
find_circuit(node )
---------------------------------------------
# nextnode and visited is a local array
# the path will be found in reverse order
//递归函数
find_circuit(node i)
if node i has no neighbors then
circuit(circuitpos) = node i
circuitpos = circuitpos +
else
while (node i has neighbors)
pick a random neighbor node j of node i
delete_edges (node j, node i)
find_circuit (node j)
circuit(circuitpos) = node i
circuitpos = circuitpos +
-------------------------------------------
最终结果:将circuit()数组倒序输出即可
/*
PROB:fence
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define INF 999999 int dx=INF,dy=,n,f,x,y,p;
int e[][];
int t[],seq[]; void dfs(int x)
{
for (int i=dx;i<=dy;i++)
if (e[x][i]>)
{
e[x][i]--;
e[i][x]--;
dfs(i);
}
p++;
seq[p]=x;
} int start()
{
for (int i=dx;i<=dy;i++)
if (t[i]%!=)
return i;
return ;
} int main()
{
freopen("fence.in","r",stdin);
freopen("fence.out","w",stdout); memset(e,,sizeof(e));
memset(t,,sizeof(t));
cin>>f;
for (int i=;i<=f;i++)
{
cin>>x>>y;
e[x][y]++;
e[y][x]++;
t[x]++;
t[y]++;
if (x<dx) dx=x;
if (y<dx) dx=y;
if (x>dy) dy=x;
if (y>dy) dy=y; } x=start();
//cout<<dx<<" "<<dy<<"--"<<x<<endl;
p=;
dfs(x); //cout<<p<<endl;
for (int i=p;i>=;i--)
cout<<seq[i]<<endl; return ;
}
USACO 3.3 fence 欧拉回路的更多相关文章
- USACO 6.3 Fence Rails(一道纯剪枝应用)
Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...
- USACO 4.1 Fence Loops(Floyd求最小环)
Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...
- USACO 4.1 Fence Loops
Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...
- USACO 4.1 Fence Rails
Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...
- USACO 3.4 Electric Fence
Electric FenceDon Piele In this problem, `lattice points' in the plane are points with integer coord ...
- usaco training 4.1.2 Fence Rails 题解
Fence Rails题解 Burch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of h ...
- POJ 2230 Watchcow && USACO Watchcow 2005 January Silver (欧拉回路)
Description Bessie's been appointed the new watch-cow for the farm. Every night, it's her job to wal ...
- USACO 3.3.1 Riding the Fences 骑马修栅栏(欧拉回路)
Description 农民John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个一个栅栏.你必须编一个程 ...
- USACO 3.4 Electric Fence 皮克定理
题意:在方格纸上画出一个三角形,求三角形里面包含的格点的数目 因为其中一条边就是X轴,一开始想的是算出两条边对应的数学函数,然后枚举x坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点( ...
随机推荐
- java7-3 继承
1.继承概述: 把多个类中相同的内容给提取出来定义到一个类中. 如何实现继承呢? Java提供了关键字:extends 格式: class 子类名 extends 父类名 {} 父类也称为基类.超类: ...
- java 12-5 StringBuffer的几个案例
1. 把数组拼接成一个字符串 public class StringBufferTest2 { public static void main(String[] args) { //定义一个数组 in ...
- 对访问修饰关键字public, protected, internal and private的说明
对访问修饰关键字public, protected, internal and private的说明1.msdn: Internal types or members are accessible o ...
- f2fs解析(十)nid 如何从nat_root中删除
上面我们谈到了一个nid如何从free_nid中转移到node_info中去[分别有一个链表和一棵基数树搭伙做事],讲free_nid时,详细说明了free_nid中是如何进如何出的,上一篇说了nid ...
- Ruby Web实时消息后台服务器推送技术---GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样用Ruby实现最方便呢?我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEasy推 ...
- Android 下的EXIF
一.什么是Exif Exif(Exchangeable Image File 可交换图像文件)是一种图象文件格式,它的数据存储与JPEG格式是完全相同的.实际上Exif格式就是在JPEG格式头部插入了 ...
- ffplay 参数说明分享
ffplay 使用参数说明分享 E:\SRCFORTEST\software\ffmpeg-20131021\ffmpeg-20131021-git-712eff4-win32-static\ bin ...
- 团购、定时抢购倒计时js版
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org ...
- 求时间差的sql语句。 比如如下数据
msisdn createtime closetime138 2011-5-17 15:30:00:000 2011-5-17 15:30:00:530138 2011-5-17 15:40:00:0 ...
- 实现chrome扩展启动本地进程 - 补充
实现chrome扩展启动本地进程 - 补充 标签: chrome扩展启动本地程序访问本地磁盘 2014-10-17 11:42 6753人阅读 评论(17) 收藏 举报 分类: Chrome Plu ...