Euler's loop-horse riding fence repair

Source: Internet
Author: User

# Include <iostream>
# Include <string. h>
Using namespace STD;
Int f [1050] [1050], d [1050], Res [1550000];
Int N, j = 0, K = 0, m = 0;
Void search (int z)
{
For (INT I = 1; I <= m; I ++)
If (F [Z] [I]> 0)
{F [Z] [I] --;
F [I] [Z] --;
Search (I );
}
J ++;
Res [J] = z;
}
Int main (){
Cin> N;
For (INT I = 1; I <= N; I ++)
{Int X, Y;
Cin> x> Y;
F [x] [Y] ++;
F [y] [x] ++;
D [x] ++; d [y] ++;
M = max (x, m), y );
}
For (INT I = 1; I <= m; I ++)
If (d [I] % 2! = 0)
{K = I; break ;}
If (k = 0)
{
For (INT I = 1; I <= N; I ++)
If (d [I])
{K = I; break ;}}
Search (k );
For (INT I = J; I> 0; I --)
Cout <res [I] <Endl;
Return 0;
}

The program written by this scum. The idea is to plagiarize a player in wikioi.

Now, let me review our ideas, strengthen our understanding, and consolidate our memories.

F [x] [Y] ++;
F [y] [x] ++; these two statements are very good. The subtraction behind them is the eye of this algorithm (for personal opinions, Do not spray ). This sentence indicates the path that has been recorded, and some are marked as 1. The subtraction in the function indicates that the path has been passed once and is marked as 0, so that it will not be repeated. After understanding this, the algorithm will understand more than half of it.

D [X], d [y] indicates the total number of lines (like a qualifying line) at this point ?) To identify the odd and even points (the outlet is odd or even ).

M = max (x, m), Y); for this sentence, the maximum value of the vertex that appears in the record is used to determine the vertex range.

For (INT I = 1; I <= m; I ++)
If (d [I] % 2! = 0)
{K = I; break ;}
If (k = 0)
{
For (INT I = 1; I <= N; I ++)
If (d [I])
{K = I; break ;}} this is to find out if there is any odd degree (Baidu can not understand the meaning of the odd degree for the Euler loop ), if there is an odd degree, a small odd degree is recorded. If not, the minimum even point is recorded.

Then, after finding the smallest odd (even) degree point, you can start to use the function!

Void search (int z)
{
For (INT I = 1; I <= m; I ++)
If (F [Z] [I]> 0)
{F [Z] [I] --;
F [I] [Z] --;
Search (I );
}
J ++;
Res [J] = z;
} Starting from the smallest odd (even) degree, find the line composed of the vertex size and its closest vertex, and record the line as 0 to avoid repetition. The following J ++ records the number of occurrences of the vertex.

Save it with res.

I still don't understand why I need inverted output at the end ,. I will try again later.

Thank you for your advice.

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.