2012年10月16日 星期二

uva 10596 - Morning Walk



Problem H
Morning Walk
Time Limit
3 Seconds

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 in Chittagong 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 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 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 c1 and 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
0 1
1 0
2 1
0 1
Possible
Not Possible

Problemsetter: Muhammad Abul Hasan
International Islamic University Chittagong

euler graph check, notice weird requirement: M=0 -> not possible
#include<stdio.h>
#include<cstring>
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define DBG 0
#define MAXN 200
using namespace std;

int N,M,D;
int cnt[MAXN],adx[MAXN][MAXN];
bool trav[MAXN];
void DFS(int a){
if(DBG)printf("dfs %d\n",a);
trav[a]=1,D++;
rep(i,cnt[a]){
if(!trav[adx[a][i]])DFS(adx[a][i]);
}
}
void ans(){
int i,c=0;
for(i=0;i<N;i++){
if(cnt[i]%2){printf("Not Possible\n");return;}
if(cnt[i])c++;
}
memset(trav,0,sizeof(trav)),D=0;
rep(i,N)if(cnt[i]){DFS(i);break;}
if(D<c)printf("Not Possible\n");
else printf("Possible\n");
}
int main(){
int a,b;
while(scanf("%d%d",&N,&M)==2){
memset(cnt,0,sizeof(cnt));
rep(i,M)scanf("%d%d",&a,&b),adx[a][cnt[a]++]=b,adx[b][cnt[b]++]=a;
if(!M)printf("Not Possible\n"); //weird judge
else ans();
}
}

沒有留言:

張貼留言