UVA10596- Morning Walk

开始的时候我钻了牛角尖,总是想用dfs()找到一条欧拉回路,憋了好几天也没憋出来,就只好屈从了欧拉定理,

代码如下,不算高效的代码:

#include <iostream>
#include <cstring>
using namespace std;
int n, m, start = 0, in[250][250], visit[250], du[250];
int input()
{
    memset(in,0,sizeof(in));
    memset(visit,0,sizeof(visit));
    memset(du,0,sizeof(du));
    cin>>m;
    int a, b, _m=m;
    while(_m--)cin>>a>>b,in[a][b]=1,du[a]++, du[b]++;
    return 0;
}
void dfs(int u, int &count)
{
    visit[u] = 1;
    count++;
    for(int i = 0; i < n; i++)
    {
        if(!visit[i]&&in[u][i]){
            dfs(i, count);}
    }
}
int main ()
{
    while(cin>>n)
    {
        int flag = 1, count=0;
        input();
        for(int i = 0; i < n; i++)
        if(du[i]&1){flag = 0; cout<<"Not "; break;}
        if(flag){dfs(0,count);if(count!=n) cout<<"Not ";}
        cout<<"Possible"<<endl;
    }
    return 0;
}
另外我从vj上找到了一段神码跟大家分享一下:


#include<cstdio>
#include<cstring>

#define MAXX 222

short dg[MAXX];
short n,r,i,j;

int main()
{
    while(scanf("%hd %hd",&n,&r)!=EOF)
    {
        memset(dg,0,sizeof(dg));
        while(r--)
            scanf("%hd %hd",&i,&j),++dg[i],++dg[j];
        for(i=0;i<n;++i)
            if(!dg[i] || dg[i]&1)
                break;
        if(i<n)
            puts("Not Possible");
        else
            puts("Possible");
    }
    return 0;
}


posted on 2012-12-17 17:16  Primo...  阅读(130)  评论(0编辑  收藏  举报