USACO 3.3 fence 欧拉回路

题意:求给定图的欧拉回路(每条边只走一次)

若欧拉回路存在,图中只可能有0个or2个奇数度的点。

求解时,若有奇数度的点,则必须从该点开始。否则可以从任一点开始

求解过程:dfs

 1 //主程序部分
 2 # circuit is a global array
 3    find_euler_circuit
 4      circuitpos = 0
 5      find_circuit(node 1)
 6 ---------------------------------------------
 7 # nextnode and visited is a local array
 8 # the path will be found in reverse order
 9 //递归函数
10   find_circuit(node i)
11     if node i has no neighbors then
12       circuit(circuitpos) = node i
13       circuitpos = circuitpos + 1
14     else
15       while (node i has neighbors)
16           pick a random neighbor node j of node i
17           delete_edges (node j, node i)
18           find_circuit (node j)
19       circuit(circuitpos) = node i
20       circuitpos = circuitpos + 1
21 -------------------------------------------
22 最终结果:将circuit()数组倒序输出即可

 1 /*
 2 PROB:fence
 3 LANG:C++
 4 */
 5 #include <iostream>
 6 #include <cstring>
 7 #include <cstdio>
 8 using namespace std;
 9 #define INF 999999
10
11 int dx=INF,dy=0,n,f,x,y,p;
12 int e[1100][1100];
13 int t[1100],seq[1100];
14
15 void dfs(int x)
16 {
17     for (int i=dx;i<=dy;i++)
18         if (e[x][i]>0)
19         {
20             e[x][i]--;
21             e[i][x]--;
22             dfs(i);
23         }
24     p++;
25     seq[p]=x;
26 }
27
28 int start()
29 {
30     for (int i=dx;i<=dy;i++)
31         if (t[i]%2!=0)
32             return i;
33     return 1;
34 }
35
36 int main()
37 {
38     freopen("fence.in","r",stdin);
39     freopen("fence.out","w",stdout);
40
41     memset(e,0,sizeof(e));
42     memset(t,0,sizeof(t));
43     cin>>f;
44     for (int i=1;i<=f;i++)
45     {
46         cin>>x>>y;
47         e[x][y]++;
48         e[y][x]++;
49         t[x]++;
50         t[y]++;
51         if (x<dx)   dx=x;
52         if (y<dx)   dx=y;
53         if (x>dy)   dy=x;
54         if (y>dy)   dy=y;
55
56     }
57
58     x=start();
59     //cout<<dx<<" "<<dy<<"--"<<x<<endl;
60     p=0;
61     dfs(x);
62
63     //cout<<p<<endl;
64     for (int i=p;i>=1;i--)
65         cout<<seq[i]<<endl;
66
67     return 0;
68 }

时间: 2024-08-01 22:40:15

USACO 3.3 fence 欧拉回路的相关文章

USACO 4.1 Fence Loops

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

USACO 4.1 Fence Rails

Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber s

USACO 6.3 Fence Rails(一道纯剪枝应用)

Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber s

USACO 4.1 Fence Loops(Floyd求最小环)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

USACO fence

题目的意思就是给你一个图, 输出他的欧拉路(欧拉通路 或者 欧拉回路),无向图欧拉回路判断条件是:1:图连通 2:所有点的度数为偶数     无向图欧拉通路的条件是:1:图连通 2:有且只有两个点的度数为奇数, 不过寻找欧拉路的代码是一样的,学习了新的建图方法,代码如下: /* ID: m1500293 LANG: C++ PROG: fence */ #include <cstdio> #include <cstring> #include <algorithm> #

POJ 2230 Watchcow &amp;&amp; USACO Watchcow 2005 January Silver (欧拉回路)

题意: Bessie 最近做了农场看守,他每天晚上的工作就是巡视农场并且保证没有坏人破坏农场.从谷仓出发去巡视,并且最终回到谷仓. Bessie 视力不是很好,不能像其他农场的看守一样,对农场的每一条连接不同场地的路走一遍就可以发现是不是有异常情况,他需要每条路都走两遍,并且这两边必须是不同的方向,因为他觉得自己应该不会两次都忽略农场中的异常情况. 每块地之间一定会由至少一条路相连.现在的任务就是帮他制定巡视路线.前提假设一定存在满足题意的路径. 输入: 第一行输入两个数N(2 <= N <=

usaco Electric Fence

这种有小数的题目总会令我格外头疼. /* ID: modengd1 PROG: fence9 LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <math.h> using namespace std; long long ans,x1,x2; int n,m,p; int main() { freopen("fence9.in"

USACO 3.4 Electric Fence 皮克定理

题意:在方格纸上画出一个三角形,求三角形里面包含的格点的数目 因为其中一条边就是X轴,一开始想的是算出两条边对应的数学函数,然后枚举x坐标值求解.但其实不用那么麻烦. 皮克定理:给定顶点坐标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i.边上格点数目b的关系:A = i + b/2 - 1. 有了这条定理就好办了. 三角形面积直接用公式就能算出来. 对于从点(0,0)到点(x,y)的线段,该线段上的格点数目即gcd(x,y)+1 这样A和b都有了,套公式就行了.

USACO 3.3 Riding the Fences

Riding the Fences Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts. Farmer John is as lazy as the next