zoukankan      html  css  js  c++  java
  • 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,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

    题目大意与分析

    一笔画问题,求将所有的边都只走一次所需要的最少笔画数,首先用并查集维护连通图,对于每个连通图,如果所有的点度都为偶数,代表有一个欧拉路,如果只有一个点,则不需要笔画,对于有度为奇数的点,需要的笔画数为奇度点个数/2

    #include<bits/stdc++.h>
    
    using namespace std;
    
    int f[100005],deg[100005],g[100005],j[100005],anss,cnt,n,m;
    
    int findf(int x)
    {
        return x==f[x]?x:f[x]=findf(f[x]);
    }
    
    void hebing(int a,int b)
    {
        int fa=findf(a);
        int fb=findf(b);
        if(fa!=fb)
        {
            f[fa]=fb;
        }
    }
    
    int main()
    {
        while(cin>>n>>m)
        {
            anss=0;
            memset(deg,0,sizeof(deg));
            memset(g,0,sizeof(g));
            memset(j,0,sizeof(j));
            for(int i=1;i<=n;i++)
            f[i]=i;
            cnt=0;
            while(m--)
            {
                int a,b;
                cin>>a>>b;
                hebing(a,b);
                deg[a]++;
                deg[b]++;
             } 
            for(int i=1;i<=n;i++)
            {
                if(findf(i)==i)
                {
                    g[cnt++]=i;
                }
                if(deg[i]%2==1)
                {
                    j[findf(i)]++;
                }
            }
            for(int i=0;i<cnt;i++)
            {
                if(deg[g[i]]==0)
                continue;
                if(j[g[i]]==0)
                anss++;
                else
                anss+=j[g[i]]/2;
            }
            cout<<anss<<endl;
        }
     } 
  • 相关阅读:
    什么是web标准
    Axure 快捷方式
    asp.net mvc4中model与Model的区别
    Git 操作常用命令
    ASP.NET MVC 中@html.ActionLink的几种参数格式
    Datagridview控件实现分页功能
    winform 拖动无边框窗体(调用Windows API)
    利用C#轻松创建不规则窗体
    Linq to sql 操作
    第一篇博客,写些学习感想
  • 原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12635208.html
Copyright © 2011-2022 走看看