zoukankan      html  css  js  c++  java
  • 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;    
    }
  • 相关阅读:
    【t066】致命的珠宝
    【t081】序列长度
    【t081】序列长度(贪心做法)
    【t093】外星密码
    【codeforces 761A】Dasha and Stairs
    【codeforces 761B】Dasha and friends
    【codeforces 761C】Dasha and Password(动态规划做法)
    【codeforces 761C】Dasha and Password(贪心+枚举做法)
    【codeforces 761D】Dasha and Very Difficult Problem
    【u224】传送机
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5654045.html
Copyright © 2011-2022 走看看