標籤:ace size com man out 歐拉路徑 end pos print
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: fenceINPUT 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 /* 2 ID: ivorysi 3 PROG: fence 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstring> 9 #include <algorithm>10 #include <queue>11 #include <set>12 #include <vector>13 #define siji(i,x,y) for(int i=(x);i<=(y);++i)14 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)15 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)16 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)17 #define inf 0x7fffffff18 #define MAXN 40000519 #define ivorysi20 #define mo 9779797721 #define ha 97471122 #define ba 4723 #define fi first24 #define se second25 #define pii pair<int,int>26 using namespace std;27 typedef long long ll;28 int adj[600][600];29 int size[600];30 int po[605];31 int path[2016],cnt;32 void init() {33 int f,u,v;34 scanf("%d",&f);35 siji(i,1,f) {36 scanf("%d%d",&u,&v);37 ++size[u];++size[v];38 ++adj[u][v];++adj[v][u];39 }40 }41 void dfs(int u) {42 while(po[u]<=500) {43 while(adj[u][po[u]]) {44 --adj[po[u]][u];--adj[u][po[u]];45 dfs(po[u]);46 }47 ++po[u];//自加要放到下面48 }49 path[++cnt]=u;50 }51 void solve() {52 init();53 int val=-1;54 siji(i,1,500) if(size[i]%2) {val=i;break;}55 if(val==-1) {56 siji(i,1,500) if(size[i]!=0) {val=i;break;}57 }58 dfs(val);59 gongzi(i,cnt,1) {60 printf("%d\n",path[i]);61 }62 }63 int main(int argc, char const *argv[])64 {65 #ifdef ivorysi66 freopen("fence.in","r",stdin);67 freopen("fence.out","w",stdout);68 #else69 freopen("f1.in","r",stdin);70 #endif71 solve();72 }
http://www.cnblogs.com/ivorysi/p/5745005.html
以前寫的超級認真的歐拉迴路……但其實USACO上講的也很好
USACO 3.3 Riding the Fences