Riding the Fences (USACO 3.3) 无向图欧拉通路/回路
来源:互联网 发布:淘宝店怎么申请店铺号 编辑:程序博客网 时间:2024/05/16 01:18
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)
91 22 33 44 24 52 55 65 74 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)
1234254657
两点注意:首先,没有跟你说点的序号一定是从1开始的,两个点之间可能有不止一条道路。
/* ID: hysfwjr1PROG: fence LANG: C++ */#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <memory.h>#define _DEBUG 0#define MAXN 502#define MAXE 1025int fence[MAXN][MAXN];int degree[MAXN];int path[MAXE<<1];//路径节点int pc=0;//路径节点计数void Eulerian(int start){assert(start>=0);if(degree[start]>0){for(int i=1;i<=MAXN;i++){if(fence[start][i]>0){//可能不止一条边//去掉此路径degree[start]--;degree[i]--;fence[start][i]--;fence[i][start]--;Eulerian(i);}}}path[pc++]=start;}int main(){freopen("fence.in","r",stdin);#if _DEBUG==0freopen("fence.out","w",stdout);#endifint e;int i;scanf("%d",&e);int u,v;for(i=0;i<e;i++){scanf("%d %d",&u,&v);degree[u]++;degree[v]++;fence[u][v]++;fence[v][u]++;}//寻找起点int start=-1;for(i=1;i<=MAXN;i++)//寻找第1个度不为0的点if(degree[i]!=0){start=i;break;}for(i=start;i<=MAXN;i++){//寻找第1个度为奇数的点if(degree[i]&0x1){start=i;break;}}Eulerian(start);for(i=pc-1;i>=0;i--){printf("%d\n",path[i]);}return 0;}
- Riding the Fences (USACO 3.3) 无向图欧拉通路/回路
- USACO Section 3.3 Riding The Fences - 欧拉回路
- 欧拉回路 USACO 3.3.1 Riding the Fences
- USACO-Section 3.3 Riding the Fences(欧拉回路)
- usaco 3.3 Riding the Fences
- usaco 3.3 Riding the Fences
- usaco Riding the Fences
- USACO section 3.3 Riding the Fences(欧拉通路的遍历,dfs)
- USACO 3.3.1Riding the Fences(弗罗莱算法寻找欧拉通路)
- 欧拉路径, 欧拉回路 USACO Riding the Fences
- usaco Riding the Fences(欧拉回路模板)
- USACO 3.3 Riding The Fences (fence)
- USACO-Section 3.3-PROB Riding The Fences
- USACO: Chap3 Riding the Fences
- usaco 3.2 Riding The Fences
- usaco java Riding the Fences
- USACO Riding Fences 欧拉回路
- usaco Chapter 3 section 3.3 Riding the Fences
- 开发人员一定要加入收藏夹的网站
- Android String 转 MD5
- 多态的总结
- Kindeditor跨域上传图片正确修改方法
- 【ajax技术学习】原生ajax表单用户名验证
- Riding the Fences (USACO 3.3) 无向图欧拉通路/回路
- codility上的问题 (19)Sigma 2012
- CSDN,我来了
- C# AES-256-CBC 加解密
- linux arm2440 LCD 控制
- C语言标识符命名规则
- 第八章接口与内部类
- 自定义DTD(myeclipser的XML提示功能)
- P53----例题