[欧拉回路] hdu 3018 Ant Trip

[欧拉回路] hdu 3018 Ant Trip

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

全栈程序员社区此处内容已经被作者隐藏,请输入验证码查看内容
验证码:
请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“全栈程序员社区”或者“www_javaforall_cn”或者微信扫描右侧二维码都可以关注本站微信公众号。

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3018

Ant Trip

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




Problem Description
Ant Country consist of N towns.There are M roads connecting the towns.

Ant Tony,together with his friends,wants to go through every part of the country. 

They intend to visit every road , and every road must be visited for exact one time.However,it may be a mission impossible for only one group of people.So they are trying to divide all the people into several groups,and each may start at different town.Now tony wants to know what is the least groups of ants that needs to form to achieve their goal.



[欧拉回路] hdu 3018 Ant Trip
 


Input
Input contains multiple cases.Test cases are separated by several blank lines. Each test case starts with two integer N(1<=N<=100000),M(0<=M<=200000),indicating that there are N towns and M roads in Ant Country.Followed by M lines,each line contains two integers a,b,(1<=a,b<=N) indicating that there is a road connecting town a and town b.No two roads will be the same,and there is no road connecting the same town.
 


Output
For each test case ,output the least groups that needs to form to achieve their goal.
 


Sample Input
   
   
3 3 1 2 2 3 1 3 4 2 1 2 3 4

 


Sample Output
   
   
1 2
Hint
New ~~~ Notice: if there are no road connecting one town ,tony may forget about the town. In sample 1,tony and his friends just form one group,they can start at either town 1,2,or 3. In sample 2,tony and his friends must form two group.

 


Source
 


Recommend
gaojie   |   We have carefully selected several similar problems for you:  
3013 
3015 
3016 
3011 
3010 
 




Statistic | 
Submit | 
Discuss | 
Note

题目意思:

给一幅无向图,求要用多少次一笔画,把全部边走完,边仅仅能走一次。孤立点不算。

解题思路:
dfs把每一个连通块找到,然后统计奇数度数节点个数。

注意孤立节点不算。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

#define Maxn 110000
int de[Maxn],n,m;
vector<vector<int> >myv;
int in[Maxn],cnt;
bool vis[Maxn];

void dfs(int cur)
{
    in[++cnt]=cur;
    vis[cur]=true;
    for(int i=0;i<myv[cur].size();i++)
    {
        int ne=myv[cur][i];
        if(vis[ne])
            continue;
        dfs(ne);
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
   //freopen("out.txt","w",stdout);
   while(~scanf("%d%d",&n,&m))
   {
       myv.clear();
       myv.resize(n+10);
       memset(de,0,sizeof(de));

       for(int i=1;i<=m;i++)
       {
           int a,b;
           scanf("%d%d",&a,&b);
           myv[a].push_back(b);
           myv[b].push_back(a);
           de[a]++;
           de[b]++;
       }
       memset(vis,false,sizeof(vis));

       int ans=0;

       for(int i=1;i<=n;i++)
       {
           if(!vis[i])
           {
               cnt=0;
               dfs(i);
               int temp=0;
               if(cnt==1)  //孤立节点不算
                    continue;
               for(int j=1;j<=cnt;j++)
               {
                   if(de[in[j]]&1)
                        temp++;
                    //printf("i:%d j")
               }
               if(!temp)
                    ans++;
               else
                    ans+=temp/2;
           }
       }
       printf("%d\n",ans);

   }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117913.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql索引是什么 优点和缺点_MySQL索引优缺点、使用原则及种类介绍「建议收藏」

    mysql索引是什么 优点和缺点_MySQL索引优缺点、使用原则及种类介绍「建议收藏」一、索引简介1、索引简介索引(Index)是帮助MySQL高效获取数据的数据结构。在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的。MyISAM和InnoDB存储引擎只支持BTREE索引,MEMORY/HEAP存储引擎支持HASH和BTREE索引。2、索引的优点A、提高数据检索效率,降低数据库的IO成本。B、通过索引对数据进行排序,降低数据排序的成本降低了CPU的消…

    2022年5月26日
    42
  • 电脑表格制作步骤word_php入门案例

    电脑表格制作步骤word_php入门案例OFFICE办公软件零基础入门系列教程【WORD第四节】这是一个新开的一个系列教程,适合零基础的小白学习使用OFFICE办公软件。本教程以解决在使用OFFICE办公软件实用的问题为引导,从最基础的使用知识一点点的深入,最后到熟练使用OFFICE办公软件。本教程会分为三个专题,【WORD篇】【EXCE篇】【PPT篇】。表格是Word文档中一个比较重要的存在,有很多的不太会使用,下面我们就详细讲解…

    2022年8月29日
    2
  • visual C++ 项目和解决方案的区别

    项目:项目是构成某个程序的全部组件的容器,该程序可能是控制台程序、基于窗口的程序或某种别的程序。程序通常由一个或多个包含用户代码的源文件,可能还要加上包含其它辅助数据的文件组成。某个项目的所有文件都

    2021年12月24日
    50
  • 一键生成H5_h5生成器

    一键生成H5_h5生成器H5模板代码一键生成器2021-01技术交流或商业合作:QQ(591033633)微信(15858293899)演示地址见文中目录1、功能介绍22、准备工作53、演示地址及步骤53.1、后台数据接口图形化定义63.2、后台数据接口线上调试63.3、前端页面模板定义73.4、生成页面demo解析11功能介绍笔者作为多年的资深开发,深知调试前端UI是后端开发人员最惧怕的事,并且厌倦了无止境的Ctrl+C和Ctrl+V工作…

    2025年9月24日
    4
  • java数组删除元素_java中删除 数组中的指定元素方法[通俗易懂]

    java数组删除元素_java中删除 数组中的指定元素方法[通俗易懂]java中删除数组中的指定元素要如何来实现呢,如果各位对于这个算法不是很清楚可以和小编一起来看一篇关于java中删除数组中的指定元素的例子。java的api中,并没有提供删除数组中元素的方法。虽然数组是一个对象,不过并没有提供add()、remove()或查找元素的方法。这就是为什么类似ArrayList和HashSet受欢迎的原因。不过,我们要感谢ApacheCommonsUtils,我…

    2022年8月11日
    12
  • ETL开发工具KETTLE使用教程「建议收藏」

    ETL开发工具KETTLE使用教程「建议收藏」Kettle的建立数据库连接、使用kettle进行简单的全量对比插入更新:kettle会自动对比用户设置的对比字段,若目标表不存在该字段,则新插入该条记录。若存在,则更新。Kettle简介:Kettle是一款国外开源的ETL工具,纯java编写,可以在Window、Linux、Unix上运行,数据抽取高效稳定。Kettle中文名称叫水壶,该项目的主程序员MATT希望把各种数据放到一个…

    2022年6月3日
    408

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号