HDU 3018 Ant Trip【欧拉路、并查集】

来源:互联网 发布:怎么用八爪鱼爬数据 编辑:程序博客网 时间:2024/06/10 00:14

Ant Trip

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


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,就能够得到欧拉路的总路数。

        int ans=0;        for(int i=1;i<=n;i++)        {            if(degree[i]%2==1)            {                vis[f[i]]=1;//这块我们下边详解、等价于vis[find(i)]=1                ans++;//统计欧拉路起点和终点            }        }        ans/=2;

统计完欧拉路,我们还要统计欧拉回路,那么欧拉回路怎样来判断呢?抛掉欧拉路剩下的就是欧拉回路了、

欧拉路我们要这样来抛掉:我们用一个vis【】数组来标记这条路的起点(节点祖先)是否统计过,在统计欧拉路的时候,一边统计一边标记,标记这条路是欧拉路,也就是上文中的vis【f【i】】=1(当然我路径压缩了,否者要写成注释里边的形式);

然后我们在统计欧拉路的时候,只要vis【i】==0&&F【i】==i就说明这是条路是欧拉回路了。

完整的AC代码:

#include<stdio.h>#include<string.h>using namespace std;int degree[100002];int f[100002];int vis[100002];//int use[100002];int find(int a){    int r=a;    while(f[r]!=r)    {        r=f[r];    }    int i=a;    int j;    while(i!=r)    {        j=f[i];        f[i]=r;        i=j;    }    return r;}void merge(int x,int y){    int xx=find(x);    int yy=find(y);    if(xx!=yy)    {        f[xx]=yy;    }}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        memset(vis,0,sizeof(vis));        memset(degree,0,sizeof(degree));        for(int i=1;i<=n;i++)        {            f[i]=i;            vis[i]=0;        }        for(int i=0;i<m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            degree[x]++;            degree[y]++;            merge(x,y);        }        for(int i=1;i<=n;i++)//对每一个点都路径压缩一下        {            find(i);        }        int ans=0;        for(int i=1;i<=n;i++)//统计欧拉路        {            if(degree[i]%2==1)            {                vis[f[i]]=1;                ans++;            }        }        ans/=2;        for(int i=1;i<=n;i++)//抛去欧拉路的路就是欧拉回路        {            if(degree[i]>0)            {                if(vis[f[i]]==0&&f[i]==i)                {                    ans++;                }            }        }        printf("%d\n",ans);    }}



0 0
原创粉丝点击