UVA10596 Morning Walk欧拉回路

Posted tigerisland45

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
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
2 2
0 1
1 0
2 1
0 1
Sample Output
Possible
Not Possible

问题链接UVA10596 Morning Walk
问题简述:(略)
问题分析
????给定结点数N(2<=N)和边(路)数R(0<=R<=10000),然后是R条边的起始和终止结点对。问能否从一个结点出发遍历所有的边。
????这是一个典型的欧拉回路问题。判定欧拉回路有2个条件,一是所有结点的出入度为偶数,另外一个是连通性,即所有有边的结点是连通的。需要注意的是,这个问题并不要求所有结点是连通的。
????这里给出2种解法,判定出入度是否为偶数的处理是相同的;连通性判定可以用DFS实现,也可以用并查集实现。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序(并查集)如下:

/* UVA10596 Morning Walk */

#include <bits/stdc++.h>

using namespace std;

const int N = 200;
int f[N], fcnt;

void UFInit(int n)
{
    for(int i = 0; i < n; i++)
        f[i] = i;
    fcnt = n;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    if (a != b) {
        f[a] = b;
        fcnt--;
    }
}

int degree[N];

int main()
{
    int n, m;
    while (scanf("%d%d", &n, &m) != EOF) {
        // 初始化并查集
        UFInit(n);

        // 变量初始化
        memset(degree, 0, sizeof(degree));

        // 统计各个结点的联通度,并构建并查集(为判定图是否为连通图)
        for(int i = 1; i <= m; i ++) {
            int u, v;
            scanf("%d%d", &u, &v);

            degree[u]++;
            degree[v]++;

            Union(u, v);
        }

        // 判定
        bool flag = true;
        if(m < 2)
            flag = false;
        else {
            for(int i = 0; i < n; i++)
                if(degree[i] & 1 /*degree[i] % 2 == 1*/) {
                    flag = false;
                    break;
                }
            if(flag) {
                int cnt = n;
                for(int i = 0; i < n; i++)
                    if(degree[i])
                        cnt--;
                if(fcnt - cnt != 1)
                    flag = false;
            }
        }
        // 输出结果
        printf(flag ? "Possible
" : "Not Possible
");
    }

    return 0;
}

AC的C++语言程序(DFS)如下:

/* UVA10596 Morning Walk */

#include <bits/stdc++.h>

using namespace std;

const int N = 200;
int degree[N], g[N][N], n, m;
bool vis[N];

void dfs(int u)
{
    vis[u] = true ;
    for(int v = 0 ; v < n ; v++)
        if(!vis[v] && g[u][v])
            dfs(v);
}

int main()
{
    while (scanf("%d%d", &n, &m) != EOF) {
        memset(g, 0, sizeof(g));
        memset(degree, 0, sizeof(degree));
        for ( int i = 0 ; i < m ; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            g[u][v] = g[v][u] = 1;
            degree[u]++;        // 统计出入度
            degree[v]++;
        }

        bool flag = false;
        for (int i = 0 ; i < n ; i++)   // 欧拉回路判定:所有结点出入度都为偶数
            if (degree[i] % 2 == 1 ) {
                flag = true;
                break;
            }

        if (flag || m < 2 )
            printf("Not Possible
");
        else {
            memset(vis, false, sizeof(vis));

            int i;
            for(i = 0; i < n; i++)
                if(degree[i])
                    break;
            dfs(i);

            flag = true;
            for (int i = 0 ; i < n ; i++)
                if(degree[i] && !vis[i]) {
                    flag = false;
                    break;
                }

            printf(flag ? "Possible
" : "Not Possible
");
        }
    }

    return 0;
}

以上是关于UVA10596 Morning Walk欧拉回路的主要内容,如果未能解决你的问题,请参考以下文章

uva-10596-欧拉回路

UVA 10054 - The Necklace(欧拉回路)

Uva10129(欧拉回路)

欧拉回路UVA - 10054 The Necklace

Play on Words UVA - 10129 (欧拉回路)

UVA-10054.The Necklace(欧拉回路)解题报告