hdu3018 Ant Trip(欧拉回路)

Ant Trip
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2847 Accepted Submission(s): 1125

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.

题意:给你一些联通图,要求最少的群组形式走过所有的点

也就是用最少的笔画画遍这个图,图中可能形成的区域为有向图欧拉通路形式的,也有可能形成有向图欧拉回路形式的,对于通路形式,笔画肯定为度数为奇数的点/2次,对于回路形式,笔画一笔就能画完。所以整合笔画就是最少
一:通路,a——>b
二:回路,a——>…——>a
算法思路:利用并查集,将图全联通,用一个数组装上每一个点的度数,然后遍历所有的点,对于图中的每一块,度数数为奇数的点必须是由第一种画出来的,所以奇数/2就是画的笔数
如果图只有第二种的话,即该块中不存在奇数点,则只要画一笔

对于整副图(每一块块组合而成),等于 :第一块奇数点/2+第二块奇数点/2+…….,最后得,图的总奇数点/2+偶数的笔画

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 100000+1
int d[N];
int fa[N];
int vist[N];
int save[N],gree[N],rank1[N],mark[N];
void init(int x)
{
    fa[x] = x;
    rank1[x] = 0;
}

int find(int x)
{
    return x==fa[x]?fa[x]:find(fa[x]);
}


void Union(int a, int b)
{

    int x = find(a);
    int y = find(b);

    if(x!=y)
    {
        fa[x]=y;
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(gree,0,sizeof(gree));
        memset(vist,0,sizeof(vist));
        memset(mark,0,sizeof(mark));
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            if(!vist[a])
            {
                vist[a]=1;
                init(a);
            }
            if(!vist[b])
            {
                vist[b]=1;
                init(b);
            }
            gree[a]++;///度数
            gree[b]++;
            Union(a,b);
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(vist[i]&&gree[i]%2==1)
            {
                if(mark[find(i)]==0)///有奇数度需要奇数度/2次连接
                {
                    //printf("-->%d\n",find(i));
                    mark[find(i)]=1;
                }
                ans++;
            }
        }
        ans/=2;
        for(int i=1;i<=n;i++)
        {
            if(vist[i]&&fa[i]==i&&mark[i]==0)///全为偶数度,只需要一次连接
            ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值