HDU 3018 Ant Trip
Ant Trip
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.
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
题意: 判断一共有多少个欧拉路
sl: 首先并查集维护欧拉路的个数,然后一笔画需要的次数就等与奇数度的节点+欧拉路的个数。-----并查集很神奇啊。
1 //判断有多少欧拉回路
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<vector>
6 using namespace std;
7 const int MAX = 1e5+10;
8 vector<int> v;
9 int ans[MAX],vis[MAX];
10 int deg[MAX],odd[MAX];
11 void make(int n)
12 {
13 for(int i=1;i<=n;i++) ans[i]=i;
14 }
15 int find(int x)
16 {
17 if(ans[x]!=x)
18 ans[x]=find(ans[x]);
19 return ans[x];
20 }
21
22 void Union(int x,int y)
23 {
24 ans[x]=y;
25 }
26 int main()
27 {
28 int ret,n,m; int a,b;
29 while(scanf("%d %d",&n,&m)==2)
30 {
31 make(n);
32 ret=0; memset(vis,0,sizeof(vis));
33 memset(odd,0,sizeof(odd));
34 memset(deg,0,sizeof(deg));
35 v.clear();
36 for(int i=0;i<m;i++)
37 {
38 scanf("%d %d",&a,&b);
39 deg[a]++; deg[b]++;
40 int x=find(a); int y=find(b);
41 if(x!=y) Union(x,y);
42 }
43 for(int i=1;i<=n;i++)
44 {
45 int u=find(i); //找一个欧拉回路。
46 if(!vis[u])
47 {
48 vis[u]=1;
49 v.push_back(u);
50 }
51 if(deg[i]&1) odd[u]++;
52 }
53 for(int i=0;i<v.size();i++)
54 {
55 int k=v[i];
56 if(deg[k]==0) continue;
57 if(odd[k]==0) ret++;
58 ret+=odd[k]/2;
59 }
60 printf("%d\n",ret);
61 }
62 return 0;
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<vector>
6 using namespace std;
7 const int MAX = 1e5+10;
8 vector<int> v;
9 int ans[MAX],vis[MAX];
10 int deg[MAX],odd[MAX];
11 void make(int n)
12 {
13 for(int i=1;i<=n;i++) ans[i]=i;
14 }
15 int find(int x)
16 {
17 if(ans[x]!=x)
18 ans[x]=find(ans[x]);
19 return ans[x];
20 }
21
22 void Union(int x,int y)
23 {
24 ans[x]=y;
25 }
26 int main()
27 {
28 int ret,n,m; int a,b;
29 while(scanf("%d %d",&n,&m)==2)
30 {
31 make(n);
32 ret=0; memset(vis,0,sizeof(vis));
33 memset(odd,0,sizeof(odd));
34 memset(deg,0,sizeof(deg));
35 v.clear();
36 for(int i=0;i<m;i++)
37 {
38 scanf("%d %d",&a,&b);
39 deg[a]++; deg[b]++;
40 int x=find(a); int y=find(b);
41 if(x!=y) Union(x,y);
42 }
43 for(int i=1;i<=n;i++)
44 {
45 int u=find(i); //找一个欧拉回路。
46 if(!vis[u])
47 {
48 vis[u]=1;
49 v.push_back(u);
50 }
51 if(deg[i]&1) odd[u]++;
52 }
53 for(int i=0;i<v.size();i++)
54 {
55 int k=v[i];
56 if(deg[k]==0) continue;
57 if(odd[k]==0) ret++;
58 ret+=odd[k]/2;
59 }
60 printf("%d\n",ret);
61 }
62 return 0;
63 }