POJ 1041 John’s trip (歐拉迴路)

來源:互聯網
上載者:User

題意:John買了新車,想開車去拜訪他的朋友,已知每條街道上都恰巧有他的一個朋友,所以他想穿過每條街道一次並且回到起點。城鎮上街道總數不超過1995,節點總數不超過44,任意一個節點所關聯的街道總數不超過44,每一條街道所關聯的兩個節點都不同,每條街道的編號也不同。起點是第一次輸入的兩個節點中較小的那個。若存在多條迴路,輸出字典序最小的。

#include <iostream>using namespace std;#define MAXN 2000#define max(a,b) ((a)>(b)?(a):(b))#define min(a,b) ((a)<(b)?(a):(b))struct Edge{int st, ed;bool del;} edge[MAXN];int stk[MAXN];int deg[50];int top, E, V;void find_path ( int now ){for ( int i = 1; i <= E; i++ )  // 邊從小到大取{if ( ! edge[i].del && (edge[i].st == now || edge[i].ed == now) ){edge[i].del = true;if ( edge[i].st == now )    find_path ( edge[i].ed );elsefind_path ( edge[i].st );stk[++top] = i;}}}int main(){int x, y, z, s;while ( 1 ){scanf("%d%d",&x,&y);if ( x == 0 && y == 0 ) break;memset(deg,0,sizeof(deg));s = min ( x, y );E = top = 0;scanf("%d",&z);edge[z].st = x;edge[z].ed = y;edge[z].del = false;deg[x]++; deg[y]++;E = max ( E, z );V = max ( V, max(x,y) );    while ( 1 ){scanf("%d%d",&x,&y);if ( x == 0 && y == 0 ) break;scanf("%d",&z);edge[z].st = x;        edge[z].ed = y;        edge[z].del = false;deg[x]++; deg[y]++;E = max ( E, z );V = max ( V, max(x,y) );}int i;for ( i = 1; i <= V; i++ )if ( deg[i] % 2 ) break;if ( i <= V )printf("Round trip does not exist.\n");else{find_path ( s );for ( i = top; i >= 1; i-- )printf("%d ",stk[i]);printf("\n");}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.