UVa10596 Morning Walk

来源:互联网 发布:创意中国设计大赛 知乎 编辑:程序博客网 时间:2024/05/21 02:19

UVa10596 Morning Walk


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
nput 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
2 2
0 1
1 0
2 1
0 1


Output for Sample Input
Possible
Not Possible


并查集+欧拉回路的判断

题意: 一个老头每天早上散步, 他想知道能不能走过所有的路, 回到出发点, 但不走重复的路。

思路:欧拉回路存在性判断。 连通、 度数都为偶数。度数判断就不说了。 连通性的判断用并查集,根节点最多只有一个的时候,图是连通的

题目陷阱: 切记m < 2的时候肯定是不能实现的, 要直接输出。


#include<iostream>#include <cstdio>#include <string.h> #define N 210using namespace std;int n, m, d[N], u[N], ran[N];void make(){    for(int i = 0; i < n; i++)    {        u[i] = i;        ran[i] = 1;    }}int find(int x){    if (x != u[x])    {        u[x] = find(u[x]);    }    return u[x];}void UNion(int x, int y){    x = find(x);    y = find(y);    if (x == y)        return ;    if (ran[x] > ran[y])    {        u[y] = x;    }    else    {        u[x] = y;        if (ran[x] ==  ran[y])        {            ran[y]++;        }    }}int main(){#ifndef ONLINE_JUDGE    freopen("1.txt", "r", stdin);#endif    int i, j, x, y;    while(~scanf("%d%d", &n, &m))    {        bool flag = true;        make();        memset(d, 0, sizeof(d));//      memset(out, 0, sizeof(out));        for (i = 0; i < m; i++)        {            cin >> x >> y;            UNion(x, y);            d[x]++;            d[y]++;        }        if (!m || n <= 1)        {            printf("Not Possible\n");            continue;        }        int root = find(0);        for(i = 0; i < n; i++)        {            if (d[i] != 0)            if (find(i) != root || d[i] % 2 != 0)            {                flag = false;                break;            }        }        if (flag)            printf("Possible\n");        else            printf("Not Possible\n");    }    return 0;}
0 0
原创粉丝点击