日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ - 2230 Watchcow(欧拉图)

發布時間:2024/4/11 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ - 2230 Watchcow(欧拉图) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一張由n個點和m條邊組成的無向圖,要求我們從點1為起點,沿著每條路都走一遍,正向邊反向邊都走恰好一次,最后到點1結束,題目需要我們輸出路徑

題目分析:歐拉圖模板題目,因為題目中保證了給出的圖一定時歐拉圖,而歐拉圖的定義是每個點的度數都是偶數,意思就是只要到達一個節點,那么就必定有一條尚未走過的邊可以離開該點,所以我們可以不斷用dfs搜索,并標記已經走過的邊,實時輸出就好了

當然因為遞歸層數過多,如果怕爆棧的話也可以自己手寫棧然后用循環模擬dfs遞歸的過程

最后插個眼,感覺這個博客解釋歐拉路和歐拉圖比較生動形象,尤其是解釋了為什么要在回溯的時候輸出答案而不是在開頭就記錄答案:https://blog.csdn.net/a_bright_ch/article/details/82954119

代碼:

遞歸實現:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e4+100;struct Node {int to;bool flag;Node(int TO){to=TO;flag=true;} };vector<Node>node[N];void dfs(int u) {for(int i=0;i<node[u].size();i++){int v=node[u][i].to;if(node[u][i].flag){node[u][i].flag=false;dfs(v);}}printf("%d\n",u); }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int n,m;scanf("%d%d",&n,&m);while(m--){int u,v;scanf("%d%d",&u,&v);node[u].push_back(Node(v));node[v].push_back(Node(u));}dfs(1);return 0; }

循環實現:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e4+100;const int M=1e5+100;struct Node {int to,next; }edge[M];int head[N],Stack[M],cnt,top;void addedge(int u,int v) {edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++; }void euler(int st) {Stack[++top]=st;while(top){int x=Stack[top],k=head[x];if(k!=-1){Stack[++top]=edge[k].to;head[x]=edge[k].next;}else{top--;printf("%d\n",x);}} }void init() {memset(Stack,0,sizeof(Stack));memset(head,-1,sizeof(head));cnt=top=0; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);init();int n,m;scanf("%d%d",&n,&m);while(m--){int u,v;scanf("%d%d",&u,&v);addedge(u,v);addedge(v,u);}euler(1);return 0; }

?

總結

以上是生活随笔為你收集整理的POJ - 2230 Watchcow(欧拉图)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。