Problem H | Morning Walk |
Time Limit | 3 Seconds |
Kamal is aMotashota guy. He has got a new job in Chittagong. So, he has moved to Chittagong fromDinajpur. 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 helpKamal 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 number R denotes the number of roads (0 ≤ R ≤ 10000). Then there will beR lines each containing two numbers c1 andc2 indicating the intersections connecting a road.
Output
Print a single line containing the text “Possible” without quotes if it is possible forKamal 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 UniversityChittagong
灰常蛋疼的一个题目,看他的样例像是有向图,看别人的解题报告都说是无向图......
无向图的欧拉回路问题,条件为连通图且所有节点度数为偶数
#include <stdio.h>
#define size 210
int num,f,n,visit[size],map[size][size],degree[size];
int dfs(int x)
{int i;
for (i=0;i<n;i++)
if ((visit[i]==1)&&(map[x][i]==1))
{visit[i]=0; ++num;
dfs(i);
}
};
int main()
{int i,j,r,x,y;
while (scanf("%d%d",&n,&r)!=EOF)
{
for (i=0;i<n;i++)
for (j=0;j<n;j++)
map[i][j]=0;
for (i=0;i<n;i++)
{visit[i]=1; degree[i]=0;}
for (i=1;i<=r;i++)
{scanf("%d%d",&x,&y);
map[x][y]=1;
++degree[x];
++degree[y];
}
if (r>0) f=1; else f=0;
for (i=0;i<n;i++)
if (degree[i]%2==1) {f=0;break;}
if (f) {num=1; visit[0]=0; dfs(0); //开始写的visit[n-1];dfs(n-1);死活过不了,改成0就过了,题目也没说0是起点,判断连通性而已。真奇怪.....
if (num!=n) f=0;
}
if (f) printf("Possible\n");
else printf("Not Possible\n");
}
return 0;
}