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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

PAT甲级1126 Eulerian Path:[C++题解] 欧拉路径、并查集,测试点4有问题请进来

發布時間:2025/4/5 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PAT甲级1126 Eulerian Path:[C++题解] 欧拉路径、并查集,测试点4有问题请进来 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 題目分析
    • 題目鏈接

題目分析


來源:acwing

分析:
歐拉圖: 1)連通 2)度都為偶數
半歐拉圖:歐拉路徑:2)連通2) 度為奇數的結點有兩個,其他度都是偶數
非歐拉圖:不是歐拉圖和半歐拉圖。
判連通使用并查集。

ac代碼(并查集)

#include<bits/stdc++.h> using namespace std; const int N =510; int cnt[N];//每個點的度數 int n ,m; bool g[N][N];//有邊int p[N];//找根 int find(int x){if(p[x] != x) p[x] =find(p[x]);return p[x]; }int main(){cin >> n>> m;for(int i = 1; i<=n; i++) p[i] =i;while(m--){int a, b;cin >> a >> b;cnt[a]++,cnt[b]++;p[find(a)] = find(b); //合并集合,如果已經是同一個集合會自動忽略}//統計度int one =0; //統計度為奇數的結點個數for(int i = 1 ;i <=n; i++){if( cnt[i] &1 ) one ++;}//判連通性//用set保存根結點個數set<int> root;for(int i=1; i<=n; i++)root.insert(find(i));int num =root.size();cout<<cnt[1];for(int i =2; i<= n; i++) cout<<" "<<cnt[i];cout<<endl;if(num==1 &&one == 0) cout<<"Eulerian"<<endl;else if(num ==1&& one == 2) cout<<"Semi-Eulerian"<<endl;else cout<<"Non-Eulerian"<<endl; }

wa1個點的代碼(同樣使用并查集)
下面的代碼測試點4有問題
錯誤原因:統計錯了度為偶數的結點個數,統計度為奇數的就沒有問題了。也就是最后if、else有問題

#include<bits/stdc++.h> using namespace std; const int N =510; int cnt[N];//每個點的度數 int n ,m; bool g[N][N];//有邊int p[N];int find(int x){if(p[x] != x) p[x] =find(p[x]);return p[x]; }int main(){cin >> n>> m;for(int i = 1; i<=n; i++) p[i] =i;while(m--){int a, b;cin >> a >> b;cnt[a]++,cnt[b]++;if(find(a)!=find(b)) p[find(a)] = find(b);}//統計度int one = 0 ,two =0;for(int i = 1 ;i <=n; i++){if(cnt[i]&1) one++;if(cnt[i] && cnt[i] % 2 ==0) two ++;}//判連通性//用set保存根結點個數set<int> root;for(int i=1; i<=n; i++)root.insert(find(i));int num =root.size();cout<<cnt[1];for(int i =2; i<= n; i++) cout<<" "<<cnt[i];cout<<endl;if(num==1 &&two == n) cout<<"Eulerian"<<endl;else if(num ==1&& two == n -2) cout<<"Semi-Eulerian"<<endl; //這里錯了,不能保證one有2個,因為n可能是奇數else cout<<"Non-Eulerian"<<endl; }

題目鏈接

PAT甲級1126 Eulerian Path

總結

以上是生活随笔為你收集整理的PAT甲级1126 Eulerian Path:[C++题解] 欧拉路径、并查集,测试点4有问题请进来的全部內容,希望文章能夠幫你解決所遇到的問題。

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