HDU 1878 歐拉迴路

來源:互聯網
上載者:User

標籤: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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.