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 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 beR lines each containing two numbersc1 andc2 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 |
Problemsetter: Muhammad Abul Hasan
International Islamic University Chittagong
首先道路要连通,一笔画,即所有点的度数为偶数(陷阱:当R小于2的时候是不可能的,R==0时是不可能的,虽然度数为偶数;R==1是由例子可知)
#include<stdio.h>
#include<string.h>
#define M 205
int n,r;
int de[M],fa[M];
int find(int x)
{
return x==fa[x]?fa[x]:(fa[x]=find(fa[x]));
}
int main()
{
while(~scanf("%d%d",&n,&r))
{
int ok=1;
for(int i=0;i<M;i++)fa[i]=i,de[i]=0;
for(int i=0;i<r;i++)
{
int u,v;
scanf("%d%d",&u,&v);
fa[find(u)]=find(v);
de[u]++;
de[v]++;
}
if(r<2||n==0)ok=0;
int root=find(0);
for(int i=0;i<n&&ok;i++)
if(de[i]!=0&&(find(i)!=root||de[i]%2))ok=0;
if(ok)puts("Possible");
else puts("Not Possible");
}
return 0;
}
/*
#include<stdio.h>
#include<string.h>
#define MM 20000
int N,M,a[MM];
main()
{
int i,N,R,u,v,p,q;
while(scanf("%d%d",&N,&R)!=EOF)
{
memset(a,0,sizeof(a));
for(i=0;i<R;i++)
{
scanf("%d%d",&u,&v);
a[u]++;
a[v]++;
}
if(R<2) {printf("Not Possible\n");continue;}
p=0;q=0;
for(i=0;i<N;i++)
if(a[i]%2==0)
p++;
else q++;
if(q==0&&p==N) printf("Possible\n");
else printf("Not Possible\n");
}
return 0;
}
*/