码迷,mamicode.com
首页 > 其他好文 > 详细

HDU3018:Ant Trip(欧拉回路)

时间:2016-07-08 18:15:36      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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;    
}

 

HDU3018:Ant Trip(欧拉回路)

标签:

原文地址:http://www.cnblogs.com/program-ccc/p/5654045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!