Poj1041 undirected Euler's loop output in the smallest Ascending Order

Source: Internet
Author: User

John's trip

This is a good question. Since the graph has been connected, it is difficult to determine whether the graph is an even number of degrees and whether it is a European region.CodeThis topic makes me understand another powerful feature of deep search. In fact, every time I perform a small-to-large search, I first find a minimal-order loop, then we can search for each point on the ring. In fact, for the European Union, there is only one point left for each point, and nothing can be found, or there is another ring, you only need to insert all the paths on the ring to the corresponding location and use the stack storage path to trace back to the current vertex each time. That is to say, the current vertex has already been searched, the current vertex is put into the stack. In this way, an Euler loop can be obtained with the smallest ascending order.

  Void  DFS (  Int  U ){
Int I, V, E;
For (I = 1 ; I <= Num [u]; I ++ ){
E = Adj [u] [I];
If ( ! Mark [e]) {
V = Map [u] [E];
Mark [E] = 1 ;
DFS (v );
STK [ ++ Top] = E;
}
}
}

It is such a code that can realize the output of the Euler loop. The only special point is that STK [++ top] = E, which is executed only after the V point is searched, think about what it means. This is the true least-order Euler loop. For the implementation code, see:

Code

  # Include  <  Stdio. h  > 
# Include < Stdlib. h >
# Include < String . H >

# Define Nn 46
# Define MM 1998

Int Map [NN] [mm]; // Map [u] [e] = V, indicating that point U is connected to V through edge e
Int Adj [NN] [mm]; // Adj [u] [I] = E, indicating that the I-th edge of U is E
Int Num [NN]; // Number of edge entries of each vertex
Int Mark [mm];
Int STK [mm]; // Record Euler's path
Int Idx, S, top;

Int Min ( Int X, Int Y ){
Return X < Y ? X: Y;
}

Int CMP ( Const Void * A, Const Void * B ){
Return * ( Int * ) - * ( Int * ) B;
}

Void DFS ( Int U ){
Int I, V, E;
For (I = 1 ; I <= Num [u]; I ++ ){
E = Adj [u] [I];
If ( ! Mark [e]) {
V = Map [u] [E];
Mark [E] = 1 ;
DFS (v );
STK [ ++ Top] = E;
}
}
}
Int Main ()
{
Int X, Y, Z, I, maxn;
While (Scanf ( " % D " , & X, & Y) ! = EOF ){
If (X = 0 && Y = 0 ) Break ;
Memset (Num, 0 , Sizeof (Num ));
Scanf ( " % D " , & Z );
S = Min (x, y );
Adj [x] [ ++ Num [x] = Z;
Adj [y] [ ++ Num [y] = Z;
Map [x] [Z] = Y;
Map [y] [Z] = X;
While (Scanf ( " % D " , & X, & Y) ! = EOF ){
If (X = 0 && Y = 0 ) Break ;
Scanf ( " % D " , & Z );
Adj [x] [ ++ Num [x] = Z;
Adj [y] [ ++ Num [y] = Z;
Map [x] [Z] = Y;
Map [y] [Z] = X;
}
For (I = 1 ; I ++ ){
If (Num [I] = 0 ) Break ;
If (Num [I] & 1 ) Break ;
}
If (Num [I] ! = 0 ){
Puts ( " Round trip does not exist. " );
Continue ;
}
Maxn = I - 1 ;
For (I = 1 ; I <= Maxn; I ++ ){ // Sort the edge of point I
Qsort (adj [I] + 1 , Num [I], Sizeof ( Int ), CMP );
}
Memset (mark, 0 , Sizeof (Mark ));
Top = 0 ;
DFS (s );
For (I = Top; I > 1 ; I -- ){
Printf ( " % D " , STK [I]);
}
Printf ( " % D \ n " , STK [I]);
}
Return 0 ;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.