1. 程式人生 > >UVA 10196 Morning Walk(歐拉回路)

UVA 10196 Morning Walk(歐拉回路)

ble move eve man first pre intersect sum ons

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



UVA 10196 Morning Walk(歐拉回路)