hdoj3018 Ant Trip 欧拉回路

来源:互联网 发布:数据库系统的三级模式 编辑:程序博客网 时间:2024/05/29 06:33

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.

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个点,m个边,问要遍历所有的边,需要的最小组队数。

利用并查集和欧拉回路,求一个图中最少几笔画。
一个连通块里构成欧拉回路的最少起点数=max(1, 连通块里度为奇数的个数)。


代码:

#include<stdio.h>#include<bits/stdc++.h>using namespace std;const int maxn=100000+10;int odd[maxn],du[maxn],cnt[maxn],f[maxn];int find(int x){    if(x==f[x])       return  x;    return find(f[x]);}void mix(int x,int y){    int t1=find(x);    int t2=find(y);    if(t1!=t2)        f[t1]=t2;}int n,m;void init(){    memset(odd,0,sizeof(odd));    memset(du,0,sizeof(du));    memset(cnt,0,sizeof(cnt));    for(int i=0;i<=n;i++)        f[i]=i;}int main(){    while(~scanf("%d%d",&n,&m))    {        init();        int a,b;        for(int i=0;i<m;i++)        {            scanf("%d%d",&a,&b);            mix(a,b);            du[a]++;            du[b]++;        }        for(int i=1;i<=n;i++)        {            int fa=find(i);            cnt[fa]++;//连通块的个数            odd[fa]+=du[i]%2;//奇点个数        }        int sum=0;        for(int i=1;i<=n;i++)        {           if(cnt[i]>=2)               sum+=max(1,odd[i]/2);        }        printf("%d\n",sum);    }}
1 0