Getting Euler

 If you start experiencing all the edges of the entire map and only once, and then back to the starting point, this is the Euler from a node. Eulerian path.

 Analyzing method:

    1. undirected graph: graph given in FIG communication, and for all the nodes is an even number.
    2. the drawings: given graph of FIG communication, and zero for all nodes.

  Basic questions HDU 1878 to determine whether the Euler (undirected graph)

  Solution: if the output is 1, otherwise outputs 0; here I forget a thing, that is, if there is only one node i == pr [i], then this figure constitutes a loop. Then it is clear, the search and view of accumulating and determines whether the loop, with the CNT [] to record the number of each point, only one of both even and i == pr [i], it is a European back to the road.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int pr[1005],cnt[1005];
int find(int x)
{
    if(x!=pr[x])
        return pr[x]=find(pr[x]);
        return x;
}
void join(int a,int b)
{
    int f1=find(a),f2=find(b);
    if(f1!=f2)
        pr[f1]=f2;
    return ;
}
int main ()
{
    int n,m;
    while(cin>>n)
    {
        if(n==0)
            break;
        cin>>m;
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++)
            pr[i]=i;
        for(int i=1;i<=m;i++)
        {
            int a,b;
            cin>>a>>b;
            cnt [a] ++ ;
            cnt[b]++;
            join(a,b);
        }
        int du = 0 ;
        int ou = 0 ;
        for ( int i = 1 ; i <= n; i ++ )
        {
            if(cnt[i]%2==0)
                I ++ ;
            forceful (STOTT [in] == i)
                of ++ ;
        }
        if(ou==n&&du==1)
            cout<<"1"<<endl;
        else
            cout<<"0"<<endl;
    }
}

 

 

 

 

HDU - 3018

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. 

InputInput 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.OutputFor 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. 
    Gives a view to ask several strokes after all sides.
   Euler, knowledge has been mentioned in the last blog. For out of each point, if there is an odd number, then you need an odd / 2 pen to go through all the points.
    FIG analysis did not indicate whether FIG communication, so there may be a plurality of FIG, then this case, ans = number of odd number / 2 + Euler (containing only the even set point)
  parsing code
  
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;    //jishu/2+oulatu int PR [MAXN], CNT [MAXN], Mark [MAXN]; int n-, m; int ANS = 0 ; void First () {for (int I =. 1; I <= n-; I ++ ) {PR [I] = I;} Memset (CNT, 0, the sizeof (CNT)); Memset (Mark, 0, the sizeof (Mark)); ANS = 0 ;} int Find (int X) {IF (! X = PR [X]) return PR [X] = Find (PR [X]); return X;} void the Join (int A, int B) {int F1 = Find (A), F2 = Find (B); IF (! F1 = F2) PR [F1] = F2; return ;} void AC () {for (int I =. 1; I <= n-; I ++ ) {IF (CNT [I]% 2 = 0! ) {int F = Find (I) ; // count the number of odd-degree point. [] Array of the recording Mark, if i is odd points degrees, where i Euler FIG not, then the root node labeled i 1, this figure is not representative of Euler. Mark [F] =. 1 ; ANS ++ ;}} ANS / = 2 ; for (int I =. 1; I <= n-; I ++) // IF statistical Euler FIG {(CNT [I]> 0 ) // such as input, only 939 points are given a three relationships, certainly not a little, cnt [i] = 0, can not be included calculation. F = int { Find (i); i // root node is found, and if not labeled as 1, indicating the degree of i is an even number, and satisfies the pr [i] == i (i == f) ( i.e., found to x == pr or not labeled [x] when) Description this figure is a Euler, because if the degree of singularity points exist, PR I == [I] 
                    // marked must have been at. ++ ANS; IF (Mark [F] == 0 && PR [I] == I) {ANS ++ ;}}}} int main () {the while (CIN >> >> n- m) {First (); // initialize for ( . 1 I = int; I <= m; I ++ ) {int A, B; A CIN >> >> B; the Join (A, B); CNT [A] ++ ; // Add disjoint-set, the degree of statistical the degree of CNT [B] ++ ;} AC (); ANS COUT << << endl;} return 0 ;}

  

 

Guess you like

Origin www.cnblogs.com/liyexin/p/11695188.html