Morning Walk UVA 10596

本文探讨了一道关于欧拉图的编程题,通过分析和给出代码实现,介绍了有向图和无向图中欧拉路径的判定方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值