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坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点( ...
随机推荐
- 用yo命令创建项目
1,npm install -g yo 安装yeoman 2,npm install -g generator-webapp 安装项目脚手架(生成器) 如果安装angular的项目后面则是genera ...
- transition的局限
transition的优点在于简单易用,但是它有几个很大的局限. (1)transition需要事件触发,所以没法在网页加载时自动发生. (2)transition是一次性的,不能重复发生,除非一再触 ...
- awk 和 sed 使用案例
1.模仿wc的行为,统计一个文本文件的文件的字符数.行数.单词数. awk '{numOfChar+=length($0);numOfWord+=NF}END{print numOfChar" ...
- [转]iptables详解
FROM : http://blog.chinaunix.net/uid-26495963-id-3279216.html 一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的 ...
- 0.1 hint crack
http://files.cnblogs.com/files/crac/27.rar
- no.4 抽奖测试
#-*-coding=gbk-*- import sys import random a=[] try: for x in range(1,20+1,1): #打印20人数编号 a.append(x) ...
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
既然是一个窗体设计器,那就应该能够设置控件的属性,设置属性最好的当然是PropertyGrid了,我们仅仅需要使用一个PropertyGrid.SelectedObject = Control就可以搞 ...
- 实现chrome扩展启动本地进程 - 补充
实现chrome扩展启动本地进程 - 补充 标签: chrome扩展启动本地程序访问本地磁盘 2014-10-17 11:42 6753人阅读 评论(17) 收藏 举报 分类: Chrome Plu ...
- Android Home键状态保存运用场景
当我们在一个Activity中有接收Intent过来的值,或者当前Activity有保存数据时候,如果此时不小心按到了Home键,然后没有及时回来而是运行了其它应用程序,当你想起来的时候,恐怕已经是几 ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...