HDU 3018 Ant Trip (Euler number of disjoint-set path)

Ant Trip

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


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

Topic effect and analysis

Eulerian path, seeking to go all the sides only a minimum number of strokes needed, first with FIG disjoint-set maintenance communication, communication for each FIG., If all the points of all even numbers, representing a Eulerian path If only one point, the stroke is not required, for a point with an odd number of strokes required is the number of singular points / 2

#include<bits/stdc++.h>

using namespace std;

int f[100005],deg[100005],g[100005],j[100005],anss,cnt,n,m;

int findf(int x)
{
    return x==f[x]?x:f[x]=findf(f[x]);
}

void hebing(int a,int b)
{
    int fa=findf(a);
    int fb=findf(b);
    if(fa!=fb)
    {
        f[fa]=fb;
    }
}

int main()
{
    while(cin>>n>>m)
    {
        anss=0;
        memset(deg,0,sizeof(deg));
        memset(g,0,sizeof(g));
        memset(j,0,sizeof(j));
        for(int i=1;i<=n;i++)
        f[i]=i;
        cnt=0;
        while(m--)
        {
            int a,b;
            cin>>a>>b;
            hebing(a,b);
            deg[a]++;
            deg[b]++;
         } 
        for(int i=1;i<=n;i++)
        {
            if(findf(i)==i)
            {
                g[cnt++]=i;
            }
            if(deg[i]%2==1)
            {
                j[findf(i)]++;
            }
        }
        for(int i=0;i<cnt;i++)
        {
            if(deg[g[i]]==0)
            continue;
            if(j[g[i]]==0)
            anss++;
            else
            anss+=j[g[i]]/2;
        }
        cout<<anss<<endl;
    }
 } 

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/12635208.html