HDU3018:Ant Trip(欧拉回路)

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=100005;
int n,m;
int deg[MAXN],vis[MAXN];
vector<int> arc[MAXN];
void dfs(int u,int& dep,int& odd)
{
    dep++;
    if(deg[u]&1)    odd++;
    vis[u]=1;
    for(int i=0;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,0,sizeof(deg));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)    arc[i].clear();
        for(int i=0;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=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                int dep=0,odd=0;
                dfs(i,dep,odd);
                if(dep>1)//连通分量存在边,即不为孤立点
                {
                    if(odd==0)    res++;//若奇数度的结点个数为0,则只需派一组人
                    else    res+=(odd/2);//派odd/2组人
                }
            }
        }
        cout<<res<<endl;
    }
    return 0;
}
时间: 2024-12-25 08:56:20

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

hdu-3018 Ant Trip(欧拉路径)

题目链接: Ant Trip Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) 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 ever

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

Ant Trip 题目描述 原题来自:2009 Multi-University Training Contest 12 - Host by FZU 给你无向图的 N 个点和 M 条边,保证这 M 条边都不同且不会存在同一点的自环边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 输入格式 多组数据,每组数据用空行隔开. 对于每组数据,第一行两个整数 N,M表示点数和边数.接下去 M 行每行两个整数 a,b,表示 a,b 之间有一条边. 输出格式 对于每组数据,输出答案. 样例

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

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) 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

杭电ACM3018——Ant Trip~~欧拉回路

这题,欧拉回路的应用,只是比较麻烦. 题目的意思是,一群蚂蚁,想要走遍给定的每一条边,但是,图不连通或者不存在欧拉回路的不可能走完,所以可以将人分组,求解最少的分组的数目. 解题的主要思路是: 1.将各点进行合并,使用并查集. 2.遍历并查集,查找各个连通图的奇数度的个数. 3.如果该连通图的奇数点为0,只需一笔.如果不是,就需要奇数点的数目 / 2. 还有一点需要注意的是,单个点应该无视他,题目有讲到. 题目有点坑,输入那里还有样例那里每一个测试样例有一个空行.可是实际上不需要空格,是的,不需

HDU3018 Ant Trip

题意:每条边过且只过一次,问至少要画几笔才能全部边都经过.孤立的点忽视. #include <iostream> using namespace std; const int M=100000+10; int gree[M]; int father[M]; int rank1[M]; int save[M]; bool used[M]; bool mark[M]; void Make_Set(int x) { father[x]=x; rank1[x]=0; } int Find(int x)

[欧拉回路] hdu 3018 Ant Trip

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1658    Accepted Submission(s): 641 Problem Description Ant Country consist of N to

hdu 3018 Ant Trip 算是一道欧拉通路的题目吧~

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1826    Accepted Submission(s): 702 Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,

poj 1041 John&#39;s trip 欧拉回路

题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 #include

HDU 3018 Ant Trip (欧拉路的个数 并查集)

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5501    Accepted Submission(s): 2146 Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,