`
473687880
  • 浏览: 485488 次
文章分类
社区版块
存档分类
最新评论

Morning Walk UVA 10596

 
阅读更多

Problem H

Morning Walk

Time Limit

3 Seconds

Kamalis aMotashotaguy. He has got a new job inChittagong. So, he has moved toChittagongfromDinajpur. He was getting fatter inDinajpuras he had no work in his hand there. So, moving toChittagonghas turned to be a blessing for him. Every morning he takes a walk through the hilly roads of charming cityChittagong. He is enjoying this city very much. There are so many roads inChittagongand every morning he takes different paths for his walking. But while choosing a path he makes sure he does not visit a road twice not even in his way back home. An intersection point of a road is not considered as the part of the road. In a sunny morning, he was thinking about how it would be if he could visit all the roads of the city in a single walk. Your task is to helpKamalin determining whether it is possible for him or not.

Input

Input will consist of several test cases. Each test case will start with a line containing two numbers. The first number indicates the number of road intersections and is denoted byN(2 ≤ N ≤ 200). The road intersections are assumed to be numbered from0toN-1. The second numberRdenotes the number of roads (0 ≤ R ≤ 10000). Then there will beRlines each containing two numbersc1andc2indicating the intersections connecting a road.

Output

Print a single line containing the text “Possible” without quotes if it is possible forKamalto visit all the roads exactly once in a single walk otherwise print “Not Possible”.

Sample Input

Output for Sample Input

22

0 1

1 0

2 1

0 1

Possible

Not Possible

Problemsetter: Muhammad Abul Hasan

International Islamic UniversityChittagong



这道题一看觉得是挺简单的欧拉图问题,一想总觉得不对劲,果然卡了很久,对欧拉图一点都不熟悉,代码还参考了别人的,在这里写一下欧拉图的判断

有向图欧拉路或欧拉回路的判定方法:
(1)有向图G为欧拉图(存在欧拉回路),当且仅当G的基图连通,且所有顶点的入度等于出度。
(2)有向图G为半欧拉图(存在欧拉道路),当且仅当G的基图连通,且存在顶点u的入度比出度大1、v的入度比出度小1,其它所有顶点的入度等于出度。

无向图的欧拉图判定方法:

(1) 图连通。

(2)所有的点的度数(出度和入度之和)为偶数。

#include<iostream>
#include<cstring>

using namespace std;

int du[210];
int bin[210];

int findx(int t)
{
    int r=t;
    while(bin[r]!=r)
    {
        r=bin[r];
    }
    return r;
}

void mergex(int x,int y)
{
    int fx,fy;
    fx=findx(x),fy=findx(y);
    if(fx!=fy)
    {
        bin[fx]=fy;
    }
}

int main()
{
    int n,r;
    while(cin>>n>>r)
    {
        if(r==0)
        {
            cout<<"Not Possible"<<endl;
            continue;
        }
        memset(du,0,sizeof(du));
        int i,j,k;
        for(i=0;i<n;i++)
            bin[i]=i;
        for(i=0;i<r;i++)
        {
            cin>>j>>k;
            du[j]++;
            du[k]++;
            mergex(j,k);
        }
        int tag=1,t=0;
        for(i=0;i<n;i++)
            if(bin[i]==i) t++;
        for(i=0;i<n;i++)
        {
            if((du[i])%2!=0)
            {
                tag=0;
                break;
            }
        }
        if(tag&&t==1) cout<<"Possible"<<endl;
        else cout<<"Not Possible"<<endl;
    }
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics