標籤:des style color os io for ar div
Problem Description歐拉迴路是指不令筆離開紙面,可畫過圖中每條邊僅一次,且可以回到起點的一條迴路。現給定一個圖,問是否存在歐拉迴路?
Input測試輸入包含若干測試案例。每個測試案例的第1行給出兩個正整數,分別是節點數N ( 1 < N < 1000 )和邊數M;隨後的M行對應M條邊,每行給出一對正整數,分別是該條邊直接連通的兩個節點的編號(節點從1到N編號)。當N為0時輸入結
束。
Output每個測試案例的輸出佔一行,若歐拉迴路存在則輸出1,否則輸出0。
Sample Input
3 31 21 32 33 21 22 30
Sample Output
10
AuthorZJU 解決無向圖的歐拉迴路就是直接上。寫了UVA的那道,這題是弱化版。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>using namespace std;const int maxn=1100;int d[maxn];int pre[maxn];int n,m;void init(){ memset(d,0,sizeof(d)); for(int i=0;i<=n;i++) pre[i]=i;}int find_root(int x){ if(x!=pre[x]) pre[x]=find_root(pre[x]); return pre[x];}bool ok(){ int x=-1; for(int i=0;i<=n;i++) { if(d[i]&1) return false;; if(d[i]) { if(x==-1) x=find_root(i); else { if(x!=find_root(i)) return false; } } } return true;}int main(){ int x,y; while(~scanf("%d",&n)) { if(n==0) break; scanf("%d",&m); init(); for(int i=0;i<m;i++) { scanf("%d%d",&x,&y); d[x]++; d[y]++;; pre[find_root(x)]=find_root(y); } if(!ok()) cout<<0<<endl; else cout<<1<<endl; } return 0;}