HDU 3018 Ant Trip

来源:互联网 发布:备案域名格式要求 编辑:程序博客网 时间:2024/06/09 19:28
Ant Trip
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

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 31 22 31 34 21 23 4
 

Sample Output

12

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

2009 Multi-University Training Contest 12 - Host by FZU

 

题意:

在一个图中每条边只能通过一次

问至少需要几笔画才能画成

 

代码:

#include<cstdio>#include<cstring>#include<cstdlib>#include<vector>#define MAX 200002using namespace std;int point[MAX];  //记录点的度数int odd[MAX];    //记录一棵树中度数为奇数的点的个数int tree[MAX];bool vist[MAX];vector<int> vec;int n,m;void init(){vec.clear();for(int i=1;i<=n;i++){point[i]=0;odd[i]=0;tree[i]=i;vist[i]=false;}}int Find(int i){while(i!=tree[i])i=tree[i];return i;}void Merge(int x,int y){int xx=Find(x);int yy=Find(y);if(xx!=yy)tree[xx]=yy;}int main(){while(scanf("%d%d",&n,&m)!=EOF){init();int a,b;for(int i=1;i<=m;i++){scanf("%d%d",&a,&b);point[a]++;point[b]++;Merge(a,b);}for(int i=1;i<=n;i++){int k=Find(i);if(!vist[k]){vist[k]=true;vec.push_back(k);}if(point[i]%2==1) odd[k]++;}int sum=0;for(int i=0;i<vec.size();i++){int k=vec[i];if(point[k]==0) continue;if(odd[k]==0) sum++;  //若一棵树中度数为奇数的点为零,说明为回路else sum+=odd[k]/2;   //否则所需笔画数为度数为奇数点个数除以2}printf("%d\n",sum);}return 0;}


思路:

欧拉回路+一笔画问题

判断能形成欧拉回路的条件:

图中度数为奇的点=0或2 则可构成欧拉回路 可仅由一笔画成

否则:

所需的笔画数=所有度数为奇的点的度数/2 之和

原创粉丝点击