HDU - 3018 Ant Trip(欧拉回路一笔画+并查集)

55 篇文章 0 订阅
7 篇文章 0 订阅

点击打开题目链接

Ant Trip

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

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

Recommend

gaojie   |   We have carefully selected several similar problems for you:   3013  3015  3016  3011  3010 

Statistic | Submit | Discuss | Note

题目大意:一个队想遍访所有小城,因为每个队每条路只能走一次,所以不可能完成遍访所有小城的任务。因此该队被分成了很多小队,求得使能够遍访所有小城的最小队数。(TAT 请原谅我表达不清)

思路:先用并查集搭图,然后再判断欧拉回路。用in数组统计每个节点的度(因为本题无向图),用cnt数组统计相同集合的节点数,用odd数组统计某集合的奇度总数。

①如果某个城镇所在集合的子节点只有它自己,则不考虑该孤立的点。

②如果某个城镇所在集合的奇度总数为0,则该集合可以构成欧拉回路。即可以用一个小队完成。

③如果某个城镇所在集合的奇度总数不为0,则该集合构成了非欧拉回路的其他连通集。则需要奇度总数/2个小队数。因为一条欧拉路径最多有两个奇度点。

附上AC代码:

#include<iostream>
#include<cstring>

using namespace std;
const int maxn=1e5+5;
int par[maxn],in[maxn],cnt[maxn],odd[maxn];
int N,M;
int a,b;
int ans;

void init()
{
    for(int i=1;i<=N;i++)
        par[i]=i;
    memset(in,0,sizeof(in));
    memset(cnt,0,sizeof(cnt));
    memset(odd,0,sizeof(odd));
    ans=0;
}

int find(int a)
{
    if(a==par[a])return a;
    return par[a]=find(par[a]);
}

void unite(int a,int b)
{
    a=find(a);
    b=find(b);
    if(a==b)return ;
    else if(a>b)
        par[a]=b;
    else
        par[b]=a;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>N>>M)
    {
        init();
        for(int i=0;i<M;i++)
        {
            cin>>a>>b;
            unite(a,b);
            in[a]++;in[b]++;//统计节点度
        }
        for(int i=1;i<=N;i++)
        {
            cnt[find(i)]++;//统计i的根节点的节点数
            if(in[i]%2)odd[find(i)]++;//统计i的根节点的节点奇度数
        }
        for(int i=1;i<=N;i++)
        {
            if(cnt[i]<=1)continue;//独立的点不用考虑或者不为根节点的点不考虑
            else if(odd[i]==0)ans++;//构成欧拉回路一笔画
            else if(odd[i]>0)ans+=odd[i]/2;//没有构成欧拉回路奇度/2
        }
        cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值