UVA Morning Walk(判欧拉回路)

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

并查集做法:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>

using namespace std;

#define M 210

int num[M];

int findx(int x)
{
    int r = x;
    while(r!=num[r])
    {
        r = num[r];
    }
    int k = x,j;
    while(k!=r)
    {
        j = num[k];
        num[k] = r;
        k = j;
    }
    return r;
}

void bing(int x,int y)
{
    int fx = findx(x);
    int fy = findx(y);
    if(fx!=fy)
    {
        num[fx] = fy;
    }

}

int main()
{
    int t, k = 1;
    int v[M];
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for (int i = 0; i < n; i++)
        {
            num[i] = i;
            v[i] = 0;
        }

        for (int i = 0; i < m; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            v[a]++;
            v[b]++;
            bing(a,b);
        }
        int cnt = 0;
        for (int i = 0; i < n; i++)
        {
            if (num[i] == i && v[i]!=0)
            {
                cnt++;
            }
        }
        int sum = 0;
        if(cnt == 1 && m>=2)
        {
            for(int i=0; i<n; i++)
            {
                if(v[i]%2!=0)
                {
                    sum++;
                    //printf("i = %d\n",i);
                    break;
                }
            }
            if (sum > 0)
            {
                printf("Not Possible\n");
            }
            else
            {
                printf("Possible\n");
            }
        }
        else
        {
            printf("Not Possible\n");
        }

    }
    return 0;
}

DFS做法:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int v[210];
int map[210][210];
int num[210];
int n,m;

void DFS(int x)
{
    v[x] = 1;
    for(int i=0;i<n;i++)
    {
        if(v[i] == 0 && map[x][i] == 1)
        {
            DFS(i);
        }
    }
}

int findx()
{
    int count = 0;
    for(int i=0;i<n;i++)
    {
        if(num[i]!=0 && v[i] == 0)
        {
            DFS(i);
            count++;
        }
        if(count > 1)
        {
            return 0;
        }
    }
    return 1;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            v[i] = 0;
            num[i] = 0;
            for(int j=0;j<n;j++)
            {
                map[i][j] = 0;
            }
        }
        int px,py;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&px,&py);
            map[px][py] = 1;
            map[py][px] = 1;
            num[px]++;
            num[py]++;
        }
        int sum = 0;
        for(int i=0;i<n;i++)
        {
            if(num[i]%2!=0)
            {
                sum++;
                break;
            }
        }
        if(sum>0 || m<2)
        {
            printf("Not Possible\n");
        }
        else
        {
            int k = findx();
            if(k == 1)
            {
                printf("Possible\n");
            }
            else
            {
                printf("Not Possible\n");
            }
        }
    }
    return 0;
}
时间: 2024-07-29 06:19:47

UVA Morning Walk(判欧拉回路)的相关文章

UVA - 10917 Walk Through the Forest (最短路+DP)

题意:Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比所有从A出发回家的路径都短,你的任务是计算有多少条不同的路径 从后往前找最短路, 对于每一步要更新之后走的位置值: #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> using namespace s

uva 10917 Walk Through the Forest(最短路)

uva 10917 Walk Through the Forest gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共有多少条不同的回家路径.其中起点的编号为1,终点的编号为2. Input 多组数据输入,每组数据第一行输入n,m(1<=n<=1000)表示点的数目和边的数目,点的编号为1~n,接下来m行每行输入3个数a,b,c表示有一条双向道路连接a,

uva 10054 The Necklace 欧拉回路

// uva 10054 The Necklace 欧拉回路 // 以颜色为节点,两种颜色互相连着有向边,然后跑一边欧拉回路就ok了 // 这题套了余老师模板书上的欧拉回路,然后就过了 // // 通过这题我了解到了,欧拉回路的基本思想 // 哎,继续练吧... #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat>

UVA 10917 - Walk Through the Forest(dijstra)

UVA 10917 - Walk Through the Forest 题目链接 题意:公司编号为1,家编号为2,每次回家都不走回头路,回头路定义为:满足条件的道路(A,B),满足存在一条从B出发比所有从A出发的回家的路径都短,问有几种走法 思路:先从家求dijstra,这样满足条件的道路就是d[A] > d[B],这个图是一个dag,在上面进行dp就可以找出种数了 代码: #include <cstdio> #include <cstring> #include <v

uva 10596 Morning Walk (欧拉回路)

uva 10596 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 ble

UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)

UVA - 10596 Morning Walk Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chittagong . So, he has moved to Ch

UVA 10196 Morning Walk(欧拉回路)

Problem H Morning Walk Time Limit 3 Seconds Kamalis a Motashotaguy. 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 toChittagong has tu

UVa 10596 Moring Walk【欧拉回路】

题意:给出n个点,m条路,问能否走完m条路. 自己做的时候= =三下两下用并查集做了交,WA了一发-后来又WA了好几发--(而且也是判断了连通性的啊) 搜了题解= = 发现是这样的: 因为只要求走完所有的路,即为只需要走完已经给出的路,而并没有要求所走得路上含有所有的点, 比如说 给出的路有这些 0 1 1 2 2 3 3 0 4 4 那么构成的路即为,绕着图中的蓝色线走一圈,即为走完了所有的路, 而4是一个孤立点,也并没有构成路,所以不需要管它 代码中的 if(d[i]!=0)是判断这个点是否

判欧拉回路或求一个图中欧拉图的个数

判欧拉图两个条件首先联通,其次度全部为欧度.那么就很easy了. 题目:hdoj1878 求一个图中欧拉图的个数. 首先通过连通性求出各个子图,然后求子图中奇数度的个数cnt,cnt/2为欧拉图的个数.若子图没有奇数度,则为一个欧拉回路. 题目:hdoj3018Ant Trip 注意这个题目中可能出现孤立点,不算入欧拉图中. AC代码: include include include include include include include include include include