zoukankan      html  css  js  c++  java
  • HDU 1878 欧拉回路

    欧拉回路

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8309    Accepted Submission(s): 2980


    Problem Description
    欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?
     
    Input
    测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
    束。
     
    Output
    每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
     
    Sample Input
    3 3
    1 2
    1 3
    2 3
    3 2
    1 2
    2 3
    0
     
    Sample Output
    1 0
     
    Author
    ZJU
     
    Source

    思路:简单欧拉回路,不需要输出欧拉回路,题目讲述的是无方向的图,只需要根据存在欧拉回路

    的条件判断即可

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <queue>
    using namespace std;
    int n,m;
    int flag;
    int a,b;
    int hash[1010],map[1010];
    void Merge(int a,int b)
    {
        int i = min(map[a],map[b]);
        int j = max(map[a],map[b]);
        for(int k = 1;k <= n;k ++)
           if(map[k] == j)
              map[k] = i;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m),n != 0 && m != 0)
        {
            flag = 1;
            memset(hash,0,sizeof(hash));
            for(int i = 1;i <= n;i ++)
                map[i] = i;
            while(m --)
            {
                scanf("%d%d",&a,&b);
                Merge(a,b);
                hash[a] ++;hash[b] ++;
            }
           for(int i = 1;i <= n;i ++)
               if(map[i] != 1 || hash[i] % 2 == 1 || hash[i] == 0)
                {
                    flag = 0;
                    break;
                }
           if(flag == 1)
               printf("1 ");
           else
               printf("0 ");
        }
        return 0;
    }

  • 相关阅读:
    解决 SQL Server Profiler 跟踪[不断]出现检索数据
    Linq表达式开窍
    CSS3——动画效果
    MongoDB学习与BUG解答
    MongoDB 客户端 MongoVue
    Memcached——分布式缓存
    WRONGTYPE Operation against a key holding the wrong kind of value
    Redis——分布式简单使用
    HTML5——播放器
    HTML5——行走日记
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3320637.html
Copyright © 2011-2022 走看看