[USACO3.3.1]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 farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.

Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection. It is always possible to ride from any fence to any other fence (i.e., all fences are "connected").

Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.

There will always be at least one solution for each set of input data supplied to your program for testing.

PROGRAM NAME: fence

INPUT FORMAT

Line 1:The number of fences, F (1 <= F <= 1024)
Line 2..F+1:A pair of integers (1 <= i,j <= 500) that tell which pair of intersections this fence connects.

SAMPLE INPUT (file fence.in)

9
1 2
2 3
3 4
4 2
4 5
2 5
5 6
5 7
4 6

OUTPUT FORMAT

The output consists of F+1 lines, each containing a single integer. Print the number of the starting intersection on the first line, the next intersection's number on the next line, and so on, until the final intersection on the last line. There might be many possible answers to any given input set, but only one is ordered correctly.

SAMPLE OUTPUT (file fence.out)

1
2
3
4
2
5
4
6
5
7
 
一道还算不错的欧拉通路(回路)题目,但是这题比较坑的点就是,要按照字典序输出,所以不能用邻接表(数组模拟指针)等存储,得用邻接矩阵。
最小的应该是从最小的奇数点开始(其实最多就一个),如果没有奇数点,从最小的点开始,然后每次到最小的点,同时记录。。
欧拉路劲做法同

http://train.usaco.org/usacotext2?a=Zuvo26a11bg&S=euler

The Algorithm

Detecting whether a graph has an Eulerian tour or circuit is actually easy; two different rules apply.

  • A graph has an Eulerian circuit if and only if it is connected (once you throw out all nodes of degree 0) and every node has `even degree'.
  • A graph has an Eulerian path if and only if it is connected and every node except two has even degree.
  • In the second case, one of the two nodes which has odd degree must be the start node, while the other is the end node.

The basic idea of the algorithm is to start at some node the graph and determine a circuit back to that same node. Now, as the circuit is added (in reverse order, as it turns out), the algorithm ensures that all the edges of all the nodes along that path have been used. If there is some node along that path which has an edge that has not been used, then the algorithm finds a circuit starting at that node which uses that edge and splices this new circuit into the current one. This continues until all the edges of every node in the original circuit have been used, which, since the graph is connected, implies that all the edges have been used, so the resulting circuit is Eulerian.

More formally, to determine a Eulerian circuit of a graph which has one, pick a starting node and recurse on it. At each recursive step:

  • Pick a starting node and recurse on that node. At each step:
    • If the node has no neighbors, then append the node to the circuit and return
    • If the node has a neighbor, then make a list of the neighbors and process them (which includes deleting them from the list of nodes on which to work) until the node has no more neighbors
    • To process a node, delete the edge between the current node and its neighbor, recurse on the neighbor, and postpend the current node to the circuit.
And here's the pseudocode:
# circuit is a global array
   find_euler_circuit
     circuitpos = 0
     find_circuit(node 1)

# 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 + 1
    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 + 1

To find an Eulerian tour, simply find one of the nodes which has odd degree and callfind_circuit with it.

Both of these algorithms run in O(m + n) time, where m is the number of edges and n is the number of nodes, if you store the graph in adjacency list form. With larger graphs, there's a danger of overflowing the run-time stack, so you might have to use your own stack.


我承认,我英语差,完全看不懂,但是至少以前听到过一点,然后还是做出来了。

/*
ID:cqz15311
PROG:fence
LANG:C++
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int maxe = 1050 * 2;
const int maxv = 505;
int Map[maxv][maxv];
int Path[maxe],cnt;
int num[maxv];
int Min,Max;
void Dfs(int u){
	if (num[u]){
		do{
			int v;
			for (v = Min;v<=Max;v++){
				if (Map[u][v]) break;
			}
			Map[u][v]--;
			Map[v][u]--;
			num[u]--;
			num[v]--;
			Dfs(v);
		} while (num[u]);
		Path[++cnt] = u;
	}
	 else{
		Path[++cnt] = u;
	}
	
	
}
void solve(){
	for (int i=Min;i<=Max;i++){
		if ((num[i] % 2 == 1) && (num[i] != 0)){
			Dfs(i);
			return ;
		}
	}
	Dfs(Min);
}

int main(){
	freopen("fence.in","r",stdin);
	freopen("fence.out","w",stdout);
	int F,a,b,In[maxv];
	scanf("%d",&F);
	Min = 500;Max = 1;
	while (F--){
		scanf("%d%d",&a,&b);
		Map[a][b]++;
		Map[b][a]++;
		num[a]++;
		num[b]++;
		Min = min(Min,min(a,b));
		Max = max(Max,max(a,b));
	}
	solve();	
	for (int i=cnt;i>=1;i--){
		printf("%d\n",Path[i]);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值