Ant Trip

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

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
 
思路:dfs遍历所有连通分量。若某连通分量不是孤立点即存在边,那么设连通分量中奇数度结点的个数为odd,如果odd为0,只需派一组人否则派odd/2组人。
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
const int MAXN=;
int n,m;
int deg[MAXN],vis[MAXN];
vector<int> arc[MAXN];
void dfs(int u,int& dep,int& odd)
{
dep++;
if(deg[u]&) odd++;
vis[u]=;
for(int i=;i<arc[u].size();i++)
{
int v=arc[u][i];
if(!vis[v])
{
dfs(v,dep,odd);
}
}
}
int main()
{
while(cin>>n>>m)
{
memset(deg,,sizeof(deg));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++) arc[i].clear();
for(int i=;i<m;i++)
{
int u,v;
cin>>u>>v;
arc[u].push_back(v);
arc[v].push_back(u);
deg[u]++;
deg[v]++;
}
int res=;
for(int i=;i<=n;i++)
{
if(!vis[i])
{
int dep=,odd=;
dfs(i,dep,odd);
if(dep>)//连通分量存在边,即不为孤立点
{
if(odd==) res++;//若奇数度的结点个数为0,则只需派一组人
else res+=(odd/);//派odd/2组人
}
}
}
cout<<res<<endl;
}
return ;
}

HDU3018:Ant Trip(欧拉回路)的更多相关文章

  1. hdu-3018 Ant Trip(欧拉路径)

    题目链接: Ant Trip Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

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

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

  3. hdu3018 Ant Trip (并查集+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题意:给你一个图,每条路只能走一次.问至少要多少个人才能遍历所有的点和所有的边. 这是之前没有接 ...

  4. HDU 3018 Ant Trip (欧拉回路)

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

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

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

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

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

  7. 一本通1530 Ant Trip

    1530:Ant Trip [题目描述] 原题来自:2009 Multi-University Training Contest 12 - Host by FZU 给你无向图的 N 个点和 M 条边, ...

  8. HDU 3108 Ant Trip

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

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

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

随机推荐

  1. linux 创建账户

    linux下创建用户 linux下创建用户(一) Linux 系统是一个多用户多任务的分时操作系统,不论什么一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. ...

  2. 改善程序与设计的55个具体做法 day8

    条款20:宁以pass-by-reference-to-const 替换 pass-by-value 即 以const引用 替换值传递. 采用引用传递参数时,底层往往是用指针方式实现,因此参数传递内置 ...

  3. PHP数组各种操作与函数汇总

    对于Web编程来说,最重要的就是存取和读写数据了.存储方式可能有很多种,可以是字符串.数组.文件的形式等.数组,可以说是PHP的数据应用中较重要的一种方式.PHP的数组函数众多,下面是我学习的小结,借 ...

  4. Python程序打包成exe的一些坑

    今天写了一个项目,Python项目,需要在win7上跑起来,我想,这不是简单的不行么,直接上Pyinstaller不就完了? 但是后来,我发觉我真是too young too simple. 为什么这 ...

  5. ios 表情编码

    感受 :可以做自定义键盘时候用  很方便 还可以在textView里面看到 用户体验很好~ 但是要和服务器管理员协商好,做好解析转码工作,不然网页上是不显示的. ios表情编码 在ios中可以使用可爱 ...

  6. Data Structure Array: Maximum circular subarray sum

    http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/ #include <iostream> #include < ...

  7. P4388 付公主的矩形(gcd+欧拉函数)

    P4388 付公主的矩形 前置芝士 \(gcd\)与欧拉函数 要求对其应用于性质比较熟,否则建议左转百度 思路 有\(n×m\)的矩阵,题目要求对角线经过的格子有\(N\)个, 设函数\(f(x,y) ...

  8. 一个例子看懂所有nodejs的官方网络demo

    今天看群里有人用AI技术写了个五子棋,正好用的socket.io,本身我自己很久没看nodejs了,再加上Tcp/IP的知识一直很弱,我就去官网看了下net.socket 发现之前以为懂的一个官方例子 ...

  9. Android系统Recovery模式的工作原理【转】

    本文转载自:http://blog.csdn.net/mu0206mu/article/details/7464987  在使用update.zip包升级时怎样从主系统(main system)重启进 ...

  10. 修改push动画的方向

    CATransition *animation = [CATransition animation]; animation.duration = 0.4; animation.timingFuncti ...