UVA 10196 Morning Walk(欧拉回路)

本文解析了一道关于晨间漫步的算法题,探讨如何在一个城市中遍历所有道路仅一次的可能性。通过记录每个交叉点的度数并使用并查集判断是否形成连通图来解决问题。

Problem H

Morning Walk

Time Limit

3 Seconds

Kamalis a Motashotaguy. He has got a new job in Chittagong. So, he has moved to Chittagong fromDinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving toChittagong has turned to be a blessing for him. Every morning he takes a walk through the hilly roads of charming city Chittagong. He is enjoying this city very much. There are so many roads in Chittagongand 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 help Kamal in 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 from0 to N-1. The second numberR denotes the number of roads (0 ≤ R ≤ 10000). Then there will beRlines each containing two numbersc1 andc2indicating the intersections connecting a road.

Output

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

Sample Input

Output for Sample Input

2 2

0 1

1 0

2 1

0 1

Possible

Not Possible


WA了一个上午。最终找出错误了,visit all the roads of the city in a single walk.走全然部的边且每条边仅仅走一次,点能够不走完,一直WA在这里。

题意:给出N个点和R条边,问能不能走全然部的边且每条边仅仅走一次,点能够不走完。

deg记录每一个点的度,并查集推断全部的边和这些边连接的点是不是一个连通图。

#include<stdio.h>
#include<string.h>
int father[205], deg[205];

int Find(int x)
{
    if(x != father[x])
        father[x] = Find(father[x]);
    return father[x];
}

void Init(int n)
{
    memset(deg, 0, sizeof(deg));
    for(int i = 0; i < n; i++)
        father[i] = i;
}

int main()
{
    int n, r,i;
    while(scanf("%d%d",&n,&r)!=EOF)
    {
        if(r == 0)
        {
            printf("Not Possible\n");
            continue;
        }
        Init(n);
        int u, v;
        for(i = 0; i < r ; i++)
        {
            scanf("%d%d",&u,&v);
            father[Find(u)] = Find(v);
            deg[u]++;
            deg[v]++;
        }
        int ok = 1;
        int j = 0;
        while(deg[j] == 0)
            j++;
        for(i = j + 1; i < n; i++)
            if(deg[i] && Find(i) != Find(j))
            {
                ok = 0;
                break;
            }
        int cnt = 0;
        for(i = 0; i < n; i++)
            if(deg[i] % 2 != 0)
                cnt++;
        if(!ok || cnt > 0)
            printf("Not Possible\n");
        else
            printf("Possible\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值