Ant Trip

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

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.

 
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
 

题意:每条边过且只过一次,问至少要画几笔才能全部边都经过。。孤立的点忽视。

分析:首先,你用笔来画的话,只可能有2种,一:每回路,a——>b  二:形成回路,a——>...——>a

对于图中的每一块,度数数为奇数的点必须是由第一种画出来的,所以奇数/2就是画的笔数

由两种结合而成的图,也只是奇数/2

特别的,如果图只有第二种的话,即该块中不存在奇数点,则只要画一笔

对于整副图(每一块块组合而成),等于 :第一块奇数点/2+第二块奇数点/2+.......,最后得,图的总奇数点/2

接着还要计算有多少块里不存在奇数点(不存在奇数点的那块中,一定没有第一种画法,只需要画一笔),累加起来就得到答案了。。

(如果是个欧拉回路一笔就可以完成,如果是个其它连通集,要根据这个集合的奇度数而定,笔划数=奇度数/2,用并查集来判断有多少个连通集,然后用vector来存这些连通集,通过判断度数是奇偶性来确定是否为欧拉回路;总之笔划数 = 奇度数/2 + 欧拉回路数;)

  1. /* 一笔画问题: 每条边过且只过一次,问至少要画几笔才能全部边都经过(不考虑鼓励的点)
  2. 图有多个集合构成  集合有两种
  3. 一种为含奇数点的  一种为只含偶数点的
  4. 对于含奇数点的     笔画数=奇数点个数/2
  5. 对于只含偶数点的   存在欧拉回路  笔画数=1
  6. 对于整张图  则  ans=总奇数点/2+只含偶数点的集合个数
  7. */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> using namespace std; const int N=; int n,m;
int father[N],deg[N],odd[N],vis[N];
vector<int> vt; void makeSet(){
for(int i=;i<=n;i++)
father[i]=i;
} int findSet(int x){
if(x!=father[x]){
father[x]=findSet(father[x]);
}
return father[x];
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
vt.clear();
memset(vis,,sizeof(vis));
memset(deg,,sizeof(deg));
memset(odd,,sizeof(odd));
makeSet();
int x,y;
while(m--){
scanf("%d%d",&x,&y);
deg[x]++;
deg[y]++;
int fx=findSet(x);
int fy=findSet(y);
if(fx!=fy)
father[fx]=fy;
}
for(int i=;i<=n;i++){
int fi=findSet(i);
if(!vis[fi]){
vt.push_back(fi); //vt保存着集合,集合就是图
vis[fi]=;
}
if(deg[i]%==)
odd[fi]++; //保存这个集合的奇数度数的个数
}
int ans=;
for(int i=;i<(int)vt.size();i++){
int tmp=vt[i];
if(deg[tmp]==) //孤立点
continue;
if(odd[tmp]==) //该集合是欧拉回路,有一条路
ans++;
else
ans+=odd[tmp]/;
}
printf("%d\n",ans);
}
return ;
}

HDU 3018 Ant Trip (欧拉回路)的更多相关文章

  1. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

  2. [欧拉回路] hdu 3018 Ant Trip

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...

  3. HDU 3018 Ant Trip(欧拉回路,要几笔)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  5. HDU 3018 Ant Trip

    九野的博客,转载请注明出处:  http://blog.csdn.net/acmmmm/article/details/10858065 题意:n个点m条边的无向图,求用几笔可以把所有边画完(画过的边 ...

  6. HDU3018:Ant Trip(欧拉回路)

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. HDU 3108 Ant Trip

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...

  9. HDU 3018 欧拉回路

    HDU - 3018 Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together ...

随机推荐

  1. css文件放在根目录之后不起作用原因

    修改为

  2. java之类的初始化

    概述 在java中,一个类能够包括的元素有变量,方法,代码块.这当中变量能够分为普通变量和静态变量,代码块也有静态代码块和普通代码块.在创建一个对象的时候,这个对象是怎么初始化的呢.这里我们就開始来解 ...

  3. GLFW_KEY_KP_ADD和GLFW_KEY_KP_SUBTRACT

      这两个键的代码分别为: GLFW_KEY_KP_ADD(334) GLFW_KEY_KP_SUBTRACT(333)   对应的是键盘右侧数字面板上的+ -键.

  4. OpenGL ES 3.0(五)

    使用EGL(在iOS中是EAGL)创建屏幕渲染 加载顶点和片段着色器 创建程序对象,连接顶点和片段着色器,连接程序对象 设置视口 清除颜色缓冲区 绘制一个简单的图元(三角形) 显示缓冲区内容 1.创建 ...

  5. git Alias 设置

    git Alias 设置 Git 使用比較多的话能够设置一些命令的 Alias ,简单的说就是用简写取代整个完整的命令. 如co 代表 checkout. Mac下,到根文件夹 cd ~ 然后 vi ...

  6. Elasticsearch - 理解字段分析过程(_analyze与_explain)

    我们经常会遇到问题.为什么指定的文档没有被搜索到.许多情况下, 这都归因于映射的定义和分析例程配置存在问题. 针对分析过程的调试,ElasticSearch提供了专用的REST API. _analy ...

  7. Skyline开发2-第一个程序

    来试试Skyline的Hello World.使用的工具是VS2017+Skyline6.5 加载组件 在工具箱右键新建skyline选项卡,在skyline选项卡上右键选择项,在弹出的"选 ...

  8. Redis 操作数据

    展现最新数据 Web应用常常要展现最新数据,就会根据时间对数据排序: SELECT * FROM foo WHERE ... ORDER BY time DESC LIMIT 10 随着数据的增加,问 ...

  9. 【自动化测试】基于IntelliJ IDEA的Gradle和testNG

    这几篇文章值得一读: TestNG测试框架使用笔记:http://www.cnblogs.com/xguo/p/3300358.html TestNg官方文档:http://testng.org/do ...

  10. Ant详解之-path、classpath和fileset

    转自:http://www.cnblogs.com/itech/archive/2011/11/01/2231206.html 一 .<path/> 和 <classpath/> ...