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. mysql数据库优化课程---9、php用什么写的

    mysql数据库优化课程---9.php用什么写的 一.总结 一句话总结:php是用c语言写的,所以php里面的那些模块什么都是c语言 c 1.php用什么写的? c php是用c语言写的,所以php ...

  2. 分享知识-快乐自己:Ajax 跨域请求处理

    <%-- Created by IntelliJ IDEA. User: asus Date: 2019/1/24 Time: 15:57 To change this template use ...

  3. 003——VUE操作元素属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. L142

    keep half an eye on something分神留意splash out随意花钱 大肆挥霍half a mind有想做某事go Dutch v. 各自付帐,打平伙chance in a ...

  5. .net 学习路线感想(转)

    从上到大学到现在工作,已经有六年多了,发现学习编程到以开发为工作也是一个挺长的过程的. 大学中,从c语言到java.C#到其他各种语言的学习,还有其他知识的学习如:数据库(oracle.sql Ser ...

  6. Git常用命令以及用法

    一 如何让单个文件回退到指定的版本 1.   进入到文件所在文件目录,或者能找到文件的路径 查看整个目录的修改记录 git log . 2.   回退到指定的版本 git reset f7a22076 ...

  7. ng 双向数据绑定 实现 注册协议效果

    效果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...

  8. Java中最常见的十道面试题

    第一,谈谈final, finally, finalize的区别. final?修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被声明为 ...

  9. 毕业生、程序猿转岗该如何选择Java、大数据和VR?答案在这里!

    许久不见的朋友请我吃饭,期间给我介绍他一个弟弟,说明年要毕业了,还不知道找啥工作,说有培训机构让他学VR.大数据什么的,不知道前景咋样,想咨询一下我.相信很多朋友面临毕业,都不知道该从事哪个行业,自己 ...

  10. eclipse javaw.exe in your current path问题

    问题: 第一次运行eclipse的时候,可能会提醒找不到javaw.exe ******等的问题 很坑的! 解决方案: 无法启动Eclipe,因找不到javaw.exe 还是环境变量的问题!!! 注意 ...