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 inDinajpur 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 Rdenotes 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 2 0 1 1 0 2 1 0 1 | Possible Not Possible |
Problemsetter: Muhammad Abul Hasan
International Islamic University Chittagong
这道题一看觉得是挺简单的欧拉图问题,一想总觉得不对劲,果然卡了很久,对欧拉图一点都不熟悉,代码还参考了别人的,在这里写一下欧拉图的判断
有向图欧拉路或欧拉回路的判定方法:
(1)有向图G为欧拉图(存在欧拉回路),当且仅当G的基图连通,且所有顶点的入度等于出度。
(2)有向图G为半欧拉图(存在欧拉道路),当且仅当G的基图连通,且存在顶点u的入度比出度大1、v的入度比出度小1,其它所有顶点的入度等于出度。
无向图的欧拉图判定方法:
(1) 图连通。
(2) 所有的点的度数(出度和入度之和)为偶数。
#include<iostream>
#include<cstring>
using namespace std;
int du[210];
int bin[210];
int findx(int t)
{
int r=t;
while(bin[r]!=r)
{
r=bin[r];
}
return r;
}
void mergex(int x,int y)
{
int fx,fy;
fx=findx(x),fy=findx(y);
if(fx!=fy)
{
bin[fx]=fy;
}
}
int main()
{
int n,r;
while(cin>>n>>r)
{
if(r==0)
{
cout<<"Not Possible"<<endl;
continue;
}
memset(du,0,sizeof(du));
int i,j,k;
for(i=0;i<n;i++)
bin[i]=i;
for(i=0;i<r;i++)
{
cin>>j>>k;
du[j]++;
du[k]++;
mergex(j,k);
}
int tag=1,t=0;
for(i=0;i<n;i++)
if(bin[i]==i) t++;
for(i=0;i<n;i++)
{
if((du[i])%2!=0)
{
tag=0;
break;
}
}
if(tag&&t==1) cout<<"Possible"<<endl;
else cout<<"Not Possible"<<endl;
}
return 0;
}