hdu3018 Ant Trip(欧拉回路)

来源:互联网 发布:淘宝众筹赚钱吗 编辑:程序博客网 时间:2024/05/14 20:06

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+1int 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;}
0 0