当前位置 : 主页 > 编程语言 > python >

USACO section 3.3 Riding the Fences(欧拉通路的遍历,dfs)

来源:互联网 收集:自由互联 发布时间:2023-03-22
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

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)

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

 

思路:遍历欧拉通路的方法是用深搜起始边开始的每一条边,当某个点的边都遍历完就用栈记录这个点,当深搜结束后,依次从栈取出的就是遍历过程的答案。

 

 

具体看代码:

/*ID:nealgav1LANG:C++PROG:fence*/#include<fstream>#include<algorithm>#include<vector>#include<queue>using namespace std;ifstream cin("fence.in");ofstream cout("fence.out");const int mm=520;vector<int>edge[mm];///容器存边方便int ans[1234567],pos;///模拟栈,存答案的int vis[mm][mm];///判断边是否有被遍历过void dfs(int u,int v){ vector<int>::iterator it; vis[u][v]--;vis[v][u]--;///遍历的边删去 for(it=edge[v].begin();it!=edge[v].end();it++) { int z=*it; if(vis[v][z])///深搜下一条边 { dfs(v,z); //cout<<"pos="<<pos<<endl; } } //vis[u][v]++;vis[v][u]++; ans[pos++]=u;///搜完记录答案}int main(){ int cas,u,v; cin>>cas; for(int i=0;i<503;i++) for(int j=0;j<503;j++) vis[i][j]=0; while(cas--) { cin>>u>>v;edge[u].push_back(v);edge[v].push_back(u); vis[u][v]++;vis[v][u]++; } int start=550;int flag=0; int end=505; ///从小到大排序 for(int i=0;i<=500;i++)sort(edge[i].begin(),edge[i].end()); ///找起点和终点 for(int i=1;i<=500;i++) { if(edge[i].size()&&i<start)start=end=i; if(edge[i].size()&1) { if(!flag) start=i; else {end=i;break;} flag++; } } pos=0; dfs(start,edge[start].front()); for(int i=pos-1;i>=0;i--) cout<<ans[i]<<"\n"; ///最后一个点特殊处理输出 cout<<end<<"\n"; cin.close();cout.close();}

 

 

Riding the Fences

Hal Burch

Assuming you pick the lowest index vertex connected to each node, the Eulerian Path algorithm actually determines the path requested, although in the reverse direction. You must start the path determination at the lowest legal vertex for this to work.

/* Prob #5: Riding the Fences */#include <stdio.h>#include <string.h>#define MAXI 500#define MAXF 1200char conn[MAXI][MAXI];int deg[MAXI];int nconn;int touched[MAXI];int path[MAXF];int plen;/* Sanity check routine */void fill(int loc) { int lv; touched[loc] = 1; for (lv = 0; lv < nconn; lv++) if (conn[loc][lv] && !touched[lv]) fill(lv); }/* Sanity check routine */int is_connected(int st) { int lv; memset(touched, 0, sizeof(touched)); fill(st); for (lv = 0; lv < nconn; lv++) if (deg[lv] && !touched[lv]) return 0; return 1; }/* this is exactly the Eulerian Path algorithm */void find_path(int loc) { int lv; for (lv = 0; lv < nconn; lv++) if (conn[loc][lv]) { /* delete edge */ conn[loc][lv]--; conn[lv][loc]--; deg[lv]--; deg[loc]--; /* find path from new location */ find_path(lv); } /* add this node to the `end' of the path */ path[plen++] = loc; }int main(int argc, char **argv) { FILE *fin, *fout; int nfen; int lv; int x, y; if (argc == 1) { if ((fin = fopen("fence.in", "r")) == NULL) { perror ("fopen fin"); exit(1); } if ((fout = fopen("fence.out", "w")) == NULL) { perror ("fopen fout"); exit(1); } } else { if ((fin = fopen(argv[1], "r")) == NULL) { perror ("fopen fin filename"); exit(1); } fout = stdout; } fscanf (fin, "%d", &nfen); for (lv = 0; lv < nfen; lv++) { fscanf (fin, "%d %d", &x, &y); x--; y--; conn[x][y]++; conn[y][x]++; deg[x]++; deg[y]++; if (x >= nconn) nconn = x+1; if (y >= nconn) nconn = y+1; } /* find first node of odd degree */ for (lv = 0; lv < nconn; lv++) if (deg[lv] % 2 == 1) break; /* if no odd-degree node, find first node with non-zero degree */ if (lv >= nconn) for (lv = 0; lv < nconn; lv++) if (deg[lv]) break;#ifdef CHECKSANE if (!is_connected(lv)) /* input sanity check */ { fprintf (stderr, "Not connected?!?\n"); return 0; }#endif /* find the eulerian path */ find_path(lv); /* the path is discovered in reverse order */ for (lv = plen-1; lv >= 0; lv--) fprintf (fout, "%i\n", path[lv]+1); return 0; }
网友评论