Ant Trip

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

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   |   We have carefully selected several similar
problems for you:  3013 3015 3016 3011 3010 
 
  题目大意:其实题目意思就是给你一幅图,问最少用多少笔可以把整幅图划完,每条边只能经过一次,孤立点忽略不计。这个就是欧拉回路的问题,判断划几笔要看入度和出度的值,还有入度-出度的值,判断有多少个欧拉路。用并查集来判断连通性,度数判断欧拉回路。
 

题意:给一个无向图,N个顶点和M条边,问至少需要几笔才能把所有边画一遍

思路:只要知道一个结论就随便做了。

 如果该连通分量是一个孤立的点,显然只需要0笔.

         如果该连通分量是一个欧拉图或半欧拉图,只需要1笔.

         非(半)欧拉图需要的笔数==该图中奇数度的点数目/2

对于每个以i为根的连通分量我们记录属于该连通分量的点数目num[i]和该连通分量中奇度点的个数odd[i]。

详见代码注释:

 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
int a[];
int pra[];
int rak[];
int num[];//属于该连通分量的点数目
int odd[];//该连通分量中奇度点
int find(int x)
{
if(pra[x]==x) return x;
else return pra[x]=find(pra[x]);
}
void unite(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx==yy) return;
else
{
if(rak[xx]<rak[yy]) pra[xx]=yy;
else
{
pra[yy]=xx;
if(rak[xx]==rak[yy]) rak[xx]++;
}
} }
int main()
{
int n,m;
while(cin>>n&&n)
{
cin>>m;
memset(a,,sizeof(a));
memset(num,,sizeof(num));
memset(odd,,sizeof(odd));
for(int i=;i<=n;i++)
{
pra[i]=i;
rak[i]=;
}
for(int i=;i<=m;i++)
{
int x,y;
cin>>x>>y;
a[x]++;//度数+1
a[y]++;
unite(x,y);//合并
}
int ans=;
for(int i=;i<=n;i++)
{
num[find(i)]++;//属于该连通分量的点数目
if(a[i]%==)
odd[find(i)]++;//该连通分量中奇度点
}
for(int i=;i<=n;i++)
{
if (num[i]<=) //只有一个点,不用走
continue;
else if (odd[i]==) //没有奇度数点,那就+1
ans++;
else //有的话
ans+=odd[i]/; //+奇度数点/2
}
cout<<ans<<endl;
}
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. NumPy线性代数

    NumPy - 线性代数 NumPy 包包含numpy.linalg模块,提供线性代数所需的所有功能. 此模块中的一些重要功能如下表所述. 序号 函数及描述 1. dot 两个数组的点积 2. vdo ...

  2. http客户端-基于boost开发

    http客户端-基于boost开发 基于BOOST编写的http客户端,作为BOOST开发学习之用.目前支持功能: http协议,单向链接返回http response code 200 可conte ...

  3. activity启动模式之standard

    activity启动模式之standard 一.简介 这种模式是默认的,不用我们自己设定 就像一只叠加在栈中 如果退出,就一个个退出,其实就是我们自己用手机的那种感受 二.代码实例 activityL ...

  4. 找出此产品描述中包含N个关键字的长度最短的子串

    阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...

  5. python学习笔记(pip下载安装)

    python有很多扩展模块需要安装 这个时候万能的pip就可以提供帮助 首页进入官网下载压缩包: https://pypi.python.org/pypi/pip#downloads 解压文件 cmd ...

  6. java多线程补:充原子性和可见性

    参考:http://www.cnblogs.com/mengyan/archive/2012/08/22/2651575.html 原子性:所谓原子性就是不可分割的,比如:在我们编程中直接给变量赋值, ...

  7. struts转换器

    struts转换器:在B/S应用中,将字符串请求参数转换为相应的数据类型,是MVC框架提供的功能,而Struts2是很好的MVC框架实现者,理所当然,提供了类型转换机制. 一.类型转换的意义 对于一个 ...

  8. Codeforces Round #394 (Div. 2) B. Dasha and friends

    B. Dasha and friends time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...

  9. DRF 的 版本,解析器,与序列化

    DRF 的 版本,解析器,与序列化 补充 配置文件中的 类的调用: (字符串) v1 = ["view.xx.apth.Role","view.xx.apth.Role& ...

  10. c/s和b/s的区别

    一.C/S 架构 1. 概念 C/S 架构是一种典型的两层架构,其全程是Client/Server,即客户端服务器端架构,其客户端包含一个或多个在用户的电脑上运行的程序,而服务器端有两种,一种是数据库 ...