HDU 3018 欧拉回路

Posted liyexin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3018 欧拉回路相关的知识,希望对你有一定的参考价值。

HDU - 3018

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. 

InputInput 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.OutputFor 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.
    给出一个图,问几笔画才能经过所有边。
   欧拉回路,知识点已经在上个博客提到。对于每个点的出度,如果存在奇数,那么需要奇数/2笔才能经过所有的点。
    给出的图并没有说明是否为连通图,所以可能有多个图,那么这种情况,ans=奇数度个数/2+欧拉回路个数(只含偶数点的集合)
  解析在代码里
  
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;    //jishu/2+oulatu
int pr[maxn],cnt[maxn],mark[maxn];
int n,m;
int ans=0;
void first()
{
    for(int i=1;i<=n;i++)
    {
        pr[i]=i;
    }
    memset(cnt,0,sizeof(cnt));
    memset(mark,0,sizeof(mark));
    ans=0;
}
int find(int x)
{
    if(x!=pr[x])
        return pr[x]=find(pr[x]);
        return x;
}
void join(int a,int b)
{
    int f1=find(a),f2=find(b);
    if(f1!=f2)
        pr[f1]=f2;
        return;
}
void ac()
{
    for(int i=1;i<=n;i++)
    {
        if(cnt[i]%2!=0)
        {
            int f=find(i);    //统计奇数度点数量。用mark[]数组来记录,如果i点奇度,那么i所在图不是欧拉回路,那么i的根节点标为1,代表此图不是欧拉回路。
            mark[f]=1;
            ans++;
        }
    }
    ans/=2;
    for(int i=1;i<=n;i++)  //统计欧拉回路图
    {
        if(cnt[i]>0)      //比如输入,9  3  9个点只给出了3个关系,肯定有点不算,cnt[i]=0,不能纳入计算。
        {                
            int f=find(i);    //找到i的根节点,如果没被标为1,说明i出度为偶数,而且满足pr[i]==i(i==f)(即搜到x==pr[x]时还是没被标记)说明此图是个欧拉回路,因为如果存在奇度点,i==pr[i]
                    //处肯定被标记了。      ans++;
if(mark[f]==0&&pr[i]==i) { ans++; } } } } int main() { while(cin>>n>>m) { first();    //初始化 for(int i=1;i<=m;i++) { int a,b; cin>>a>>b; join(a,b); cnt[a]++;  //加入并查集,统计入度出度 cnt[b]++; } ac(); cout<<ans<<endl; } return 0; }

  

以上是关于HDU 3018 欧拉回路的主要内容,如果未能解决你的问题,请参考以下文章

HDU-3018-Ant Trip-欧拉回路+并查集

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

欧拉路HDU3018

hdu1878欧拉回路(DFS+欧拉回路)

hdu1878 欧拉回路(并查集+无向图欧拉回路)

hdu 1956 (网络流解决欧拉回路)