POJ 1041 John's trip Euler歐拉迴路判定和求迴路

來源:互聯網
上載者:User

標籤:des   class   blog   code   使用   2014   

就是歐拉判定,判定之後就可以使用DFS求歐拉迴路了。圖論內容。

這裡使用鄰接矩陣會快很多速度。

這類題目都是十分困難的,光是定義的記錄的陣列變數就會是一大堆。


#include <cstdio>#include <cstring>#include <stack>#include <vector>using namespace std;struct Edge{int ed, des;Edge(int e = 0, int d = 0) : ed(e), des(d) {}};const int EDGES = 2000;//1996;const int VEC = 45;stack<int> stk;int degree[VEC];vector<Edge> gra[VEC];bool vis[EDGES];void euler(int u){for(int i = 0; i < (int)gra[u].size(); i++){if(!vis[gra[u][i].ed])//標誌訪問過了,這裡需要表示橋,不是頂點{vis[gra[u][i].ed] = true;euler(gra[u][i].des);stk.push(gra[u][i].ed);//不能break}}}int main(){int x, y, z, one;while (scanf("%d %d", &x, &y) != EOF && x && y){memset(vis, 0, sizeof(vis));memset(degree, 0, sizeof(degree));for (int i = 1; i < VEC; i++)gra[i].clear();scanf("%d", &z);one = min(x, y);degree[x]++; degree[y]++;gra[x].push_back(Edge(z, y)); gra[y].push_back(Edge(z, x));while (scanf("%d %d", &x, &y) != EOF && x && y){scanf("%d", &z);gra[x].push_back(Edge(z, y)); gra[y].push_back(Edge(z, x));degree[x]++, degree[y]++;}for (int i = 1; i < VEC; i++){if (degree[i] & 1){puts("Round trip does not exist.");goto endLoop;//玩玩goto}}euler(one);while (!stk.empty()){printf("%d ", stk.top());stk.pop();}putchar('\n');endLoop:;}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.