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 无向图:
(1)欧拉路:有两个奇数度节点 其他都是偶数度的节点
(2)欧拉回路: 所有节点的度数都是偶数度的
2 有向图
(1)欧拉路 : 一个节点的出度-入度=1 ,另一个节点的入度-出度=1,其他节点的出度等于入度
(2)欧拉回路: 所有的节点的出度等于入度
#include <iostream>
#include <string.h>
using namespace std;
int a[300],b[300];
int main()
{
void f(int x,int y);
int i,j,n,m,s,t,k;
int x,y;
while(cin>>n>>m)
{
memset(a,0,sizeof(a));
for(i=0;i<=n-1;i++)
{
b[i]=i;
}
s=0;
for(i=0;i<=m-1;i++)
{
cin>>x>>y;
a[y]++; a[x]++;
f(x,y);
}
for(i=0;i<=n-1;i++)
{
if(b[i]==i)
{
s+=1;
}
if(s>1)
{
break;
}
}
if(s==1)
{
for(i=0;i<=n-1;i++)
{
if(a[i]%2!=0)
{
break;
}
}
if(i==n)
{
cout<<"Possible"<<endl;
}else
{
cout<<"Not Possible"<<endl;
}
}else
{
cout<<"Not Possible"<<endl;
}
}
return 0;
}
int find(int x)
{
int k1,k2;
k1=x;
while(k1!=b[k1])
{
k1=b[k1];
}
while(x!=b[x])
{
k2=b[x];
b[x]=k1;
x=k2;
}
return k1;
}
void f(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
{
b[x]=y;
}
}