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坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点( ...
随机推荐
- Volley(四)—— ImageLoader & NetworkImageView
Volley(四)—— ImageLoader & NetworkImageView ImageLoader是一个加载网络图片的封装类,其内部还是由ImageRequest来实现的.但因为源码 ...
- localStorage实现购物车数量单价和总价实时同步(二)
利用localStorage实时显示购物车小计和总价页面显示: 和昨天的原理相同,本地存储同时实时循环计算总价之和,注意循环时候的先清空再计算 Success is getting what you ...
- 浅谈设计模式--单例模式(Singleton Pattern)
题外话:好久没写blog,做知识归纳整理了.本来设计模式就是个坑,各种文章也写烂了.不过,不是自己写的东西,缺少点知识的存在感.目前还没做到光看即能记住,得写.所以准备跳入设计模式这个大坑. 开篇先贡 ...
- 2015某编程网易语言vip课堂全套教程 包含post,hook入门到精通等
2015某编程网易语言vip课堂全套教程 包含post,hook入门到精通等 官方论坛弄来的 如果在官方下载需要权限的 挺不错教程 想学习易语言入门到精通 post hook js改写的可以看 ...
- Android ViewPager使用详解
这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api.而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那如 ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- Linux及安全——Linux基础实践
Linux及安全——Linux基础实践 一.实践一:掌握软件源的维护方法,配置系统使用教育网内的软件源镜像.掌握通过软件源来查找,安装,卸载,更新软件的方法. 1.软件源的维护方法 Ubuntu的软件 ...
- openssl实践总结
openssl实验总结 OPENSSL简介 OpenSSL项目是一个协作开发一个健壮的,商业级的,全功能的,并且开放源代码工具包,它实现了安全套接字层(SSL v2/v3)和传输层安全(TLS v1) ...
- 基于微信红包插件的原理实现android任何APP自动发送评论(已开源)
背景 地址:https://github.com/huijimuhe/postman 核心就是android的AccessibilityService,回复功能api需要23以上版本才行. 其实很像在 ...
- 细说 Web API参数绑定和模型绑定
今天跟大家分享下在Asp.NET Web API中Controller是如何解析从客户端传递过来的数据,然后赋值给Controller的参数的,也就是参数绑定和模型绑定. Web API参数绑定就是简 ...