UVa 10596:Morning Walk, 赤裸裸的欧拉回路

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1537

题目类型: 欧拉回路, 并查集, dfs

题目:

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 Chittagongand 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.

题目大意:

Kamal每天早上都要从家里走到Chittagongand这个地方。 从家里到Chittagongand有很多条路, 他每天早上都要先选择好一条路线, 这条路线从家里走到Chittagongand,在从Chittagongand走回家里。 这条路线不能重复地经过同一条路。 两个地点间可能会有多条路。 比如多次出现了从A到B的路线,  那么表示每次出现的都是不同的路

分析与总结:

这题就是赤裸裸的欧拉回路。UVa 10054 - The Necklace 和  UVa 10129 - Play on Words    这两篇讲得较详细。 不再累赘

1. 欧拉回路+DFS

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
int vis[210],N, M, G[210][210], inDegree[210], outDegree[210];  

void dfs(int v)         //深度优先遍历
{
    vis[v]=true;
    for(int i=0;i<N;i++)
    {
        if(!vis[i] && G[v][i])
        {
            dfs(i);
        }
    }
}    

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    while(~scanf("%d %d",&N, &M)){
        memset(G, 0, sizeof(G));
        int a,b;
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<M; ++i){
            scanf("%d %d",&a,&b);
            G[a][b] = G[b][a] = 1;
            ++vis[a];
            ++vis[b];
        }  

        int cnt = 0;
        for(int i=0; i<N; ++i){
            if(vis[i]%2==1){
                ++cnt;
                break;
            }
        }
        memset(vis, 0, sizeof(vis));
        if(cnt || M<2) printf("Not Possible\n");
        else{
            dfs(0);
            bool flag = true;
            for(int i=0; i<N; ++i) {
                if(!vis[i]) flag = false;
            }
            if(flag)printf("Possible\n");
            else printf("Not Possible\n");
        }  

    }
    return 0;
}

2,欧拉回路+并查集

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
int vis[210],N, M, G[210][210], inDegree[210], outDegree[210], f[210];  

void init()   //并查集判断是否是一个连通图
{
    int i;
    for(i=0;i<N;i++)
        f[i]=i;
}
int find(int x)
{
    int r=x;
    while(f[r]!=r)
    r=f[r];
    f[x]=r;
    return r;
}
void Union(int x,int y)
{
    int fx,fy;
    fx=find(x);
    fy=find(y);
    if(fx!=fy)
    f[fx]=fy;
}    

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    while(~scanf("%d %d",&N, &M) && N){
        memset(G, 0, sizeof(G));
        int a,b;
        init();
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<M; ++i){
            scanf("%d %d",&a,&b);
            ++G[a][b];
            ++G[b][a];
            ++vis[a];
            ++vis[b];
            Union(a,b);
        }  

        int ans=0;
        for(int i=0; i<N; ++i) if(f[i]==i) ++ans;  

        if(ans==1 && M>=2){
            int cnt = 0;
            for(int i=0; i<N; ++i){
                if(vis[i]%2==1){
                    ++cnt;
                    break;
                }
            }
            if(cnt) printf("Not Possible\n");
            else printf("Possible\n");
        }
        else
            printf("Not Possible\n");
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索include
, 题目
, 路线
, walk
he
欧拉回路、欧拉回路算法、求欧拉回路、欧拉通路和欧拉回路、混合图欧拉回路,以便于您获取更多的相关知识。

时间: 2024-10-30 03:15:51

UVa 10596:Morning Walk, 赤裸裸的欧拉回路的相关文章

uva 10596 - Morning Walk

点击打开链接 题目意思:给定n个点,判断由某一点出发最后能否回到原点 解题思路:比较简单的欧拉回路的应用,根据欧拉回路的性质,所有节点的度数(入度加出度)都是偶数,我么只须要开一个road数组存储每一个节点的度数,最后遍历数组判断是不是全部都是偶数即可   代码: //简单的欧拉回路的应用 #include <iostream> #include <cstdio> #include <cstring> #include <queue> using names

UVa 10054:The Necklace, 欧拉回路+打印路径

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=995 题目类型: 欧拉回路 题目: My little sister had a beautiful necklace made of colorful beads. Two successive beads in the necklace sha

UVa 10720:Graph Construction

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1661 原题: Graph is a collection of edges E and vertices V. Graph has a wide variety of applications in computer. There are diffe

UVa 10714:Ants

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1655 原题: An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant rea

UVa 10041:Vito&#039;s Family

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=982 [原题] Background The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them

UVa 10034:Freckles (最小生成树模板题)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=975 题目: Problem A: Freckles In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to fo

UVa 10986:Sending email (Dijkstra优化, SPFA)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1927 题目: Problem E Sending email Time Limit: 3 seconds "A new internet watchdog is creating a stir in Springfield. Mr. X, i

UVa 10129:Play on Words, 欧拉道路

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1070 题目类型: 欧拉道路 题目: Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve

UVa 167:The Sultan&#039;s Successors, 八皇后问题

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=103 题目类型: 回溯 原题: The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate