一个图至少几笔画可以画出

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.

对于一个联通块集合来说,如果它有奇度数节点  asn+奇度数/2;

如果只有偶数度数,那么ans+1

孤立点排除,利用并查集找联通块

#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<string.h>
#include<stdio.h>
#include<cmath>
using namespace std;
int f[600000];
int n,m;
vector<int> vt;
void init(int n)
{
    vt.clear();
    for(int i=1; i<=n; i++)
    {
        f[i]=i;
    }
}
int finds(int x)
{
    return x==f[x]?x:f[x]=finds(f[x]);
}
void unions(int x,int y)
{
    int fx=finds(x);
    int fy=finds(y);
    if(fx!=fy)
    {
        f[fx]=fy;
    }
}
int du[600000];
int vis[600000];
int odd[600000];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init(n);
        scanf("%d",&m);
        memset(du,0,sizeof(du));
        memset(vis,0,sizeof(vis));
        memset(odd,0,sizeof(odd));
        int x,y;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            unions(x,y);
            du[x]++;
            du[y]++;
        }
        int flag=0;
        for(int i=1; i<=n; i++)
        {
            int fx=finds(i);
            if(!vis[fx])
            {
                vt.push_back(fx);
                vis[fx]=1;
            }
            if(du[i]%2)
                odd[fx]++;
        }
        int ans=0;
        for(int i=0; i<vt.size(); i++)
        {
            int temp=vt[i];
            if(du[temp]==0)continue;//孤立节点
            if(odd[temp]==0)ans++;
            else ans+=odd[temp]/2;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/87900561