usaco3.3.1 Riding the Fences
来源:互联网 发布:制动系统分析软件 编辑:程序博客网 时间:2024/06/09 23:32
一 原题
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
二 分析
三 代码
USER: Qi Shen [maxkibb3]TASK: fenceLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 5164 KB] Test 2: TEST OK [0.000 secs, 5164 KB] Test 3: TEST OK [0.000 secs, 5164 KB] Test 4: TEST OK [0.000 secs, 5164 KB] Test 5: TEST OK [0.000 secs, 5164 KB] Test 6: TEST OK [0.000 secs, 5164 KB] Test 7: TEST OK [0.000 secs, 5164 KB] Test 8: TEST OK [0.000 secs, 5164 KB]All tests OK.Your program ('fence') produced all correct answers! This is yoursubmission #10 for this problem. Congratulations!
/*ID:maxkibb3LANG:C++PROG:fence*/#include<cstdio>#include<vector>using namespace std;const int MAX_V = 501;int n;int map[MAX_V][MAX_V];int edge_num[MAX_V];vector<int> trace;void dfs(int pos) {for(int i = 1; i < MAX_V; i++) {if(map[pos][i] != 0) {map[pos][i]--;map[i][pos]--;dfs(i);}}trace.push_back(pos);}int main() {freopen("fence.in", "r", stdin);freopen("fence.out", "w", stdout);scanf("%d", &n);int s, e;for(int i = 0; i < n; i++) {scanf("%d%d", &s, &e);map[s][e]++;map[e][s]++;edge_num[s]++;edge_num[e]++;}for(int i = 1; i < MAX_V; i++) {if(edge_num[i] % 2 == 1) {dfs(i);break;}}if(trace.size() == 0) {for(int i = 1; i < MAX_V; i++) {if(edge_num[i] != 0) {dfs(i);break;}}}for(int i = trace.size() - 1; i >= 0; i--) {printf("%d\n", trace[i]);}return 0;}
- usaco3.3.1 Riding the Fences
- [USACO3.3.1]Riding the Fences
- USACO3.3.1 Riding The Fences (fence)
- USACO3.3.1 Riding the Fences(fence)
- usaco3.3Riding the Fences输出欧拉通路
- [USACO3.3]骑马修栅栏 Riding the Fences
- usaco Riding the Fences
- Riding the Fences
- Riding the Fences
- 深搜解Riding the Fences
- USACO: Chap3 Riding the Fences
- usaco 3.2 Riding The Fences
- usaco 3.3 Riding the Fences
- usaco java Riding the Fences
- usaco 3.3 Riding the Fences
- USACO Riding the Fences 解题报告
- USACO 3.3 Riding The Fences (fence)
- USACO Riding the fences and C implement
- Docker的数据管理
- Flash Builder4.7 更新Air SDK及FlashPlayer
- netstat的10个基本用法
- 我的屌丝giser成长记-工作篇之B公司
- python文件操作
- usaco3.3.1 Riding the Fences
- Java设计模式(13)行为型:策略模式
- Leetcode 339 Nested List Weight Sum
- MySQL数据库高并发优化配置
- Android Studio入门小例子
- 30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
- 记录访问信息
- 终于在CSDN开博客了
- ubuntu下编译lib_acl,lib_acl_cpp,lib_protocol