Ant Trip

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

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个边,问要遍历一遍所有的边,需要的最小group数目。
思路:首先通过并查集判断有几个联通块。然后对于每个联通块,统计需要多少笔。
如果,联通块中,度为奇数节点的个数为0或2个,那就可以一笔画完。
反之,因为一笔最多消去两个度为奇数的节点,所以要最少用奇度数节点个数/2笔。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,tot,ans;
int vis[];
int fa[],num[];
int into[],du[];
int find(int x){
if(fa[x]==x) return fa[x];
else return fa[x]=find(fa[x]);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
tot=;ans=;
memset(du,,sizeof(du));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(into,,sizeof(vis));
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
into[x]++;into[y]++;
int dx=find(x);int dy=find(y);
if(dx!=dy) fa[dy]=dx;
}
for(int i=;i<=n;i++){
int now=find(i);
if(!vis[now]){
vis[now]=;
num[++tot]=now;
}
if(into[i]%!=) du[now]++;
}
for(int i=;i<=tot;i++){
if(into[num[i]]==) continue;
if(du[num[i]]==) ans++;
ans+=du[num[i]]/;
}
cout<<ans<<endl;
}
}

HDU 3108 Ant Trip的更多相关文章

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (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. 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(欧拉回路,要几笔)

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

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

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

  6. HDU 3018 Ant Trip

    九野的博客,转载请注明出处:  http://blog.csdn.net/acmmmm/article/details/10858065 题意:n个点m条边的无向图,求用几笔可以把所有边画完(画过的边 ...

  7. 一本通1530 Ant Trip

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

  8. HDU3018: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(欧拉路径)

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

随机推荐

  1. 中国剩余定理(excrt) 模板

    excrt板子题 #include <cmath> #include <cstdio> #include <cstring> #include <algori ...

  2. centos 登陆跳转指定目录

    vi /etc/bashrc cd /usr/local 重启 reboot

  3. 8:30+1.5小时,返回时间格式的 php函数

    一个实用的自定义函数 /** * 传入8:30格式的开始时间,和小数形式的小时长度,返回结束时间 * @param [type] $start [description] 8:30 * @param ...

  4. 【codeforces 799D】Field expansion

    [题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...

  5. like

    5.在WHERE中使用like做模糊查询    %符号表示0到多个任意字符    _符号表示1个任意字符     //查询名字中含有O字符的员工信息   select empno,ename   fr ...

  6. Fedora 17 无线网卡配置笔记

    转载:http://www.psichen.com/fedora-17-wifi/ 安装并更新完F17后,在网络选项中没有出现无线网,需要自己安装无线网卡驱动.而F17中默认网卡名称从以前的”eth0 ...

  7. Java程序命令行打包Jar

    最近要跑爬虫程序,需要打包成jar发在linux服务器中运行.主要是第三方的lib包与配置文件,不进行打包,方便修改. 1.eclipse中src中源码编译后生成的源码在bin文件中,把里面源码单独拿 ...

  8. Ruby中写换行

    Ruby中写换行 print("Hello,\nRuby\n!\n") print("Hello, Ruby ! ") 这两个竟然是一样的:就是说,可以直接回车 ...

  9. 小记 SqlHelper

    using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;u ...

  10. githubclient配置方法简述

    /*********************************************************** * Author : Samson * Date : 08/15/2015 * ...