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. 在spring boot中使用自定义的properties

    1 在application.properties中添加 android.name=Tim android.password=123456 新建一个保存该Setting的配置类, @Configura ...

  2. UITableview刷新时界面“乱跑”现象

    Self-Sizing在iOS11下是默认开启的,Headers, footers, and cells都默认开启Self-Sizing,所有estimated 高度默认值从iOS11之前的 0 改变 ...

  3. 在VS2017环境中编译libxml2库

    libxml2库编译 1.下载libxml2,官网是:http://www.xmlsoft.org/downloads.html, 我下载的版本是:libxml2-sources-2.9.7.tar. ...

  4. 14.Django自带的admin配置

    admin有自己的默认显示,要自定义显示的样式,一般需要自己定义一个类,在自己定义的类里进行相应的设置,然后,把自己的类交给装饰器 交给装饰器的方法有两种: 1.@admin.register(Pub ...

  5. ARDUINO W5100 WebClient 测试

    基础工作:W5100扩展板插在ARDUINO上.用网线把W5100和自己家的路由器连接.插上网线能看到侧面网口指示灯变亮.路由器开启DHCP服务(一般都是开启的). 1.打开官方例程里面的Ethern ...

  6. Django模型系统——ORM表结构对应关系

    对于数据库来说一般表结构只会有三种对应关系,分别是一对一.一对多和多对一,下面分别介绍: 1.一对多 何为一对多,例如一个学生只可能有一个班级,一个班级却又多个学生,班级表和学生表就是一对多的关系. ...

  7. IOS 判断当前UIViewController 是否正在显示

    我通常的做法是根据视图控制器的生命周期来判断,其是否是正在使用的状态. 举例 设一个实例布尔变量isVisible  在 -ViewWillAppear 里面 isVisible = YES ;  在 ...

  8. 使用cocoaPods import导入时没有提示的解决办法

    1.选择target(就是左边你的工程target)—— BuildSettings —— search Paths 下的 User Header Search Paths(如图所示:)    2.双 ...

  9. 【leetcode刷题笔记】Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  10. 剑指offer之 数组中出现次数超过一半的数字

    public class Solution { public int MoreThanHalfNum_Solution(int [] array) { if(array==null||array.le ...