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. JNI_Z_01_获取Clazz

    1. 为了能够在C/C++中使用Java类,jni.h头文件中专门定义了jclass类型来表示Java中的Class类(ZC: 就是Clazz) 2. 2.1.JNIEXPORT void JNICA ...

  2. 全方位解读Java反射(reflection)

    JAVA提供了一种反射机制,反射也称为反省. java程序运行以后内存中就是一堆对象,除了对象什么都没有. 找对象 拉关系 瞎折腾 对象在运行过程中能否有一种机制查看自身的状态,属性和行为.这就是反射 ...

  3. c++中的函数对象《未完成》

    头文件: #pragma once #include<iostream> #include<vector> using namespace std; class Student ...

  4. hive 导出数据到本地

    有时候需要将hive库中的部分数据导入至本地,这样子做可视化和小规模的数据挖掘实验都是比较方便的.数据导入至本地的HQL语法如下: INSERT OVERWRITE [LOCAL] DIRECTORY ...

  5. BZOJ 3931 [CQOI2015]网络吞吐量:最大流【拆点】

    传送门 题意 给你一个 $ n $ 个点,$ m $ 条边的无向网络,每条边有长度.每个点的流量限制为 $ c[i] $ . 要求流量只能经过从 $ 1 $ 的 $ n $ 的最短路.问你最大流是多少 ...

  6. NLP(二)_汉语言分词技术-最大匹配法

    前述 词是自然语言中最小的有意义的构成单位.汉语文本是基于单字的文本,汉语的书面表达方式以汉字作为最小单元,词与词之间没有明显的界限标志,因此,分词是汉语文本分析处理中首先要解决的问题之一. 分词可能 ...

  7. Ti CC2540蓝牙模块学习笔记整理

    接触CC2540几天,终于有了初步的理解,现将笔记整理如下,只是皮毛,如有错误,还请指正,还有好多没闹明白的地方,以后应该还会继续向里面更新~ 一.整体 1.TI的蓝牙平台支持2种协议栈/应用配置:单 ...

  8. New Concept English three (32)

    26w/m 68 The salvage operation had been a complete failure. The small ship, Elkor, which had been se ...

  9. 一个css3 DNA 效果

    这个效果就是 不断沿 Y 轴旋转 <div id="container"></div> <style> body{ background:bla ...

  10. SpringMVC札集(01)——SpringMVC入门完整详细示例(上)

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...