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
22 01 10 21 01
Sample Output
Possible
Not Possible
题意:
Kamal早上散步时,发现有很多条路,问每条路走一次能否回到原点
思路:
判断是否存在欧拉回路,两个条件:1.有边的点的度数为偶数 2.有边的点联通(用dfs)
#include<iostream>
#include<cstring>
using namespace std;
int m, n;
int vis[210];
int con[210][210];
int num[210];
int sum;
int point;
bool flag;
void initial()
{
memset(vis,0,sizeof(vis));
memset(con,0,sizeof(con));
memset(num,0,sizeof(num));
sum = 0;
point = 0;
flag = true;
}
void dfs(int k)
{
if(vis[k]==1)//若已经访问过
return;
vis[k]=1;
sum++;//计算联通的点的个数
for (int i = 0 ; i < m ;i++)
{
if (con[k][i])
{
dfs(i);
}
}
return;
}
int main () {
int k,j,c;
while (cin >> m >> n)
{
initial();
for(int i = 0; i < n ;i ++)
{
cin >> k >> j;
con[k][j]++;
con[j][k]++;//考虑特殊情况,自回路
num[k]++;//储存点的度数
num[j]++;
}
for (int i = 0 ;i < m; i++)
{
if (num[i])
{
c = i;
break;
}
}
for (int i = 0; i < m ; i++)
{
if(num[i])
point++;//计算有边的点的个数,即度数不为0的点的个数
if (num[i] % 2 != 0)//若度数为奇数,则不能构成联通图
{
flag = false;
break;
}
}
dfs(c);
if (sum!= point)
flag = false;
if(flag)
cout << "Possible"<<endl;
else
cout << "Not Possible"<<endl;
}
return 0;
}