uva 10596 - Morning Walk

来源:互联网 发布:单例模式php 编辑:程序博客网 时间:2024/05/18 01:41

Kamal is a Motashota guy. He has got a new job in Chittagong. So, he has moved to Chittagong from Dinajpur. He was getting fatter in Dinajpur as he had no work in his hand there. So, moving to Chittagong 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 inChittagong and 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 Kamalin 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 by N (2 ≤ N ≤ 200). The road intersections are assumed to be numbered from 0 to N-1. The second number R denotes the number of roads (0 ≤ R ≤ 10000). Then there will be R lines each containing two numbers c1and c2 indicating 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



此题为欧拉回路题,且是无向的,因为并没有规定路的方向,那么按照刘汝佳那本入门经典里面的欧拉回路模版来判断是否所有道路都能被走过,同时判断每个点的出度与入度之和是否都为偶数(因为终点和起点是同一个点),具体解题如下:

#include<stdio.h>#include<stdlib.h>#include<string.h>int G[205][205],vis[205][205],n,degree[205];bool flag;void euler(int u){//从0出发拜访所有点     for(int v=0;v<n&&flag;v++)if(G[u][v]&&!vis[u][v])    {            if(degree[v]%2!=0)flag=false;        vis[u][v]=vis[v][u]=1;        euler(v);    }}int main(){    int r,c1,c2;    while(scanf("%d%d",&n,&r)==2){        if(r==0)        {  printf("Not Possible\n");continue;}         memset(G,0,sizeof(G));        memset(vis,0,sizeof(vis));        memset(degree,0,sizeof(degree));        for(int i=1;i<=r;i++){            scanf("%d%d",&c1,&c2);            degree[c1]++,degree[c2]++;             G[c1][c2]=1,G[c2][c1]=1;        }        flag=true;        euler(0);//以此为起点        for(int i=0;i<n&&flag;i++)//判断是否存在没有被             for(int j=0;j<n&&flag;j++)if(G[i][j]&&!vis[i][j])           {   flag=false;    }        if(flag)printf("Possible\n");        else printf("Not Possible\n");    }    return 0;} 


原创粉丝点击