HDU 3018 Ant Trip(欧拉回路,要几笔)

Ant Trip

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


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.
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  3013 3015 3016 3011 3010 
 
  题目大意:其实题目意思就是给你一幅图,问最少用多少笔可以把整幅图划完,每条边只能经过一次,孤立点忽略不计。这个就是欧拉回路的问题,判断划几笔要看入度和出度的值,还有入度-出度的值,判断有多少个欧拉路。用并查集来判断连通性,度数判断欧拉回路。
 

题意:给一个无向图,N个顶点和M条边,问至少需要几笔才能把所有边画一遍

思路:只要知道一个结论就随便做了。

         如果该连通分量是一个孤立的点,显然只需要0笔.

         如果该连通分量是一个欧拉图或半欧拉图,只需要1笔.

         非(半)欧拉图需要的笔数==该图中奇数度的点数目/2

         对于每个以i为根的连通分量我们记录属于该连通分量的点数目num[i]和该连通分量中奇度点的个数odd[i]。

详见代码注释:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <map>
 7 using namespace std;
 8 int a[100005];
 9 int pra[100005];
10 int rak[100005];
11 int num[100005];//属于该连通分量的点数目
12 int odd[100005];//该连通分量中奇度点
13 int find(int x)
14 {
15     if(pra[x]==x) return x;
16     else return pra[x]=find(pra[x]);
17 }
18 void unite(int x,int y)
19 {
20     int xx=find(x);
21     int yy=find(y);
22     if(xx==yy) return;
23     else
24     {
25         if(rak[xx]<rak[yy]) pra[xx]=yy;
26         else
27         {
28             pra[yy]=xx;
29             if(rak[xx]==rak[yy]) rak[xx]++;
30         }
31     }
32 
33 }
34 int main()
35 {
36     int n,m;
37     while(cin>>n&&n)
38     {
39         cin>>m;
40         memset(a,0,sizeof(a));
41         memset(num,0,sizeof(num));
42         memset(odd,0,sizeof(odd));
43         for(int i=1;i<=n;i++)
44         {
45             pra[i]=i;
46             rak[i]=1;
47         }
48         for(int i=1;i<=m;i++)
49         {
50             int x,y;
51             cin>>x>>y;
52             a[x]++;//度数+1
53             a[y]++;
54             unite(x,y);//合并
55         }
56         int ans=0;
57         for(int i=1;i<=n;i++)
58         {
59             num[find(i)]++;//属于该连通分量的点数目
60             if(a[i]%2==1)
61                 odd[find(i)]++;//该连通分量中奇度点
62         }
63         for(int i=1;i<=n;i++)
64         {
65              if (num[i]<=1)  //只有一个点,不用走
66                 continue;  
67             else if (odd[i]==0)  //没有奇度数点,那就+1
68                 ans++;  
69             else  //有的话
70                 ans+=odd[i]/2; //+奇度数点/2
71         }
72         cout<<ans<<endl;
73     }
74     return 0;
75 }
原文地址:https://www.cnblogs.com/caiyishuai/p/13271147.html