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

23 篇文章 0 订阅
4 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=3018
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

思路构造:
本题是让你求这个图需要用几笔画可以画成,应注意的一点,孤立的点不计入笔画中,然后这个图就可以构造连通分支,构造完连通分支后计算每个连通分支需要几笔画可以画成,然后把每个联通分支的结果加起来就是最终的结果了。

下面是AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

int pre[100005],zitu[100005],deg[100005];

int fin(int x)
{
    if(x==pre[x])
    {
        return x;
    }
    else
    {
        return pre[x]=fin(pre[x]);
    }
}

void join(int x,int y)
{
    int t1=fin(x);
    int t2=fin(y);
    if(t1!=t2)
    {
        pre[t1]=t2;
    }
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(deg,0,sizeof(deg));
        for(int i=1;i<=n;i++)
        {
            pre[i]=i;
        }
        memset(zitu,-1,sizeof(zitu));//假设每个点都可能是连通分支的最高点
        int u,v;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            if(u==v)
            {
                continue;
            }
            join(min(u,v),max(u,v));
            deg[u]++,deg[v]++;
        }
        for(int i=1;i<=n;i++)
        {
            fin(i);
            if(zitu[pre[i]]==-1&&deg[i]>0)
                zitu[pre[i]]=0;
            if(deg[i]%2)
            {
                zitu[pre[i]]++;
            }
        }
        int re=0;
        for(int i=1;i<=n;i++)
        {
            if(zitu[i]>0)
            {
                re+=zitu[i]/2;
            }
            else if(zitu[i]==0)
            {
                re++;
            }
        }
        printf("%d\n",re);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值