日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

欧拉回路(HDU-1878)

發(fā)布時(shí)間:2025/3/17 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 欧拉回路(HDU-1878) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Problem Description

歐拉回路是指不令筆離開紙面,可畫過圖中每條邊僅一次,且可以回到起點(diǎn)的一條回路。現(xiàn)給定一個(gè)圖,問是否存在歐拉回路?

Input

測試輸入包含若干測試用例。每個(gè)測試用例的第1行給出兩個(gè)正整數(shù),分別是節(jié)點(diǎn)數(shù)N ( 1 < N < 1000 )和邊數(shù)M;隨后的M行對(duì)應(yīng)M條邊,每行給出一對(duì)正整數(shù),分別是該條邊直接連通的兩個(gè)節(jié)點(diǎn)的編號(hào)(節(jié)點(diǎn)從1到N編號(hào))。當(dāng)N為0時(shí)輸入結(jié)
束。

Output

每個(gè)測試用例的輸出占一行,若歐拉回路存在則輸出1,否則輸出0。

Sample Input

3 3
1 2
1 3
2 3
3 2
1 2
2 3
0

Sample Output

1
0

思路:使用并查集統(tǒng)計(jì)各節(jié)點(diǎn)度數(shù)即可,若圖連通且所有點(diǎn)的度數(shù)為偶數(shù),則說明該無向圖存在歐拉回路

Source Program

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<map> #include<stack> #include<ctime> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 1001 #define MOD 16007 #define E 1e-6 #define LL long long using namespace std; int n,m; int degree[N]; int father[N]; int Find(int x){if(father[x]==-1)return x;return father[x]=Find(father[x]); } void Union(int x,int y){x=Find(x);y=Find(y);if(x!=y)father[x]=y; } int main(){while(scanf("%d%d",&n,&m)!=EOF){memset(degree,0,sizeof(degree));memset(father,-1,sizeof(father));for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);degree[x]++;degree[y]++;Union(x,y);}int cnt=0;//記錄連通分量for(int i=1;i<=n;i++)if(Find(i)==i)cnt++;if(cnt!=1)//若cnt大于1,說明圖不連通printf("0\n");else{int num=0;//統(tǒng)計(jì)度數(shù)為奇數(shù)的點(diǎn)for(int i=1;i<=n;i++)if(degree[i]&1)num++;if(num==0)printf("1\n");elseprintf("0\n");}}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的欧拉回路(HDU-1878)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。