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坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点( ...
随机推荐
- C# using 三种使用方式 C#中托管与非托管 C#托管资源和非托管资源区别
1.using指令.using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到. ...
- jq 操作table
转载于:http://www.jb51.net/article/34633.htm jquery获取table中的某行全部td的内容方法,需要的朋友可以参考一下 <table>< ...
- iOS math.h数学函数
在实际工作中有些程序不可避免的需要使用数学函数进行计算,比如地图程序的地理坐标到地图坐标的变换.Objective-C做为ANSI C的扩展,使用C标准库头文件<math.h>中定义的数学 ...
- sql 索引 填充因子(转)
和索引重建最相关的是填充因子.当创建一个新索引,或重建一个存在的索引时,你可以指定一个填充因子,它是在索引创建时索引里的数据页被填充的数量.填充因子设置为100意味着每个索引页100%填满,50%意味 ...
- 实战:ADFS3.0单点登录系列-总览
本系列将以一个实际项目为背景,介绍如何使用ADFS3.0实现SSO.其中包括SharePoint,MVC,Exchange等应用程序的SSO集成. 整个系列将会由如下几个部分构成: 实战:ADFS3. ...
- jsp的三种自定义标签 写法示例
1.自定义方法标签 引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写 ...
- Spring系列: 使用aop报错:nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Refle
写了个最简单的aop例子 配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...
- [CareerCup] 10.1 Client-facing Service 面向客户服务器
10.1 Imagine you are building some sort of service that will be called by up to 1000 client applicat ...
- 做leetcode的几点体会分享(转)
1 大部分题目你都是可以自己做出来的.所以,第一遍尽量不要网上找答案: 2 写了的不管通过的,不通过的答案要保存下来.不通过的,也要记录下来哪儿没有通过.很有可能你这次错了,不知道怎么搞过了,下次还是 ...
- Linux及安全——模块
Linux及安全——模块 一.模块的编译.生成.测试.删除 1.编写模块代码 编写:gedit test.c 查看:cat test.c 2.查看版本信息 3.编写Makefile obj-m :这个 ...