华为oj中级 笔画

描述
一笔画游戏是一个数学游戏 即平面上由多条线段构成的一个图形能不能一笔画成,使得在每条线段上都不重复?例如汉字‘日’和‘中’字都可以一笔画的,而‘田’和‘目’则不能。

请编程实现一笔画:
首先输入坐标系上的点数个数,然后输入点的坐标,请判断这张图是否可以一笔画出,并输出画线顺序 (每条线段必须经过一次,且只能经过一次。每个端点可以经过多次。)
当有多种方式可以完成一笔画时,每一步都必须尽可能先画数值最小的端点。
如 输入 1,2 1,3 2,3 三个点
1.png

画线顺序为:1 2 3 1
如果能画线输出true
其他输出false;
知识点 链表,队列,栈,树,图,数组
运行时间限制 10M
内存限制 128
输入
1、输入点的个数
2、输入点的坐标
输出
如果可以一笔画这输出 画线顺序

样例输入 3 1 2 1 3 2 3
样例输出 true

无向图是否具有欧拉通路或回路的判定:

欧拉通路:图连通;图中只有0个或2个度为奇数的节点

欧拉回路:图连通;图中所有节点度均为偶数

http://blog.csdn.net/dellaserss/article/details/7724401

1.判断整幅图是不是联通的
2.判断这个这个连通图是否是欧拉回路
对于第一个问题,并查集就是为解决它而生的,我靠容我再次激动一下,我心心念念的并查集终于有用武之地了。下面给出我看了五遍以上的一个大神的博客
http://blog.csdn.net/dellaserss/article/details/7724401/
如果一遍看不懂,请多看几遍
对于第二个问题,就是需要判断所有点,度数为奇数的点的个数,只能是0个或者是2个

#include<iostream>  

#define Len 500  
using namespace std;  

int pre[Len];  

void Init()  
{  
    for (int i = 0; i<Len; i++)  
    {  
        pre[i] = i;  
    }  
}  
int Find(int n)  
{  
    int r = n;  
    while (pre[r] != r)  
    {  
        r = pre[r] ;  
    }  

    //压缩路径  
    int j = n;  
    int k;  

    while (pre[j] != r)  
    {  
        k = pre[j];  
        pre[j] = r;  
        j = k;  
    }  

    return r;  
}  

void Union(int x, int y)  
{  
    int rx = Find(x);  
    int ry = Find(y);  
    if (rx != ry)  
    {  
        pre[ry] = rx;  
    }  
}  

int main()  
{  
    int num, count, oddPoint, max;  
    int x, y, i;  
    int edge[Len];  

    while (cin >> num)  
    {  
        Init();  
        count = 0;  
        oddPoint = 0;  
        max = 0;  
        memset(edge, 0, sizeof(int)*Len);  

        for (i = 0; i < num; i++)  
        {  
            cin >> x >> y;  

            Union(x, y);  
            edge[x]++;  
            edge[y]++;  

            if (x>max)  
            {  
                max = x;  
            }  
            if (y>max)  
            {  
                max = y;  
            }  
        }  

        for (i = 1; i <= max; i++)  
        {  
            if (edge[i] > 0)  
            {  
                if (pre[i] == i)  
                {  
                    count++;  
                }  
                if (edge[i] % 2 == 1)  
                {  
                    oddPoint++;  
                }  
            }  

        }  


        if (count == 1 && (oddPoint == 0 || oddPoint == 2))  
        {  
            cout << "true" << endl;  
        }  
        else  
        {  
            cout << "false" << endl;  
        }  

    }  
    return 0;  
}  


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<unordered_map>
#include<fstream>
#include<sstream>
using namespace std;

#define maxn 101  

int g[maxn][maxn];
int du[maxn];
int circuit[maxn];
int n, e, circuitpos, i, j, x, y, start;

void find_circuit(int i)
{
    int j;
    for (j = 1; j <= n; j++)
    {
        if (g[i][j] == 1)
        {
            g[j][i] = g[i][j] = 0;
            find_circuit(j);
        }
    }

    circuit[++circuitpos] = i;
}

int main()
{
    cin >> n;

    while (cin >> x >> y)
    {
        g[y][x] = g[x][y] = 1;
        du[x]++;
        du[y]++;
    }

    start = 1;

    for (i = 1; i <= n; i++)
    if (du[i] % 2 == 1)
    {
        start = i;
        break;
    }

    circuitpos = 0;
    find_circuit(start);

    if (circuitpos == n + 1)
        cout << "true" << endl;
    else
        cout << "false" << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值