【HDU】3018Ant Trip-(欧拉图一笔画问题)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4703    Accepted Submission(s): 1842


 

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

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.

 

 

Source

2009 Multi-University Training Contest 12 - Host by FZU

 

 

Recommend

 

题目大意:现在给出一个有N个点和M条边的无向图,其中无向图中不存在对于某个点的自环,问至少需要几笔能将这个图的所有边都画一遍

思路:题目中给出的,某个图,不一定是欧拉通路或者欧拉回路,那样一笔就花完了,所以要求每个联通块,每个联通块中的欧拉回路或者欧拉通路的情况,其中有:

若某联通块为欧拉回路或者欧拉通路,则需要1笔;若某联通块不只有一条欧拉通路时,需要奇度数节点/2笔;若某联通块是一个点,则需要0,因为他不存在边,(重申题中给出的没有自环的条件...)

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;
const int maxn=100000+10;
int n,m;
int fa[maxn];
int num[maxn],odd[maxn],degree[maxn];
int findset(int x)
{
    if(fa[x]==-1)
        return x;
    return fa[x]=findset(fa[x]);
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(fa,-1,sizeof(fa));
        memset(num,0,sizeof(num));
        memset(odd,0,sizeof(odd));
        memset(degree,0,sizeof(degree));//。。。。

        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            degree[u]++,degree[v]++;
            int fu=findset(u),fv=findset(v);
            if(fu!=fv)
                fa[fu]=fv;
        }
        for(int i=1;i<=n;i++)
        {
            num[findset(i)]++;
            if(degree[i]%2) odd[findset(i)]++;
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(num[i]<=1) continue;
            else if(odd[i]==0) ans++;
            else if(odd[i]>0) ans+=odd[i]/2;
        }

        printf("%d\n",ans);
    }
    return 0;
}

证明:若某联通块不只有一条欧拉通路时,需要奇度数节点/2笔

证明过程转自:https://blog.csdn.net/u013480600/article/details/30285541

谢谢大神!!

首先一个无向图的连通分量中的奇数度的点个数一定是偶数个(成对出现),因为无向图的总度数=偶数.

       我们在这种连通分量中每次画一笔有两种选择:

       1.a->b->c->d…->g    一条起点与终点不同的路径(路中除首尾度减1外,每个点度减2)

       2.a->b->c->d…->a    一条起点与终点相同的回路(路中每个点度数减2)

       也就是说想要把非(半)欧拉图分量中的奇数度的点的度数都变成偶数,我们至少需要画奇数度点个数/2 笔.

       那么对于这种图我们最多需要画的笔数 是不是也是 : 奇数度点个数/2 笔呢?

       答案是肯定的,这里假设图中有4个奇数度的点,1,2,3,4,5,6.如下图所示:

       我们先走路:1-> b-> c-> d-> e-> f->a-> 2 这条路.然后6,5 和3,4 分别属于两个连通分量了.可以看出只需要3笔,即6/2=3即可.

       当然也可以这么画:1->b->2 然后6->f->5 ,接着就剩下了一个半欧拉图了,半欧拉图也是1笔画,正好3笔.

       也就是说对于这种非(半)欧拉图的连通分量,我们每笔必然消除正好2个点的奇度(使其度变偶数),当最后一笔的时候我们必然消除所有的点的度数(包括剩下的两个奇点,因为最后一笔就必然是欧拉通路).但是有一点要注意,有可能过程中的某几笔会使得该连通分量变成多个连通分量,当然结论不变.

       经过上面的分析,这题的结论出来了,对于每个以i为根的连通分量我们记录属于该连通分量的点数目num[i]和该连通分量中奇度点的个数odd[i].

       如果num[i]==0或1,需0笔.(注意num[i]==0表示i点不是根,num[i]==1表示i点是一个孤立的点.)

       如果num[i]>1且odd[i]==0 需1笔

       如果num[i]>1且odd[i]>0 需odd[i]/2笔
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值