HDU 3018 Ant Trip

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3018 Ant Trip相关的知识,希望对你有一定的参考价值。

Ant Trip

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


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
--------------------------------------------------------------------------------------------手动分割线--------------------------------------------------------------------------------------------------------
题目大意:
  给你N个点并且他们之间的路径,问你最少要画几笔才能经过全部边。
  第一行包括两个数N,M(N表示点的个数,M表示路径数)
  接下来是M行,每一行包括两个数a,b表示a,b之间有路径。
  最后输出要画几笔。(注意有多组输入数据)
大致思路:
  先用并查集判断图中有几个联通块,对于每个联通块,判断其中的点的度,如果其中点的度都是偶数,那么便存在欧拉回路,只用画一笔,如果是奇数,则要画该联通块中奇数的个数/2笔。
代码附上:
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 #include<vector>
 5 using namespace std;
 6 #define N 100006
 7 int fa[N];
 8 int du[N];
 9 int vis[N];
10 int ji[N];
11 vector<int> q;
12 void init(int n)
13 {
14     for(int i=0;i<=n;i++)
15     {
16         fa[i]=i;
17     }
18     memset(du,0,sizeof(du));
19     memset(vis,0,sizeof(vis));
20     memset(ji,0,sizeof(ji));
21     q.clear();
22 }
23 int myfind(int x)
24 {
25     if(fa[x]!=x)
26     {
27         fa[x]=myfind(fa[x]);
28     }
29     return fa[x];
30 }
31 void hebing(int x,int y)
32 {
33     x=myfind(x);
34     y=myfind(y);
35     if(x!=y)
36     {
37         fa[y]=x;
38     }
39 }
40 int main()
41 {
42     int n,m;
43     while(scanf("%d%d",&n,&m)!=EOF)
44     {
45         int a,b;
46         init(n);
47         int ans=0;
48         for(int i=0;i<m;i++)
49         {
50             scanf("%d%d",&a,&b);
51             hebing(a,b);
52             du[a]++;
53             du[b]++;
54         }
55         for(int i=1;i<=n;i++)
56         {
57             int f=myfind(i);
58             if(!vis[f])
59             {
60                 vis[f]=1;
61                 q.push_back(f);
62             }
63             if(du[i]%2==1)
64             {
65                 ji[f]++;
66             }
67         }
68         for(int i=0;i<int(q.size());i++)
69         {
70             int f=q[i];
71             if(du[f]==0)continue;
72             if(ji[f]==0)ans++;   
73             else ans+=ji[f]/2;
74         }
75         printf("%d\n",ans);
76     }
77     return 0;
78 }

 










以上是关于HDU 3018 Ant Trip的主要内容,如果未能解决你的问题,请参考以下文章

hdu 3018 Ant Trip 欧拉回路+并查集

HDU 3018 Ant Trip (欧拉路的个数 并查集)

HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

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

Ant Trip HDU - 3018(欧拉路的个数 + 并查集)

Ant Trip(区别于二分匹配中最小路径覆盖的一笔画问题)